C#中的泛型
簡介:
泛型,就是在定義方法時先不宣告方法的回傳值型別或者引數型別,只是宣告占位符,而是在呼叫方法時才宣告型別,泛型是延遲宣告的:即定義的時候沒有指定具體的引數型別,把引數型別的宣告推遲到了呼叫的時候才指定引數型別, 延遲思想在程式架構設計的時候很受歡迎,
案例:
class GenericMethod { static void Main(string[] args) { int iValue = https://www.cnblogs.com/wml-it/p/123; string sValue = https://www.cnblogs.com/wml-it/p/"123"; DateTime dtValue = DateTime.Now; Console.WriteLine("***********Generic***************"); //呼叫泛型方法 GenericMethod.Show<int>(iValue); GenericMethod.Show<string>(sValue); GenericMethod.Show<DateTime>(dtValue); Console.ReadKey(); } //定義泛型 public static void Show<T>(T tParameter) { Console.WriteLine("This is {0},parameter={1},type={2}", typeof(GenericMethod), tParameter.GetType().Name, tParameter.ToString()); } }
執行結果:
***********Generic*************** This is lamba.GenericMethod,parameter=Int32,type=123 This is lamba.GenericMethod,parameter=String,type=123 This is lamba.GenericMethod,parameter=DateTime,type=2020/6/8 上午 09:39:34
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/20462.html
標籤:C#
