2014-03-05 22 views
1

雖然看到了由Microsoft執行Tuple class,但我看到很多我不明白的代碼行。的含義以及如何使用ClassName <T="">

我已閱讀所有關於MSDN文檔中的泛型,我從來沒有見過T="",並且當我嘗試使用此語法編譯代碼時失敗。我想知道它的目的是什麼,以及如果沒有微軟的個人C#編譯器就可以使用它。我也很好奇這個代碼與t1T1的區別,我可能是錯的,但它看起來並不像是可行的代碼,即使沒有奇怪的=""

在這裏,我只是用其他可能相關的代碼粘貼了一個我的問題的例子。

public static class Tuple 
{ 
    //Other Create(....) Methods 

    public static Tuple<t1, t2=""> Create<t1, t2="">(T1 item1, T2 item2) 
    { 
     return new Tuple<t1, t2="">(item1, item2); 
    } 

    //Other Create(....) Methods 
} 

[Serializable] 
public class Tuple<t1, t2=""> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple 
{ 
    private readonly T1 m_Item1; 
    private readonly T2 m_Item2; 
   
    public T1 Item1 { get { return m_Item1; } } 
    public T2 Item2 { get { return m_Item2; } } 
   
    public Tuple(T1 item1, T2 item2) 
    { 
        m_Item1 = item1; 
        m_Item2 = item2; 
    } 

  //More Methods .... 

 } 
+4

這不是合法的C#代碼。我不知道爲什麼代碼是這樣寫的,但它不合法。 –

+2

編譯器不編譯這樣一個程序的事實告訴你,該語法不是合法的C#。 – Servy

+1

剛剛打破那邊的語法突出顯示器 – hazzik

回答

3

該網站沒有發佈真實的參考資源。如果你想讓實際的參考源轉到一個published by Microsoft。他們最近更新了他們的網站,允許網絡瀏覽dotnetframework.org的源代碼已過時(在您不得不下載400MB安裝程序或使用類似您鏈接的網站之前)。

5

該第三方,非微軟網站是錯誤的。那裏的語法既非法也非荒謬。如果您想查看真實的代碼,請獲取ILSpy等反編譯器,併爲您自己查看Tuple類。

這裏的Tuple代碼,因爲我剛剛從.NET 4.0框架反編譯它:

