Introduction and understanding of generics in C #

Source: Internet
Author: User

Generics in C #, literally, can be guessed, that is, a generic type, that is, type is indeterminate. Used in programming, which means that different types can be abstracted. The concept is simple, and for us novices, the main problem

It may be the mastery of the role of generics, the rules of usage and grammar, shorthand, and so on.

First, the whole of generics is introduced. Before learning and mastering generics, we need to remember a concept in which the C # language is object-oriented and generics are classes.

Generics are like classes, including generic classes, generic methods, generic interfaces, and generic structures.

I. Definition of generic type

1. Generic type

1 class Mygeneric<t>2{3     //... .... 4 }

2. Generic method

1  Public void Mymethod<t>()2{  3       //...   .. 4 }  

3. Generic interface

1 Interface Myinterface<t>2{  3     //...   4 }  

4. Generic structure

1 struct Mystruct<t>2{  3      //...   .. 4 }  

Through the observation of the above generic class , the generic method , the generic interface and the generic structure 's declaration syntax, we can easily find that the generic type is just a little more than the syntax of the class declaration in C #. The type parameter is used to indicate that no

Same type. Adding "<T>" is followed by the name of the class, method, and so on. In addition, we also know that the declarations of classes, methods, interfaces, and interfaces can contain many different types, so the same is true for generic classes, generic-side

methods, generic interfaces, and generic structures can of course contain multiple types of indeterminate generic parameters, and you need to add different generic parameters to <> when using multiple generic parameters.

Second, the use of generics

The use of generics, summed up in a sentence is that the use of ordinary classes is almost the same, but in the use of the type parameters need to be identified as a clear type.

Iii. Considerations for Generics

1, sometimes when defining generics, we need to determine that the generic type parameter must satisfy a certain condition, then we can use the WHERE keyword to implement, the purpose is to let the type parameter must implement a type interface, if

To ensure that a type parameter can be instantiated, you need to add a new () constraint at the end of the constraint in where.

2, we need to pay attention to the mutual transformation between the generic type problem.

Introduction and understanding of generics in C #

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.