Home >Backend Development >C#.Net Tutorial >Partial Class in ASP.NET
If you are developing a public function library for a project, the richer the content used for the public function library, the better. However, it cannot be written all at once and requires accumulation bit by bit. At this time, you can use Partial Class. Upload your newly developed Partial Class program to the server or a specific directory every once in a while; there is no need to copy and paste the new code into the original program code, reducing unnecessary trouble.
Partial type is a pure language layer compilation process that does not affect any execution mechanism - in fact, the C# compiler will still merge the local types of each part into a complete class during compilation.
1. Under what circumstances should partial classes be used?
(1) The type is very large and should not be implemented in one file.
(2) Part of the code in a type is code generated by automated tools and should not be mixed with code written by ourselves.
(3) Multiple people need to collaborate to write a class.
2. Modifiers on Partial types
(1) Access modifiers on various parts of a type must maintain consistency.
(2) If a part of a type uses the abstract modifier, then the entire class will be considered an abstract class.
(3) If a part of a type uses the sealed modifier, then the entire class will be considered a sealed class.
(4) Each part of a class cannot use contradictory modifiers. For example, you cannot use abstract on one part and sealed on another part.
3. Base classes and interfaces of Partial types
(1) The base classes specified on each part of a type must be consistent. A certain part does not need to specify a base class, but if specified, it must be the same.
(2) The interface on the Partial type has a "cumulative" effect.
partial class Class2: Iinterface1, Iinterface2 {}
partial class Class2: Iinterface3 {}
partial class Class2: Iinterface2 {}
Equivalent to
class Class2: Iinterface1, Iinterface2, Iinterface3 {}
4. Partial types Applied properties
Properties on local types have an "additive" effect.
[Attribute1, Attribute2("Hello")]
partial class Class1{}
[Attribute3, Attribute2("Exit")]
partial class Class1{}
is equivalent to
[Attribute1, Attribute2("Hello" ), Attribute3, Attribute2("Exit")]
class Class1 {}
Note: The Attribute2 attribute is allowed to be used multiple times on a class.
5. Restrictions on partial types
1. All partial type definitions that are to be parts of the same type must be modified with partial. As shown below:
public partial class A { }
public class A { } // Error, must also be marked partial
2. The partial modifier can only appear immediately before the keyword class, struct or interface Position (partial cannot be used for enumerations or other types);
3. All partial type definitions that want to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file) . Partial definitions cannot span multiple modules;
4. Class names and generic type parameters must match in all partial type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
5. Local types are only applicable to classes, interfaces, and structures, and do not support delegation and enumeration.
6. All parts of a type must be compiled at the same time.
6. When using Partial, you need to pay attention to the following situations
1. Use the partial keyword to indicate that other parts of the class, structure or interface can be defined within the namespace
2. All parts must use partial Keywords
3. Each part must have the same accessibility, such as public, private, etc.
4. If any part is declared as abstract, the entire type is considered abstract
5. If any part is declared abstract If a part is declared as sealed, the entire type is considered sealed
6. If any part is declared to inherit the base class, the entire type will inherit the class
7. Each part can specify different base interfaces, and finally The type will implement all interfaces listed in all partial declarations
8. Any class, structure or interface member declared in a partial definition is available to all other parts
9. Nested types can be partial even if the type they are nested in is not itself partial.
7. Partial instance
Define the Example class as Partial Class, and define the three methods m1, m2, and m3 of this class in Example1 respectively. cs,Example2. cs,Example3. cs three class files, and then in PartialClass.aspx. Instantiate the Example class in cs and call the methods in the class.
//Example1.cs public partial class Example { public string m1() { return "Method 1 "; } } //Example2.cs public partial class Example { public string m2() { return "Method 2 "; } } //Example3.cs public partial class Example { public string m3() { return "Method 3 "; } }