在泛型類型或方法定義中,類型參數(shù)是客戶端在實(shí)例化泛型類型的變量時(shí)指定的特定類型的占位符。泛型類(如泛型介紹(C# 編程指南)中列出的 GenericList<T>)不可以像這樣使用,因?yàn)樗鼘?shí)際上并不是一個(gè)類型,而更像是一個(gè)類型的藍(lán)圖。若要使用 GenericList<T>,客戶端代碼必須通過(guò)指定尖括號(hào)中的類型參數(shù)來(lái)聲明和實(shí)例化構(gòu)造類型。此特定類的類型參數(shù)可以是編譯器識(shí)別的任何類型。可以創(chuàng)建任意數(shù)目的構(gòu)造類型實(shí)例,每個(gè)實(shí)例使用不同的類型參數(shù),如下所示:
GenericList<ExampleClass> list2 = new GenericList<ExampleClass>();
GenericList<ExampleStruct> list3 = new GenericList<ExampleStruct>();
在每個(gè) GenericList
public delegate TOutput Converter<TInput, TOutput>(TInput from);
public class List<T> { /*...*/ }
考慮使用 T 作為具有單個(gè)字母類型參數(shù)的類型的類型參數(shù)名。
public delegate bool Predicate<T>(T item);
public struct Nullable<T> where T : struct { /*...*/ }
務(wù)必將“T”作為描述性類型參數(shù)名的前綴。
考慮在參數(shù)名中指示對(duì)此類型參數(shù)的約束。例如,可以將帶有 ISession約束的參數(shù)命名為TSession
新聞熱點(diǎn)
疑難解答
圖片精選