Definition of distribution class:
You can split the definition of a class, structure, or interface into two or more 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:
When processing large projects, distributing a class to multiple independent files allows multiple programmers to process the class at the same time.
When an automatically generated source is used, the code can be added 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 using these classes without editing the files created by Visual Studio.
To split the classification definition, use the partial keyword modifier as follows:
Public partial class Employee
{
Public void DoWork ()
{
}
}
Public partial class Employee
{
Public void GoToLunch ()
{
}
}
Note the following:
UsePartialKeyword indicates that the class, structure, or other parts of the interface can be defined in the namespace. All parts must be usedPartialKeyword. During compilation, each part must be used to form the final type. Each part must have the same accessibility, such as public and private.
If any part is declared as abstract, the entire type is considered abstract. If any part is declared as sealed, the entire type is regarded as sealed. If any part is declared as the base type, the entire type inherits the class.
All the parts of the specified base class must be consistent, but the parts that ignore the base class still inherit the base type. You can specify different basic interfaces for each part. The final type will implement all interfaces listed in all segment declarations. Any class, structure, or interface member declared in a segment definition can be used by all other parts. The final type is the combination of all parts during compilation.
The following rules must be followed when processing the definition of a division class:
All the division type definitions for each part of the same type must be used.Partial.
PartialModifiers can only appear near keywords.Class,StructOrInterfacePrevious position
The segment type definition allows the use of nested segment types, for example:
Partial class ClassWithNestedClass
{
Partial class NestedClass {}
}
Partial class ClassWithNestedClass
{
Partial class NestedClass {}
}
To define all the sub-departments of different regions in the same region, they must be defined in the same program set and in the same region (.exe or. dll file. The Division definition cannot span multiple modules.
The class name and generic type parameters must match in all the division type definitions. Generic types can be segments. Each division declaration must use the same parameter name in the same order.
The following keywords are optional for the definition of the division type, but if a keyword appears in a division type definition, this keyword cannot conflict with the keyword specified in other segment definitions of the same type: public protected private internal abstract sealed base class new modifier general constraint
Let's take a column as an example.
Public partial class CoOrds
{
Private int x;
Private int y;
Public CoOrds (int x, int y)
{
This. x = x;
This. y = y;
}
}
Public partial class CoOrds
{
Public void PrintCoOrds ()
{
System. Console. WriteLine ("CoOrds: {0}, {1}", x, y );
}
}
Class TestCoOrds
{
Static void Main ()
{
CoOrds myCoOrds = new CoOrds (10, 15); myCoOrds. PrintCoOrds ();}
Output result:
CoOrds: 10, 15
Example 2:
Partial interface ITest
{
Void Interface_Test ();
}
Partial interface ITest
{
Void Interface_Test2 ();} partial struct S1
{
Void Struct_Test (){}
}
Partial struct S1
{
Void Struct_Test2 (){}
}
Through this column, we can also develop the distribution interface and distribution structure!
Finally, let's take a column. This is a Windows application. Add three testboxes to the Form, and then add a Combox and a button to perform addition, subtraction, multiplication, division, and execution operations.
The sample code is as follows:
Using System;
Using System. Data;
Using System. Text;
Using System. Windows. Forms;
Namespace Test02
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Partial class account // the first part of the Division class
{
Public int addition (int a, int B) // create an integer Method
{
Return a + B; // addition operation in the Method
}
}
Partial class account // the second part of the Division class
{
Public int multiplication (int a, int B) // create an integer Method
{
Return a * B; // Multiplication operation in the Method
}
}
Partial class account // Subtraction
{
Public int subtration (int a, int B)
{
Return a-B;
}
}
Partial class account // Division
{
Public int division (int a, int B)
{
Return a/B;
}
}
Private void Form1_Load (object sender, EventArgs e)
{
ComboBox1.SelectedIndex = 0;
ComboBox1.DropDownStyle = ComboBoxStyle. DropDownList;
}
Private void button#click (object sender, EventArgs e)
{
Try
{
Account at = new account ();
Int M = int. Parse (txtNo1.Text. Trim ());
Int N = int. Parse (txtNo2.Text. Trim ());
String str = comboBox1.Text;
Switch (str)
{
Case "plus": txtResult. Text = at. addition (M, N). ToString (); break;
Case "minus": txtResult. Text = at. subtration (M, N). ToString (); break;
Case "Multiply": txtResult. Text = at. multiplication (M, N). ToString (); break;
Case "except": txtResult. Text = at. division (M, N). ToString (); break;
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
}
}
OK. I am here today. I will share some new ideas with you later. If you have any mistakes, please point out that you have some personal opinions. Please advise! Thank you!