Using C # class division and partial method

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:

 

[c-sharp]  view plain copy
 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace PartialTypes  
  7. {  
  8.     internal partial class Singer  
  9.     {  
  10.         // declare two private fields  
  11.         private string _cn;  
  12.         private string _am;  
  13.   
  14.         // declare two distribution methods  
  15.         partial void getmsgA(string str);  
  16.         partial void getmsgB(int i);  
  17.   
  18.     }  
  19. }  

 

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:

 

[c-sharp]  view plain copy
 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace PartialTypes  
  7. {  
  8.     internal partial class Singer  
  9.     {  
  10.         // achieve some two partial method  
  11.         partial void getmsgA(string str)  
  12.         {  
  13.             Console.WriteLine ( "Method getmsgA the received parameters are {{0}}, the data type is {{1}]", str.ToString (), str.GetType (   ));
  14.         }  
  15.         partial void getmsgB(int i)  
  16.         {  
  17.             Console.WriteLine ( "Method getmsgB the received parameters are {{0}}, the data type is {{1}]", i.ToString (), i.GetType (   ));
  18.         }  
  19.   
  20.         // declare getmsg method calls two partial methods  
  21.         internal void getmsg(string str, int i)  
  22.         {  
  23.             getmsgA (str);  
  24.             getmsgB(i);  
  25.         }  
  26.   
  27.         // declare two properties, for reading and writing two private fields  
  28.         internal string Cn  
  29.         {  
  30.             get  
  31.             {  
  32.                 return _cn;  
  33.             }  
  34.             set  
  35.             {  
  36.                 _cn = value;  
  37.             }  
  38.         }  
  39.         internal string Am  
  40.         {  
  41.             get  
  42.             {  
  43.                 return _am;  
  44.             }  
  45.             set  
  46.             {  
  47.                 _am = value;  
  48.             }  
  49.         }  
  50.     }  
  51. }  

 

The main program file called partial classes and partial methods, as follows:

 

[c-sharp]  view plain copy
 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace PartialTypes  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             // Create a partial class object attribute value input  
  13.             Singer sin = new Singer();  
  14.             Console.Write ( "Please enter sin Cn object attribute value:");  
  15.             sin.Cn = Console.ReadLine();  
  16.   
  17.             Console.Write ( "Please enter Am sin object attribute value:");  
  18.             sin.Am = Console.ReadLine();  
  19.   
  20.             // output  
  21.             Console.WriteLine ( "/ n-reading result, Cn = {0}, Am   = {1}", sin.Cn, sin.Am);
  22.   
  23.             // method call getmsg  
  24.             sin.getmsg ( "Sino-US cooperation", 50);  
  25.   
  26.         }  
  27.     }  
  28. }  

operation result:

Guess you like

Origin www.cnblogs.com/wwwbdabc/p/11683349.html