When we create a new Windows Form, the backend code automatically adds the following code:
1 public partial class Form1:form
2 {
3 Public Form1 ()
4 {
5 InitializeComponent ();
6}
7}
Why is the class Form1 to be modified with partial? Where is the partial mainly used?
Why the newly created Winows form class is defined as a partial class. We open the Form1.Designer.cs file and we can see that another part of the original Form1 class was designed in this file. This part of the class defines the controls we use, the event delegates, and so on, such as the Dispose method. Because the code inside is automatically generated, it's designed to be a partial class.
Partial is the meaning of a local type. Allows us to divide a class, struct, or interface into several parts, implemented in several different. cs files, respectively. The C # compiler still merges the local types of parts into one complete class at compile time
Local types are suitable for the following situations:
(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.
Restrictions on local types
(1) Local types are only applicable to classes, interfaces, structs, and delegates and enumerations are not supported.
(2) Each part of the same type must have a modifier partial.
(3) When using a local type, each part of a type must be in the same namespace.
(4) Each part of a type must be compiled at the same time.
3. Local type of attention point
(1) The keyword partial is a contextual keyword that only has the meaning of the keyword when it is put together with class, struct, interface. Therefore, the introduction of partial does not affect variables with the name partial in the existing code.
(2) Parts of a local type are generally separated into several different. cs files, but the C # compiler allows us to place them in the same file.
4. 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.
5. Modifiers on a local 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.
6. Base classes and interfaces for local 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 local type has a "cumulative" effect.
Partial class Class2:iinterface1, Iinterface2 {}
Partial class Class2:iinterface3 {}
Partial class Class2:iinterface2 {}
Equivalent
Class Class2:iinterface1, Iinterface2, Iinterface3 {}