[英]Pass a type parameter and declare a list
public static List<customModel> getList(type customModel)
{
List<customModel> tempList = new List<customModel>(10);
return tempList;
}
是否可以返回從其他地方傳遞的類型的列表? 我一直在做自己的項目,請注意,如果有任何方法可以執行此操作,則我的代碼將更加簡單。
您是說使用像這樣的泛型 :
public static List<T> getList<T>()
{
List<T> tempList = new List<T>(10);
return tempList;
}
您可以這樣稱呼:
var newList = getList<customModel>();
或C#3.0之前的版本(其中var
不可用):
List<customModel> newList = getList<customModel>();
問題是,您可以輕松地這樣做:
var newList = new List<customModel>(10);
使用通用:
public static List<T> getList<T>()
{
return new List<T>(10);
}
這樣稱呼它:
var myList = getList<int>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.