. NET basic literacy-custom generic classes

Source: Internet
Author: User

C # Custom generic classes are most widely used in collections (Collection). In fact, one of the reasons for generics is to solve the packing and unpacking problems of elements in the original collection class (if the concept of boxing and unpacking is unclear, please search Baidu). Because of the use of generics, all elements in the collection belong to the same class, which eliminates the pitfalls of different types in the compile phase-if the type is not correct, compile the error.

Only C # Custom generic classes are discussed here.

The basic customizations are as follows :

public class Mygeneric < t>  {  private T member;  public void Method (T obj)  {  }  }


Here, a generic class is defined , where T is a class that can be used in a defined class. Of course, there are no problems with defining multiple generic classes.

public class Mygeneric < TKey, tvalue>  {   private TKey key;   private TValue value;    public void Method (TKey K, TValue v)   {   }  }  

initialization of generics : Generics are required to be initialized. After using Tdoc = Default (T), the system is automatically initialized for generics.

restriction : If we know that the generic class T that will be passed in must have some properties, then we can use these properties of T in Mygeneric<t>. This is achieved through interface.

First define a interface public  interface idocument  {     string Title {get;}     String Content {get;}  }   Let the paradigm Class T implement this interface public  class Mygeneric < t>  where t:idocument  {public       void Method (T v) 
   {           Console.WriteLine (v.title);       }  }   The passed-in class must also implement Interface public  class Document:idocument  {  }   //using this generic  

Generic Methods : We can also define methods for generics

void swap< t> (ref T X, ref T y)  {  T temp = x;  x = y;  y = temp;  }  

generic Proxy (genericdelegate): Since you can define a generic method, you can naturally define a generic proxy

public delegate void Delegatesample < t> (ref T X, ref T y)   private void Swap (ref T X, ref T y)  {      T tem p = x;      x = y;      y = temp;  }   Call public  void Run ()  {     int i,j;     i = 3;     j = 5;     delegatesample< int> sample = new delegatesample< int> (Swap);     Sample (I, j);  

Set nullable value types : In general, variables of value types are non-null. But,nullable<t> can solve this problem.

nullable< int>x;   This sets a nullable integer variable x x = 4; x + = 3; if (x.hasvalue)   //Use the HasValue property to check if X is empty {Console.WriteLine ("x=" +x.tostring ());} x = null;    

Use arraysegment<t> to get part of the array. If you want to use a subset of elements of an array, it's a good idea to use arraysegment directly to delimit them.

int[] arr = ... {1, 2, 3, 4, 5, 6, 7, 8, 9}; The first argument is the pass-through array, the second argument is the offset of the starting segment within the array, and the third parameter is the number of consecutive numbers arraysegment<int> segment = new arraysegment< int> (arr, 2, 3) ;  (Array, offset, count) for   (int i =segment. Offset; i< = segment. Offset + segment. Count; i++) {    Console.WriteLine (segment. Array[i]);    Use the array property to access the passed array}

In the example, you can achieve the purpose of accessing different segments by setting the Offset property and the Count property to a different value.

. NET basic literacy-custom generic classes

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.