Asp. The partial class part of net

Source: Internet
Author: User
If you are developing a common function library for a project, the richer the content of the common function library, the better, but it is not possible to write it all at once, need 1.1 points of accumulation, then you can use the partial Class, every time you develop a new partial Class is uploaded to the server or to a specific directory, without the need to copy and paste the new code into the original program code, reducing unnecessary hassles.

The partial type is a pure language layer of compilation and does not affect any execution mechanism-in fact, the C # compiler still merges the local types of parts into a complete class at compile time.


First, under what circumstances use partial classes?

(1) The type is particularly large, should not be placed in a file to achieve.
(2) A part of the code for a type is generated by the automation tool, and it is not appropriate to mix it with the code we write ourselves.
(3) More people are required to cooperate in the preparation of a class.


Second, modifier on the partial type

(1) Access modifiers on the various parts of a type must maintain consistency.
(2) If a type has a section that uses the abstract modifier, the entire class is treated as an abstract class.
(3) If a type has a section that uses the sealed modifier, the entire class is treated as a sealed class.
(4) Each part of a class cannot use conflicting modifiers, such as the inability to use abstract on one part, and the use of sealed on another.


Iii. base classes and interfaces for partial types

(1) The base class specified on each part of a type must be consistent. A section can not specify a base class, but it must be the same if specified.
(2) The interface on the partial type has an "additive" effect.
Partial class Class2:iinterface1, Iinterface2 {}
Partial class Class2:iinterface3 {}
Partial class Class2:iinterface2 {}
Equivalent
Class Class2:iinterface1, Iinterface2, Iinterface3 {}


Iv. application Characteristics of local types

The attribute on the local type has an "additive" effect.


[Attribute1, Attribute2 ("Hello")]
Partial class class1{}

[Attribute3, Attribute2 ("Exit")]
Partial class class1{}

Equivalent
[Attribute1, Attribute2 ("Hello"), Attribute3, Attribute2 ("Exit")]
Class Class1 {}

Note: The Attribute2 property allows multiple use on a class.


V. Limitations of local types

1. All partial type definitions that are to be part of the same type must be decorated with partial. As shown below:

Public partial class A {}

public class A {}//Error, must also is marked partial

2. The partial modifier can only appear immediately preceding the keyword class, struct, or interface (no enumeration or other type can use partial);

3. All partial type definitions that are to be parts of the same type must be defined in the same assembly and in the same module (. exe or. dll file). A partial definition cannot span multiple modules;

4. The class name and generic type parameters must match in all partial type definitions. A generic type can be partial. Each partial declaration must use the same parameter name in the same order.

5. Local types are only applicable to classes, interfaces, structs, and delegates and enumerations are not supported.

6. Each part of a type must be compiled at the same time.


Six, the use of the partial need to pay attention to some of the following

1. Use the partial keyword to indicate that other parts of the class, struct, or interface can be defined within a namespace

2. All parts must use the partial keyword

3. Each part must have the same accessibility, such as public, private, etc.

4. If any part is declared abstract, the entire type is treated as an abstract

5. If any part is declared as sealed, the entire type is considered sealed

6. If any part of the declaration inherits the base class, the entire type inherits the class

7. Each section can specify a different base interface, and the final type will implement all the interfaces listed by all the partial declarations

8. Any class, struct, or interface member declared in a partial definition can be used by all other parts

9. Nested types can be partial, even if the type in which they are nested is not part of the division itself.


Vii. instances of partial

The example class is defined as partial class, and the three methods M1, M2, and m3 of the class are defined separately in Example1. CS, Example2. CS, Example3. CS Three class file, and then in Partialclass.aspx. The example class is instantiated in CS and the method in the class is called.

Example1.cspublic partial class example{public    string M1 ()    {        return ' Method 1 ';    }} Example2.cspublic partial class example{public    string m2 ()    {        return ' Method 2 ';    }} Example3.cspublic partial class example{public    string m3 ()    {        return ' Method 3 ';    }}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.