ClassName的含义以及如何使用ClassName <t =“”> </t =“”>

时间:2014-03-05 20:55:12

标签: c# generics types

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

我已经在MSDN文档中阅读了所有关于泛型的内容,我从未见过T="",当我尝试使用这种语法编译代码时,它失败了。我想知道它的目的是什么,如果没有微软的个人C#编译器就可以使用它。我也很好奇这段代码与t1T1的区别,我可能错了,但即使没有奇怪的="",它看起来也不是可行代码。< / p>

在这里,我只用其他可能相关的代码粘贴了我的问题的一个例子。

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 个答案:

答案 0 :(得分:5)

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

这是我在.NET 4.0 Framework中反编译的Tuple代码:

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 :(得分: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;
    }

答案 2 :(得分:3)

该网站未发布真实的参考资料。如果您想要实际的参考源,请转到published by Microsoft。他们最近更新了他们的网站,允许网站浏览来源使dotnetframework.org过时(在您下载400MB安装程序或使用类似您链接的网站之前)。

答案 3 :(得分:2)

这不是合法的C#。

很可能有人为源代码编写了一个“花哨”的浏览器,在源代码上运行一些javascript或诸如此类的东西来生成它。

如果您检查相关网页的源代码,您会注意到那里的代码没有那种奇怪的语法,所以有些东西在浏览器中添加。

这不合法C#。

这并不意味着什么。

C#参考源不包含这些字符。