zoukankan      html  css  js  c++  java
  • .Net 4.0特性 Tuple元组

    Tuple 字面意思:元组。是.net4.0增加的新特性,是干什么的呢?总结一句,个人觉得这个东西 就是用来在有返回很多种类型的值时可以用到。它提供了8种类型的Tuple,直接看最复杂的那种(其实不是复杂,就是参数比别人多而已,原理是完全一致的),原理不讲太多,看下这个类的结构就知道什么原理了。

    [Serializable, __DynamicallyInvokable]
    public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
    {
        // Fields
        private readonly T1 m_Item1;
        private readonly T2 m_Item2;
        private readonly T3 m_Item3;
        private readonly T4 m_Item4;
        private readonly T5 m_Item5;
        private readonly T6 m_Item6;
        private readonly T7 m_Item7;
        private readonly TRest m_Rest;
    
        // Methodspublic Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest);
        public override bool Equals(object obj);
        public override int GetHashCode();
        int IStructuralComparable.CompareTo(object other, IComparer comparer);
        bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer);
        int IStructuralEquatable.GetHashCode(IEqualityComparer comparer);
        int IComparable.CompareTo(object obj);
        int ITuple.GetHashCode(IEqualityComparer comparer);
        string ITuple.ToString(StringBuilder sb);
        public override string ToString();
    
        // Properties
        [__DynamicallyInvokable]
        public T1 Item1 { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; }
       此处省略:T2到T6的定义,因为完全一致,为了减少文章篇幅。
        [__DynamicallyInvokable]
        public T7 Item7 { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; }
        [__DynamicallyInvokable]
        public TRest Rest { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; }//注意这个最后一个参数的类型,其实它是Tupple类型的
        int ITuple.Size { get; }
    }
    
     
    Tuple<int, string, bool, char, ClassStyle, Dictionary<int,string>, IEnumerable<int> , TRest> tuple=new
    Tuple(1,"string",true,'1',new Student(),.....,Tuple<任意类型>);
    用法就是在函数返回的时候,可以new一个这样的类型的对象,对其各个属性赋值,这样就可以返回非常多类型的数据,取得返回的值,就是用这个tuple对象.Item1,就可以获得相应类型的值,比如在这里我们tuple.Item1的值是1,tuple.Item2的值是“string”,以此类推。当然这些Item是只读的,不能对其进行赋值操作。为我们提供了很大的方便。以上是创建Tuple的方法之一,接下来说下另一个方法,先看下这个Tuple静态类:
    [__DynamicallyInvokable]
    public static class Tuple
    {
        // Methods
        internal static int CombineHashCodes(int h1, int h2);internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8);
        [__DynamicallyInvokable]
        public static Tuple<T1> Create<T1>(T1 item1);
    此处省略:T2到T6的定义,因为完全一致,为了减少文章篇幅。
    [__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); }

    其实代码更多,当然区别也是参数个数不一致,原理还是一样的,这里只写出两个。

    看一眼就知道Create<....>这个方法,没错我们也可以通过Tuple这个静态类中的Create静态方法来创建不定个数参数的Tuple对象,举个例子:

    var trest = Tuple.Create<int>(8);
    var t8 = Tuple.Create<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, trest);

    第八个参数 一定是要Tuple类型的,当然只有这个需要8个参数的Tuple类型最后一个参数需要Tuple类型,而其他几个(这里指1~7个参数的Tuppe)都是任意类型都可以。可能是为了用于扩展更多的参数,因为Tuple毕竟只是提供了最多8种不同参数的构造方法。其实这个东西就是为了方便我们可以返回多个不同类型的值。

    
    
  • 相关阅读:
    实验0 了解和熟悉操作系统
    软件工程感想
    递归下降分析法--算数语法分析
    有限自动机的构造与识别
    评论
    文法解释
    【编译CEF3】2017-07 添加支持mp3 mp4的编译日记
    Visual Studio 2015/2013安装失败:Microsoft Visual Studio 2015 Shell (Minimum) Interop Assemblies 安装时发生严重错误
    [RAD Studio 10.2 Tokyo] Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major错误解决
    Delphi 编译出来的程序被小红伞报病毒 TR/Spy.Banker.Gen4 [trojan]
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/4108409.html
Copyright © 2011-2022 走看看