using System; 
namespace System 
{ 
/// <summary>Provides static methods for creating tuple objects. </summary> 
[__DynamicallyInvokable] 
public static class Tuple 
{ 
    /// <summary>Creates a new 1-tuple, or singleton.</summary> 
    /// <returns>A tuple whose value is (<paramref name="item1" />).</returns> 
    /// <param name="item1">The value of the only component of the tuple.</param> 
    /// <typeparam name="T1">The type of the only component of the tuple.</typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1> Create<T1>(T1 item1) 
    { 
     return new Tuple<T1>(item1); 
    } 
    /// <summary>Creates a new 2-tuple, or pair.</summary> 
    /// <returns>A 2-tuple whose value is (<paramref name="item1" />, <paramref name="item2" />).</returns> 
    /// <param name="item1">The value of the first component of the tuple.</param> 
    /// <param name="item2">The value of the second component of the tuple.</param> 
    /// <typeparam name="T1">The type of the first component of the tuple.</typeparam> 
    /// <typeparam name="T2">The type of the second component of the tuple.</typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2) 
    { 
     return new Tuple<T1, T2>(item1, item2); 
    } 
    /// <summary>Creates a new 3-tuple, or triple.</summary> 
    /// <returns>A 3-tuple whose value is (<paramref name="item1" />, <paramref name="item2" />, <paramref name="item3" />).</returns> 
    /// <param name="item1">The value of the first component of the tuple.</param> 
    /// <param name="item2">The value of the second component of the tuple.</param> 
    /// <param name="item3">The value of the third component of the tuple.</param> 
    /// <typeparam name="T1">The type of the first component of the tuple.</typeparam> 
    /// <typeparam name="T2">The type of the second component of the tuple.</typeparam> 
    /// <typeparam name="T3">The type of the third component of the tuple.</typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3) 
    { 
     return new Tuple<T1, T2, T3>(item1, item2, item3); 
    } 
    /// <summary>Creates a new 4-tuple, or quadruple.</summary> 
    /// <returns>A 4-tuple whose value is (<paramref name="item1" />, <paramref name="item2" />, <paramref name="item3" />, <paramref name="item4" />).</returns> 
    /// <param name="item1">The value of the first component of the tuple.</param> 
    /// <param name="item2">The value of the second component of the tuple.</param> 
    /// <param name="item3">The value of the third component of the tuple.</param> 
    /// <param name="item4">The value of the fourth component of the tuple.</param> 
    /// <typeparam name="T1">The type of the first component of the tuple.</typeparam> 
    /// <typeparam name="T2">The type of the second component of the tuple.</typeparam> 
    /// <typeparam name="T3">The type of the third component of the tuple.</typeparam> 
    /// <typeparam name="T4">The type of the fourth component of the tuple. </typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4) 
    { 
     return new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4); 
    } 
    /// <summary>Creates a new 5-tuple, or quintuple.</summary> 
    /// <returns>A 5-tuple whose value is (<paramref name="item1" />, <paramref name="item2" />, <paramref name="item3" />, <paramref name="item4" />, <paramref name="item5" />).</returns> 
    /// <param name="item1">The value of the first component of the tuple.</param> 
    /// <param name="item2">The value of the second component of the tuple.</param> 
    /// <param name="item3">The value of the third component of the tuple.</param> 
    /// <param name="item4">The value of the fourth component of the tuple.</param> 
    /// <param name="item5">The value of the fifth component of the tuple.</param> 
    /// <typeparam name="T1">The type of the first component of the tuple.</typeparam> 
    /// <typeparam name="T2">The type of the second component of the tuple.</typeparam> 
    /// <typeparam name="T3">The type of the third component of the tuple.</typeparam> 
    /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam> 
    /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5) 
    { 
     return new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5); 
    } 
    /// <summary>Creates a new 6-tuple, or sextuple.</summary> 
    /// <returns>A 6-tuple whose value is (<paramref name="item1" />, <paramref name="item2" />, <paramref name="item3" />, <paramref name="item4" />, <paramref name="item5" />, <paramref name="item6" />).</returns> 
    /// <param name="item1">The value of the first component of the tuple.</param> 
    /// <param name="item2">The value of the second component of the tuple.</param> 
    /// <param name="item3">The value of the third component of the tuple.</param> 
    /// <param name="item4">The value of the fourth component of the tuple.</param> 
    /// <param name="item5">The value of the fifth component of the tuple.</param> 
    /// <param name="item6">The value of the sixth component of the tuple.</param> 
    /// <typeparam name="T1">The type of the first component of the tuple.</typeparam> 
    /// <typeparam name="T2">The type of the second component of the tuple.</typeparam> 
    /// <typeparam name="T3">The type of the third component of the tuple.</typeparam> 
    /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam> 
    /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam> 
    /// <typeparam name="T6">The type of the sixth component of the tuple.</typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6) 
    { 
     return new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6); 
    } 
    /// <summary>Creates a new 7-tuple, or septuple.</summary> 
    /// <returns>A 7-tuple whose value is (<paramref name="item1" />, <paramref name="item2" />, <paramref name="item3" />, <paramref name="item4" />, <paramref name="item5" />, <paramref name="item6" />, <paramref name="item7" />).</returns> 
    /// <param name="item1">The value of the first component of the tuple.</param> 
    /// <param name="item2">The value of the second component of the tuple.</param> 
    /// <param name="item3">The value of the third component of the tuple.</param> 
    /// <param name="item4">The value of the fourth component of the tuple.</param> 
    /// <param name="item5">The value of the fifth component of the tuple.</param> 
    /// <param name="item6">The value of the sixth component of the tuple.</param> 
    /// <param name="item7">The value of the seventh component of the tuple.</param> 
    /// <typeparam name="T1">The type of the first component of the tuple.</typeparam> 
    /// <typeparam name="T2">The type of the second component of the tuple.</typeparam> 
    /// <typeparam name="T3">The type of the third component of the tuple.</typeparam> 
    /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam> 
    /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam> 
    /// <typeparam name="T6">The type of the sixth component of the tuple.</typeparam> 
    /// <typeparam name="T7">The type of the seventh component of the tuple.</typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7) 
    { 
     return new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7); 
    } 
    /// <summary>Creates a new 8-tuple, or octuple.</summary> 
    /// <returns>An 8-tuple (octuple) whose value is (<paramref name="item1" />, <paramref name="item2" />, <paramref name="item3" />, <paramref name="item4" />, <paramref name="item5" />, <paramref name="item6" />, <paramref name="item7" />, <paramref name="item8" />). </returns> 
    /// <param name="item1">The value of the first component of the tuple.</param> 
    /// <param name="item2">The value of the second component of the tuple.</param> 
    /// <param name="item3">The value of the third component of the tuple.</param> 
    /// <param name="item4">The value of the fourth component of the tuple.</param> 
    /// <param name="item5">The value of the fifth component of the tuple.</param> 
    /// <param name="item6">The value of the sixth component of the tuple.</param> 
    /// <param name="item7">The value of the seventh component of the tuple.</param> 
    /// <param name="item8">The value of the eighth component of the tuple.</param> 
    /// <typeparam name="T1">The type of the first component of the tuple.</typeparam> 
    /// <typeparam name="T2">The type of the second component of the tuple.</typeparam> 
    /// <typeparam name="T3">The type of the third component of the tuple.</typeparam> 
    /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam> 
    /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam> 
    /// <typeparam name="T6">The type of the sixth component of the tuple.</typeparam> 
    /// <typeparam name="T7">The type of the seventh component of the tuple.</typeparam> 
    /// <typeparam name="T8">The type of the eighth component of the tuple.</typeparam> 
    [__DynamicallyInvokable] 
    public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>> Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, T8 item8) 
    { 
     return new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>(item1, item2, item3, item4, item5, item6, item7, new Tuple<T8>(item8)); 
    } 
    internal static int CombineHashCodes(int h1, int h2) 
    { 
     return (h1 << 5) + h1^h2; 
    } 
    internal static int CombineHashCodes(int h1, int h2, int h3) 
    { 
     return Tuple.CombineHashCodes(Tuple.CombineHashCodes(h1, h2), h3); 
    } 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4) 
    { 
     return Tuple.CombineHashCodes(Tuple.CombineHashCodes(h1, h2), Tuple.CombineHashCodes(h3, h4)); 
    } 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5) 
    { 
     return Tuple.CombineHashCodes(Tuple.CombineHashCodes(h1, h2, h3, h4), h5); 
    } 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6) 
    { 
     return Tuple.CombineHashCodes(Tuple.CombineHashCodes(h1, h2, h3, h4), Tuple.CombineHashCodes(h5, h6)); 
    } 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7) 
    { 
     return Tuple.CombineHashCodes(Tuple.CombineHashCodes(h1, h2, h3, h4), Tuple.CombineHashCodes(h5, h6, h7)); 
    } 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8) 
    { 
     return Tuple.CombineHashCodes(Tuple.CombineHashCodes(h1, h2, h3, h4), Tuple.CombineHashCodes(h5, h6, h7, h8)); 
    } 
} 
} 
+1

