The preparation of the same class in multiple files, type each file with a different name, same class name, class name preceded by partial keyword, this type is called partial classes.
In the partial class you can create partial methods, add the keyword partial, partial methods method can only be divided into two parts before the method name, that declaration part and implementation part are located in a different class divisions. The method can segment a static method, but it must be implicit Private method, and the method returns no value, since the method of time division method private, virtual modifier can not be used, i.e., can not be virtual method; division method can have ref parameter, but you can not have an out parameter; not allowed to delegate converting division method.
Firstly, the embodiment items PartialTypes, the main class file is Program.cs, add a class file in the project SingerDefine.cs, class named Singer, add another class file SingerImp.cs, class named Singer. The partial modifier are added before the class name
Two private fields and two distribution methods SingerDefine.cs file a statement before the method name with the keyword partial, code is as follows:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PartialTypes
- {
- internal partial class Singer
- {
- // declare two private fields
- private string _cn;
- private string _am;
- // declare two distribution methods
- partial void getmsgA(string str);
- partial void getmsgB(int i);
- }
- }
SingerImp.cs file, comprising achieve some partial method, a division method are combined into new methods, two attributes define a private field of reading and writing, as follows:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PartialTypes
- {
- internal partial class Singer
- {
- // achieve some two partial method
- partial void getmsgA(string str)
- {
- Console.WriteLine ( "Method getmsgA the received parameters are {{0}}, the data type is {{1}]", str.ToString (), str.GetType ( ));
- }
- partial void getmsgB(int i)
- {
- Console.WriteLine ( "Method getmsgB the received parameters are {{0}}, the data type is {{1}]", i.ToString (), i.GetType ( ));
- }
- // declare getmsg method calls two partial methods
- internal void getmsg(string str, int i)
- {
- getmsgA (str);
- getmsgB(i);
- }
- // declare two properties, for reading and writing two private fields
- internal string Cn
- {
- get
- {
- return _cn;
- }
- set
- {
- _cn = value;
- }
- }
- internal string Am
- {
- get
- {
- return _am;
- }
- set
- {
- _am = value;
- }
- }
- }
- }
The main program file called partial classes and partial methods, as follows:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PartialTypes
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create a partial class object attribute value input
- Singer sin = new Singer();
- Console.Write ( "Please enter sin Cn object attribute value:");
- sin.Cn = Console.ReadLine();
- Console.Write ( "Please enter Am sin object attribute value:");
- sin.Am = Console.ReadLine();
- // output
- Console.WriteLine ( "/ n-reading result, Cn = {0}, Am = {1}", sin.Cn, sin.Am);
- // method call getmsg
- sin.getmsg ( "Sino-US cooperation", 50);
- }
- }
- }
operation result: