在特..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C#中的5個(gè)泛型類(lèi)型是什么

發(fā)布時(shí)間:2021-11-24 14:50:41 來(lái)源:億速云 閱讀:168 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹C#中的5個(gè)泛型類(lèi)型是什么,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

什么是泛型(廣泛意義上的“泛型”)-->在特定語(yǔ)言(C#)里的泛型-->C#中的5個(gè)泛型類(lèi)型

1、什么是泛型?

2、C#中提供了五種泛型(C#中的泛型)

3、C#中的泛型類(lèi)(Generic Classes )

4、泛型方法Generic Methods 

5、泛型結(jié)構(gòu)Generic Structs

6、泛型委托Generic Delegates 

7、泛型接口Generic Interfaces 

C#中的5個(gè)泛型類(lèi)型是什么

=================

1、什么是泛型

泛型是通過(guò)一種抽象的方式達(dá)到方法的重用?!胺椒ǖ闹赜谩笔欠盒偷哪康?,其手段是進(jìn)行抽象,即將“類(lèi)型”參數(shù)化。

There are times, when a class would be more useful if you could “distill” or “refactor” out its actions and apply them not just to the data types for which they are coded, but for other types as well.

Generics allow you to do just that. You can refactor your code and add an additional layer of abstraction so that, for certain kinds of code, the data types are not hard-coded. This is particularly designed for cases in which there are multiple sections of code performing the same instructions, but on different data types. 

=================

2、C#中提供了五種泛型

泛型可以讓開(kāi)發(fā)人員編寫(xiě)“類(lèi)型參數(shù)化的代碼(type-parameterized code)”,它提供了一個(gè)容器(placeholders for types)用于存放類(lèi)型。

The generics feature offers a more elegant way of using a set of code with more than one type. Generics allow you to declare type-parameterized code, which you can instantiate with different types. This means you can write the code with “placeholders for types” and then supply the actual types when you create an instance of the class. 

C#提供了5種類(lèi)型的泛型:classes, structs, interfaces, delegates和 methods.

C# provides five kinds of generics: classes, structs, interfaces, delegates, and methods. Notice that the first four are types, and methods are members.

=================

3、泛型類(lèi)(Generic Classes )

創(chuàng)建并實(shí)例化泛型類(lèi)的過(guò)程:

Generic Class-->Constructed Type-->instances of the constructed type

As you know, there are two steps for creating and using your own regular, nongeneric classes: declaring the class and creating instances of the class. However, generic classes are not actual classes, but templates for classes—so you must first construct actual class types from them. You can then create references and instances from these constructed class types. 

1.  Declare a class, using placeholders for some of the types. 

2.  Provide actual types to substitute in for the placeholders. This gives you an actual class definition, with all the “blanks” filled in. This is called a constructed type. 

3.  Create instances of the constructed type.

C#中的5個(gè)泛型類(lèi)型是什么

3.1、Declaring a Generic Class (聲明一個(gè)泛型類(lèi))

Declaring a simple generic class is much like declaring a regular class, with the following differences: 

1、在類(lèi)名之后使用“<>”

2、在<>中放置類(lèi)型參數(shù)(type parameters),中間以“,”分隔

3、在泛型類(lèi)內(nèi)部使用類(lèi)型參數(shù)(type parameters)

1. You place a matching set of angle brackets after the class name. 

2. Between the angle brackets, you place a comma-separated list of the placeholder strings that represent the types, to be supplied on demand. These are called type parameters. 

3. You use the type parameters throughout the body of the declaration of the generic class to represent the types that should be substituted in. 

C#中的5個(gè)泛型類(lèi)型是什么

泛型類(lèi)與普通類(lèi)的區(qū)別就是“<>”

There is no special keyword that flags a generic class declaration. Instead, the presence of the type parameter list, demarcated(定…的界線,區(qū)分) with angle brackets, distinguishes a generic class declaration from a regular class declaration.

3.2、Creating a Constructed Type (創(chuàng)建Constructed Type)

Once you have declared the generic type, you need to tell the compiler what actual types should be substituted for the placeholders (the type parameters). The compiler takes those actual types and creates a constructed type, which is a template from which it creates actual class objects. 

type arguments-->type parameters

The real types being substituted for the type parameters are called type arguments.

C#中的5個(gè)泛型類(lèi)型是什么

The compiler takes the type arguments and substitutes them for their corresponding type parameters throughout the body of the generic class, producing the constructed type—from which actual class instances are created.

C#中的5個(gè)泛型類(lèi)型是什么

3.3、Creating Variables and Instances 

A constructed class type is used just like a regular type in creating references and instances.

C#中的5個(gè)泛型類(lèi)型是什么

Many different class types can be constructed from the same generic class. Each one is a separate class type, just as if it had its own separate nongeneric class declaration.

3.4、Comparing the Generic and Nongeneric Stack

C#中的5個(gè)泛型類(lèi)型是什么

3.5、Constraints on Type Parameters

Since the generic stack doesn’t know the type of the items it will be storing, it can’t know what members these types implement.

All C# objects, however, are ultimately derived from class object, so the one thing the stack can be sure of about the items it’s storing is that they implement the members of class object. These include methods ToString, Equals, and GetType. Other than that, it can’t know what members are available. 

To make generics more useful, you need to be able to supply additional information to the compiler about what kinds of types are acceptable as arguments. These additional bits of information are called constraints. Only types that meet the constraints can be substituted for the given type parameter to produce constructed types.

3.5.1、Where Clauses 

Constraints are listed as where clauses. 

  1.  Each type parameter that has constraints has its own where clause. 

  2.  If a parameter has multiple constraints, they are listed in the where clause, separated by commas. 

The syntax of a where clause is the following:

C#中的5個(gè)泛型類(lèi)型是什么

The important points about where clauses are the following: (多個(gè)where clause,注意是復(fù)數(shù)形式)

  1.  They’re listed after the closing angle bracket of the type parameter list. (where clause的位置)

  2.  They’re not separated by commas or any other token. (是否有分隔符)

  3.  They can be listed in any order. (順序不重要)

 The token where is a contextual keyword, so you can use it in other contexts. 

C#中的5個(gè)泛型類(lèi)型是什么

3.5.2、Constraint Types and Order

There are five types of constraints.

C#中的5個(gè)泛型類(lèi)型是什么

The where clauses can be listed in any order. The constraints in a where clause, however, must be placed in a particular order. 

  1.  There can be at most one primary constraint, and if there is one, it must be listed first. 

  2.  There can be any number of InterfaceName constraints. 

  3.  If the constructor constraint is present, it must be listed last.

C#中的5個(gè)泛型類(lèi)型是什么

C#中的5個(gè)泛型類(lèi)型是什么

以上是“C#中的5個(gè)泛型類(lèi)型是什么”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI