C # core syntax-generic (describes in detail the generic methods, generic classes, generic interfaces, and generic constraints to understand the variant inversion ),

Source: Internet
Author: User

C # core syntax-generic (describes in detail the generic methods, generic classes, generic interfaces, and generic constraints to understand the variant inversion ),

Generic is a new feature of C # Language 2.0 and general language runtime (CLR. The generic. NET Framework introduces the concept of type parameters. 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.

  • Generic Overview

Generic classes and generic methods combine reusability, type security, and efficiency. They are not suitable for non-generic classes and methods. Generics are widely used in containers (collections) and methods for container operations .. NET Framework 2.0 Class Library provides a new namespace System. Collections. Generic, which contains some new Generic-based container classes. For the sample code of the new generic container class (collection classes), see generic in the base class library. Of course, you can also create your own generic classes and methods to provide your own general solutions and design patterns, which are safe and efficient. The following sample code demonstrates a simple generic linked list class. (In most cases, we recommend that you use the List <T> class provided by the. NET Framework class library, instead of creating your own tables .) Type parameterTIt is used in multiple places. The specific types are usually used to specify the type of elements in the table. Type parameter T has the following usage:

L in the AddHead method, it is used as the type of the method parameter.

L The type of the return value in the public method GetNext and the Data attribute of the nested class Node.

L type of private member data in a nested class.

Note that T is also effective for nested class nodes. When a specific class is used to implement MyList <T> -- for example, MyList <int> -- replace int for each T that appears.

  

using System;using System.Collections.Generic; public class MyList<T> //type parameter T in angle brackets    {        private Node head;     // The nested type is also generic on T.        private class Node                  {            private Node next;//T as private member data type:            private T data;         //T used in non-generic constructor:            public Node(T t)                    {                next = null;                data = t;            }            public Node Next            {                get { return next; }                set { next = value; }            }//T as return type of property:            public T Data                       {                get { return data; }                set { data = value; }            }        }        public MyList()        {            head = null;        }//T as method parameter type:        public void AddHead(T t)            {            Node n = new Node(t);            n.Next = head;            head = n;        }         public IEnumerator<T> GetEnumerator()        {            Node current = head;             while (current != null)            {                yield return current.Data;                current = current.Next;            }        }    }

The following sample code demonstrates how the Customer Code uses the generic class MyList <T> to create an integer table. By simply changing the parameter type, it is easy to rewrite the following code to create a string or other custom type table.

class Program    {        static void Main(string[] args)        {//int is the type argument.           MyList<int> list = new MyList<int>();            for (int x = 0; x < 10; x++)                list.AddHead(x);             foreach (int i in list)            {                Console.WriteLine(i);            }            Console.WriteLine("Done");        }    }
  • Advantages of generics

For earlier general language runtime and C # Language limitations, generics provide a solution. Previous types of generalization were implemented by mutual conversion between types and global base class System. Object .. The ArrayList container class of the NET Framework base class library is an example of this limitation. ArrayList is a convenient container class, which can store any reference type or value type without any changes.

//The .NET Framework 1.1 way of creating a listArrayList list1 = new ArrayList(); list1.Add(3);list1.Add(105);//...ArrayList list2 = new ArrayList();list2.Add(“It is raining in Redmond.”);list2.Add("It is snowing in the mountains.");//...

 

However, there is a cost for this convenience. This requires that any reference type or value type added to the ArrayList be implicitly converted to System. Object. If these elements are of the value type, they must be boxed when added to the list; when they are retrieved again, they must be split. The type conversion, packing, and unpacking operations both reduce the performance. When an iteration is required for a large container, the effects of packing and unpacking may be significant.

 

Another limitation is the lack of type check during compilation. When an ArrayList converts any type to an Object, it cannot prevent operations similar to the following in the Customer Code during compilation:

 

ArrayList list = new ArrayList(); //Okay.  list.Add(3); //Okay, but did you really want to do this?list.Add(.“It is raining in Redmond.”); int t = 0;//This causes an InvalidCastException to be returned.    foreach(int x in list){  t += x;}

 

Although this is completely legal, and sometimes it is intended to create a container containing different types of elements in this way, but put the string and int variables in an ArrayList, it is almost a manufacturing error, this error is not found until it is running.

 

In C # languages 1.0 and 1.1, you can avoid the danger of generic code (generalized code) in the container class of the. NET Framework Class Library only by writing your own specific types of containers. Of course, because such a class cannot be reused by other data types, it will lose the advantage of generics. You must rewrite this class for each type that needs to be stored.

 

What ArrayList and other similar classes really need is a way that allows the customer code to specify the desired data type before instantiation. In this way, you do not need to convert the data type to the Object, and the compiler can perform a type check at the same time. In other words, ArrayList requires a type parameter. This is exactly what generics provide. In the Generic List <T> container in the System. Collections. Generic namespace, add elements to the container, as shown in the following code:

 

List<int> list1 = new List<int>();//No boxing, no casting:list1.Add(3);//Compile-time error:list1.Add("It is raining in Redmond.");

 

Compared with ArrayList, the unique List <T> syntax added in the Customer Code is Declaration and type parameter in instantiation. The slightly complicated code returns that the table you create is not only safer than the ArrayList, but also faster, especially when the elements in the table are of the value type.

  • Generic parameters

In the definition of generic type or generic method, a type parameter is a placeholder (placeholder), usually a capital letter, such as T. When the Customer Code declares and instantiates a variable of this type, T is replaced with the data type specified by the customer code. Generic classes, such as the MyList <T> class in the generic overview, cannot be used as-is because it is not a real type, but more like a type blueprint. To use MyList <T>, the Customer Code must specify a type parameter in angle brackets to declare and instantiate a constructed type ). The type parameter of this specific class can be any type recognized by the compiler. You can create any number of constructed type instances, each of which uses different type parameters, as shown below:

MyList<MyClass> list1  = new MyList<MyClass>();MyList<float> list2 = new MyList<float>();MyList<SomeStruct> list3 = new MyList<SomeStruct>();

In these MyList <T> instances, each T in the class will be replaced by the type parameter during running. With this replacement, we only use the code for defining classes to create three independent, secure and efficient objects. For more information about CLR replacement, see runtime generics.

 

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.