Using System. Collections. Generic;, wireshark

Source: Internet
Author: User

Using System. Collections. Generic;, wireshark

Type parameters make it unnecessary to determine one or more specific parameters when designing classes and methods. The specific parameters can be declared and implemented in the Customer Code.

This means that the generic type parameter T is used to write a class MyList <T>. The customer code can call: MyList <int>, MyList <string> or MyList <MyClass>.

This avoids the cost and risks of type conversion or packing during running.

The following is a common method call:

1 public static void ShowInt (int iValue) 2 {5 Console. writeLine ("ShowInt method display {0}, whose type is {1}", iValue, typeof (int); //, iValue. getType (); 6} 7 8 public static void ShowLong (long lValue) 9 {10 Console. writeLine ("ShowLong method display {0}, whose type is {1}", lValue, lValue. getType (); 11} 12 13 public static void ShowString (string sValue) 14 {15 Console. writeLine ("ShowString method: {0}, whose type is {1}", sValue, sValue. getType (); 16} 17 18 public static void ShowDateTime (DateTime dValue) 19 {20 Console. writeLine ("ShowDateTime method display {0}, whose type is {1}", dValue, dValue. getType (); 21}

In this way, it is inconvenient to call different methods according to different categories each time.

/// <Summary> /// 1. The Child class inherits all attributes and behaviors of the parent class, where any parent class appears, subclass can replace // 2. The object type is the parent class of all types // </summary> /// <param name = "oValue"> </param> public static void ShowObject (object oValue) {Console. writeLine ("ShowObject method display {0}, whose type is {1}", oValue, oValue. getType ());}

In this way, no matter what type of object can be called, only one method is required to achieve code reuse.

However, you need to bind and unpack the containers to reduce the efficiency. For more information, see the following method:

1 public static void Show <T> (T tValue) 2 {3 Console. writeLine ("Show <T> method display {0}, whose type is {1}", tValue, tValue. getType (); 4}

Through the extension delay statement, the two shortcomings can be combined to achieve code reuse.

The so-called latency statement is to replace it with a placeholder and specify the required type when it is used.

Generic ----------------------------------------------------------- generic constraints:

Class indicates the reference type, and struct indicates the value type.

1 public static T Show <T> (T tValue) 2 // where T: class // reference type 3 // where T: struct // Value Type 4 {5 Console. writeLine ("Show <T> method display {0}, whose type is {1}", tValue, tValue. getType (); //, iValue. getType (); // 6 return default (T); // provide the default value 7 based on the generic type}
1 public class BaseModel 2 {3 public int Id {get; set;} 4 public string TableName {get; set;} 5} 6 7 public class People: baseModel 8 {9 public People () 10 {} 11 public string Name {get; set;} 12 public int Age {get; set;} 13 public string Sex {get; set;} 14 /// <summary> 15 /// greeting 16 /// </summary> 17 public string Hi {get; set ;} 18} 19 20 public interface ISports21 {22 void KickShuttlecock (); 23} 24 25 public interface IWork26 {27 void DoJob (); 28} 29 30 public class Chinese: People, ISports, IWork31 {32 public string Majiang {get; set ;} 33 // <summary> 34 // constructor 35 /// </summary> 36 /// <param name = "name"> </param> 37 public Chinese (string name) 38 {39 base. hi = "Good morning, have you eaten?"; 40 base. tableName = "P_Chinese"; 41} 42 public Chinese () 43 {44 base. hi = "Good morning, have you eaten?"; 45 base. tableName = "P_Chinese"; 46} 47 48 49 public void KickShuttlecock () 50 {51 Console. writeLine ("kicker"); 52} 53 54 public void DoJob () 55 {56 Console. writeLine ("support"); 57} 58}View Code
1 public static T Hi <T> (T t) where T: People, ISports, IWork, new () 2 {3 T tModel = new T (); // new () constraints indicate that T has a non-parameter constructor 4 tModel. id = 11; // The 5 tModel is determined by the People constraint. name = "Flowers and carrots"; // The people constraint determines 6 tModels. sex = "male"; // The people constraint determines 7 8 tModels. kickShuttlecock (); // The ISports interface constraint determines 9 tModel. doJob (); // The IWork interface constraint determines 10 return tModel; 11}

As shown above, constraints are a limitation. Only parameters that meet the constraints can replace the given type parameters.

For example, the following call:

1 Chinese = new chinese () 2 {3 Id = 123,4 Name = "tianyao♂Happy "5}; 6 GenericConstraint. Hi <Chinese> (chinese );

 

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.