2012-02-15 16 views
0

如何查看ForEach<T>(T[] array, Action<T> action)內的代碼?Array.ForEach in C#

不是定義只有或如何使用,但這個方法裏面的代碼?

我只需要看看如何改變參數類型,當我把T作爲一種傳遞的方法。

實施例:

Array.ForEach<string>(string[] array, Action<string> action); 

Array.ForEach<int>(int[] array, Action<int> action); 

Array.ForEach<whateverTypeHere>(whateverTypeHere[] array, Action<whateverTypeHere> action); 

如果我把任何類型的內部<>的參數也將發生變化。怎麼做呢? :)

+7

這是所有關於[泛型](http://msdn.microsoft.com/en-us/library/ms379564(V = VS。 80).aspx)這裏。 – ken2k 2012-02-15 09:08:58

+0

我建議你閱讀泛型,因爲像@ ken2k所說的全是關於它們的! 基本上,編譯器爲它在代碼中找到的每個泛型類型創建一個類。 除了出於好奇之外,你不需要知道.net方法的內部細節。 – 2012-02-15 09:18:59

+0

謝謝@ ken2k .. – 2012-02-15 09:21:59

回答

3
public class Test<TFirst,TSecond> 
{ 
    public TFirst First;public TSecond Second; 
} 

如果我創建這個類的一個新實例,它會顯示類似

Test<int,string> objName=new Test<int,string>{ First=1, Second="Microsoft"}; 

如果你通過如int或字符串類型,自動編譯器會推斷出你的財產的類型。

你必須嘗試練習,那麼你就能理解