C # Generic,

Source: Internet
Author: User

C # Generic,
C # Generic

1. Define generic classes

You can create a generic class by including the angle brackets syntax in the class definition:

1 class MyGenericClass <T> 2 {3 // Add code 4}

T can follow any character of the C # naming rule. T can be regarded as the type of member variables, the type of returned attributes or methods, and the type of parameters of methods. For example:

1 class MyGenericClass <T> 2 {3 private T obj; 4 public MyGenericClass (T item) 5 {6 obj = item; 7} 8 public T Obj 9 {10 get {return obj;} 11 12} 13}

Note: 1) It cannot be assumed what type is provided for the class;

2) When comparing the type values provided by the generic type and null, you can only use = and! =.

 

2. default keyword

1 public MyGenericClass () 2 {3 obj = default (T); 4}

The result is that if obj is of the reference type, null is assigned to it. If obj is of the value type, the default value is assigned.

 

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.