2
A
回答
6
之間的差異比較:
class First
{
public T Get<T>(int id) // T is declared in the method scope
{
}
}
和
class Second<T>
{
public T Get(int id) // T is declared in the class scope
{
}
}
也有一個第三方案:
class Third<U>
{
public T Get<T>(int id) // T is declared in the method scope when class scope has another generic argument declared
{
}
}
+1
+1,讓我補充一點,這兩種情況之間的混淆是導致如下問題的原因:http://stackoverflow.com/questions/6740978/type-parameter-t-has-the-same-name -as-the-type-parameter-from-outer-type –
+1
不要忘記存在稱爲'T'的類型的情況(不要這樣做!) –
0
閱讀Generics之前,你可以在代碼中使用它們。
3
不同之處在於,如果這是在T之前尚未定義的情況下,則使用第一種類型的聲明。即。
public class MyClass
{
public T Get<T>(int id);
}
而第二當T已經在類級定義的。即。
public class MyClass<T>
{
public T Get(int id);
}
在這種情況下,您也可以使用第一種類型的聲明 - 這是有效的速記。效果沒有區別。
編輯事實上,第二個聲明只要求T是在範圍,另一個例子是一個嵌套類如...
public class MyClass<T>
{
public class NestedClass
{
public T Get(int i);
}
}
相關問題
- 1. NHibernate的差異,獲取<T>和負載<T>
- 2. Ninject獲取<T> WhenTargetHas <T>
- 3. 的ArgumentException從Array.BinarySearch <T>(T [],INT,INT,T)
- 4. 獲取Nonetype()<= INT()
- 5. 從IEnumerable獲取一個清晰的ID列表<T>
- 6. 獲取從列表中<T>元素根據ID
- 7. 包含std :: function <T()>和std :: function <T(int)>
- 8. 獲取哈希碼的int和可空<int>對
- 9. 從單參數獲取Func <T>和表達式<Func<T>>
- 10. 如何從SortedList <int, T>創建列表<T>?
- 11. 如何獲取類VfNetCallback <T>
- 12. 獲取<tr> ID
- 13. 我如何反映類<T>的列表<T>並獲取T?
- 14. 獲取INT收集
- 15. 列表<T>獲取塊號碼
- 16. 獲取從列表中<T>
- 17. 從函數獲取屬性<T>
- 18. 獲取MethodInfo for ICollection <T>
- 19. 獲取PropertyInfo.GetCustomAttribute的例外<T>
- 20. 獲取任務(未任務<T>)
- 21. .net System.Collections.Generic.List <T> .sort nullable int
- 22. Int * t;和這個t [* t] - 類型定義
- 23. 獲取T的泛型列表<T>對象
- 24. 從功能獲取屬性名稱<t, T>
- 25. 在字典中獲取int值的總和<string,列表<int>>()
- 26. cast bool to T其中T是int
- 27. 獲取實體的int ID在GAE在GO
- 28. Yesod:通過ID從Int獲取數據庫實體
- 29. 獲取id字段作爲記錄在與QSqlTableModel一個int
- 30. 獲取UnsafePointer <UInt8>爲Int將編碼的Int寫入流
@davenewza:您的評論是正確的兩種情況下,實際上是通用的螞蟻仿製藥。像「這裏T是什麼意思?」或「什麼是泛型?」 ;) – abatishchev