The New Keyword provided by the partial keyword C #2.0 is used to split the definition of a class, struct, or interface and write it in different source files. Each source file contains a part of the class definition. All parts are combined during compilation of the application. In the following situations, you need to split the classification definition:
1. When processing large projects, assigning a class to multiple independent files allows multiple programmers to process the class at the same time.
2. When using an automatically generated source, you can add the code to the class without re-creating the source file. Visual Studio uses this method when creating Windows Forms and Web Service packaging code. You can create code that uses these classes without editing the files created by Visual Studio.
Usage: [modifiers] partial type
Modifier includes abstract new override virtual static extern and one of the four access modifiers (public private protected internal. Type includes one of the class struct interfaces.
See the following definition:
Public partial class Employee
{
Public void DoWork ()
{}
}
Public partial class Employee
{
Public void GoToLunch ()
{}
}
The code above defines a class of Employee, which has two methods: DoWork () and GoToLunch (). You can regard the above Code as follows: in fact, the compiler is also merged during compilation. The compiler will put all the information of a partial class together during compilation, so we can expand it. If we have already defined two interfaces: our class definition is as follows: This is necessary when developing a distributed system. Multiple programmers only need to develop their own parts.
Public class Employee
{
Public void DoWork ()
{}
Public void GoToLunch ()
{}
}
Interface IDoSomeThing
{
Void DoWork ();
Void DoPlay ();
}
Interface IGoToDinner
{
Void GoToBreakfast ();
Void GoToLunch ();
Void GoToSupper ();
}
Public partial class Employee: IDoSomeThing
{
Public void DoWork ()
{}
Public void DoPlay ()
{}
}
Public partial class Employee: IGoToDinner
{
Public void GoToBreakfast ()
{}
[System. ObsoleteAttribute]
Public void GoToLunch ()
{}
Public void GoToSupper ()
{}
}
Therefore, the compiler combines the above into a class during compilation:
Public class Employee: IDoSomeThing, IGoToDinner
{
Public void DoWork ()
{}
Public void DoPlay ()
{}
Public void GoToBreakfast ()
{}
[System. ObsoleteAttribute]
Public void GoToLunch ()
{}
Public void GoToSupper ()
{}
}
Note the following points: 1. to define all the segment types of each part of the same type, you must use partial for modification. The following Error occurs: public partial class A {} // public class A {} // Error, the must also be marked partial2.partial modifier can only appear next to the class, struct, or interface keyword. 3. nested segment types are allowed in the segment type definition. Of course, if the nested class is also a partial class, it also complies with the above rules. For example:
Partial class ClassWithNestedClass
{
Partial class NestedClass {}
}
Partial class ClassWithNestedClass
{
Partial class NestedClass {}
}
4. to define all the parts of the same project in the same program set and in the same example (.exe or. dll file. The Division definition cannot span multiple modules. 5. The class name and generic type parameters must be matched in all segment type definitions. Generic types can be segments. Each division declaration must use the same parameter name in the same order.