[參考源網站](http://referencesource.microsoft.com/)在一兩個月前已更新,允許網頁瀏覽源代碼,您不需要使用反編譯器(對於4.5.1,舊版本您仍然需要下載它或使用反編譯器)。 –

+0

@ScottChamberlain真棒 - 我不知道。謝謝! – Haney

2

這不是合法的C#。

很可能有人爲源代碼編寫了一個「幻想」瀏覽器,該源代碼運行一些JavaScript或源代碼的內容來生成該代碼。

如果您檢查有問題的網頁的源代碼,您會注意到那裏的代碼沒有那種奇怪的語法,所以在瀏覽器中添加了一些東西。

這不是合法的C#。

這並不意味着什麼。

C#參考源不包含這些字符。

4

Here's the real source code

public static class Tuple { 
    public static Tuple<T1> Create<T1>(T1 item1) { 
     return new Tuple<T1>(item1); 
    } 

    public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2) { 
     return new Tuple<T1, T2>(item1, item2); 
    } 

... 

[Serializable] 
public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple { 

    private readonly T1 m_Item1; 
    private readonly T2 m_Item2; 

    public T1 Item1 { get { return m_Item1; } } 
    public T2 Item2 { get { return m_Item2; } } 

    public Tuple(T1 item1, T2 item2) { 
     m_Item1 = item1; 
     m_Item2 = item2; 
    } 
相關問題