ClassName <T ="">的含义和方法

Ben*_*dgi 1 c# generics types

在查看Microsoft 的Tuple类的实现时,我看到了许多我不理解的代码行.

我已经阅读了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 ....

 }
Run Code Online (Sandbox Code Playgroud)

Han*_*ney 5

第三方,非微软网站是错误的.那里的语法既非法又无意义.如果您想查看真实代码,请获取ILSpy等反编译器并自行查看Tuple该类.

Tuple是我从.NET 4.0 Framework反编译它的代码:

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));
    }
}
}
Run Code Online (Sandbox Code Playgroud)