码迷,mamicode.com
首页 > Windows程序 > 详细

c# tuple还是valueTuple?

时间:2018-11-07 23:05:18      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:使用   关于   .net   model   void   val   bubuko   构造函数   忽略   

 

    自从c#7.0发布之后,其中一个新特性是关于tuple的更新,使得tuple在编码中更受欢迎

    先总结一下tuple的常规用法

一、回味Tuple

    Tuple的美式发音为  /?tj?p?l; ?t?p?l (方便在讨论的时候准确发音,我是闹过笑话)

     Tuple是C# 4.0时出的一个新特性,因此.Net Framework 4.0以上版本可用。

     1、创建Tuple 

      有两种方式可以创建tuple元祖对象,如下所示

           //1.使用tuple的静态方法创建tuple
            {
                //最多支持9个元素
                             var tuple = Tuple.Create<int, int, int, int>(1,2,3,4);

            }
            //2.使用构造函数构造tuple
            {
                var tuple = new Tuple<int>(1);
                          }

      2、使用tuple

       static void Main(string[] args)
        {
            //1.使用tuple存入相关信息,替代model对象,标识一组信息
            {
                var tuple = new Tuple<int, string, string>(1, "张山", "1班");
            }
            //2.作为方法的返回值
            {
                var tuple = GetTuple();
            }
        }

        static Tuple<int, int> GetTuple()
        {
            Tuple<int, int> tuple = new Tuple<int, int>(1, 2);
            return tuple;
        }

      3、tuple取值

         static void Main(string[] args)
          {
            //1.使用tuple存入相关信息,替代model对象,标识一组信息
            {
                var tuple = new Tuple<int, string, string>(1, "张山", "1班");
                int id = tuple.Item1;
                string name = tuple.Item2;
            }
            //2.作为方法的返回值
            {
                var tuple = GetTuple();
                var id = tuple.Item1;
            }
           }

        static Tuple<int, int> GetTuple()
        {
            Tuple<int, int> tuple = new Tuple<int, int>(1, 2);
            return tuple;
        }

     以上就是tuple使用三部曲

     下面在看下c#7.0关于tuple的新特性

    二、ValueTuple

     1、先从nuget包中下载

技术分享图片

     2、.使用新特性

     static void Main(string[] args)
        {
            //1.新特性使用
            {
                var valueTuple = GetValueTuple();
                int id = valueTuple.id;
                string name = valueTuple.name;
                string className = valueTuple.className;

                //可以对返回参数进行解构,解析成更明确的表达
                var (id1,name1,className1)= GetValueTuple();
                int id2 = id1;

                //忽略自己不必使用的值
                var (id3, _, _) = GetValueTuple();

            }

            //2.常规tuple使用
            {
                var tuple = GetTuple();
                int id = tuple.Item1;
                string name = tuple.Item2;
                string className = tuple.Item3;
            }
           
        }

        static (int id, string name, string className) GetValueTuple()
        {

            return (1, "张三", "一班");
        }

        static Tuple<int, string, string> GetTuple()
        {
            Tuple<int, string, string> tuple = new Tuple<int, string, string>(1, "张三", "一班");
            return tuple;
        }

 

    总结

     1、ValueTuple的返回值写法更简洁,且方法返回声明更明确,

           因此方法调用方能够明确获取方法返回值定义的参数名

      2、ValueTuple可对返回参数进行解构

            升级到了 .NET Framework 4.6.2就可以使用c# 7.0的语法了,也就可以使用ValueTuple

 

c# tuple还是valueTuple?

标签:使用   关于   .net   model   void   val   bubuko   构造函数   忽略   

原文地址:https://www.cnblogs.com/xxue/p/9926182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!