当前位置:首页 » 《随便一记》 » 正文

c# ValueTuple

4 人参与  2022年11月07日 15:21  分类 : 《随便一记》  评论

点击全文阅读


为什么我们需要 ValueTuple?

Tuple 是引用类型,但 ValueTuple 是值类型。Tuple 不提供命名约定,但 ValueTuple 提供了强大的命名约定。在 Tuples 中,您不能创建零分量的元组,但在 ValueTuple 中,您可以创建零元素的元组。ValueTuple 的性能优于 Tuple。 因为 ValueTuple 提供了一种轻量级的机制,用于从现有方法返回多个值。 并且 ValueTuple 的语法比 Tuples 更优化。ValueTuple 通过使用解构和 _ 关键字为访问值元组的元素提供了更大的灵活性。 但是 Tuple 不能提供解构的概念和 _ 关键字。在 ValueTuple 中,item1 和 item2 等成员是字段。 但在元组中,它们是属性。在 ValueTuple 中,字段是可变的。 但是在元组中,字段是只读的。

Creating a ValueTuple

Using Constructor

// Constructor for creating one element
ValueTuple(T1)
// Constructor for creating two elements
ValueTuple<T1, T2>(T1, T2)
// Constructor for creating eight elements
ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) /


Sample
using System; class GFG {     // Main method    static public void Main()    {         // ValueTuple with one element        ValueTuple<int> ValTpl1 = new ValueTuple<int>(345678);         // ValueTuple with three elements        ValueTuple<string, string, int> ValTpl2 = new ValueTuple<string,                                        string, int>("C#", "Java", 586);         // ValueTuple with eight elements        ValueTuple<int, int, int, int, int, int, int, ValueTuple<int> > ValTpl3 = new ValueTuple<int,                                  int, int, int, int, int, int, ValueTuple<int> >(45, 67, 65, 34, 34,                                                                    34, 23, new ValueTuple<int>(90));    }}

Using Create Method

// Method for creating an empty value tuple
Create();
// Method for creating 1-ValueTuple
Create(T1)
// Method for creating 8-ValueTuple
Create<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, T8)


Sample

using System; public class GFG {     // Main method    static public void Main()    {         // Creating 0-ValueTuple        // Using Create() Method        var Valtpl1 = ValueTuple.Create();         // Creating 3-ValueTuple        // Using Create(T1, T2, T3) Method        var Valtpl2 = ValueTuple.Create(12, 30, 40, 50);         // Creating 8-ValueTuple        // Using Create(T1, T2, T3, T4, T5, T6, T7, T8) Method        var Valtpl3 = ValueTuple.Create(34, "GeeksforGeeks",                      'g', 'f', 'g', 56.78, 4323, "geeks");    }}
Using parenthesis()

named member

using System; public class GFG {    static public void Main()    {        (int age, string Aname, string Lang) author = (23, "Sonia", "C#");    }}
using System; public class GFG {     static public void Main()    {        var author = (age : 23, Aname                      : "Sonia", Lang                      : "C#");    }}

UnNamed Member

using System; public class GFG {    static public void Main()    {        var author = (20, "Siya", "Ruby");    }}
using System; public class GFG {    static public void Main()    {        ValueTuple<int, string, string> author = (20, "Siya", "Ruby");    }}

Accessing ValueTuple members

Accessing unnamed members
using System; public class GFG {     // Main Method    static public void Main()    {         // ValueTuple with three elements        var author = (20, "Siya", "Ruby");         // Accessing the ValueTuple        // Using default Item property        Console.WriteLine("Age:" + author.Item1);        Console.WriteLine("Name:" + author.Item2);        Console.WriteLine("Language:" + author.Item3);    }}
Accessing Named members:
using System; public class GFG {     // Main Method    static public void Main()    {         // ValueTuple with three elements        var library = (Book_id : 2340, Author_name                       : "Arundhati Roy", Book_name                       : "The God of Small Things");         // Accessing the ValueTuple        // according to their names        Console.WriteLine("Book Id: {0}", library.Book_id);        Console.WriteLine("Author Name: {0}", library.Author_name);        Console.WriteLine("Book Name: {0}", library.Book_name);    }}

Returning ValueTuple

using System; public class GFG {     // This method returns the tourist details    static(int, string, string) TouristDetails()    {        return (384645, "Sophite", "USA");    }     // Main method    static public void Main()    {         // Store the data provided by the TouristDetails method        var(Tourist_Id, Tourist_Name, Country) = TouristDetails();         // Display data        Console.WriteLine("Tourist Details: ");        Console.WriteLine($ "Tourist Id: {Tourist_Id}");        Console.WriteLine($ "Tourist Name: {Tourist_Name}");        Console.WriteLine($ "Country: {Country}");    }}

点击全文阅读


本文链接:http://zhangshiyu.com/post/47279.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 《笨蛋美人重生八零,硬汉军官狂宠》免费阅读_江晚瑜沈裴竣小说免费试读
  • 《笨蛋美人重生八零,硬汉军官狂宠》江晚瑜沈裴竣章节目录免费阅读
  • 她的记忆停留在了最爱初恋的那年许欣柔楚临川完本_她的记忆停留在了最爱初恋的那年(许欣柔楚临川)
  • 全书浏览假千金的实习生男友霸占我办公室,我反手让他们倾家荡产(顾家明)_假千金的实习生男友霸占我办公室,我反手让他们倾家荡产(顾家明)全书结局
  • 童养夫让我给他的新欢出修复费(林嘉芝林思雅)_童养夫让我给他的新欢出修复费林嘉芝林思雅
  • 全文资助生女婿让我给他白月光付三千万月子中心钱(宋清玉宋雅)列表_全文资助生女婿让我给他白月光付三千万月子中心钱
  • 碎在时光里的谎言喻景宴秦明月完本_碎在时光里的谎言(喻景宴秦明月)
  • 旧爱剜心吻成灰席鄢之岑秋全书免费旧爱剜心吻成灰席鄢之岑秋全书免费
  • 结婚六年丈夫不碰我谁知儿子亲爹是寡头(纪清言傅司砚),结婚六年丈夫不碰我谁知儿子亲爹是寡头
  • 老公想换掉我的男胎,我笑他自不量力(宋薇于继业)_老公想换掉我的男胎,我笑他自不量力宋薇于继业
  • 给太子下了噬心蛊后,皇后找上门(小夭赵劼)全书浏览_给太子下了噬心蛊后,皇后找上门全书浏览
  • 豪门绝嗣!带球跑的夫人回来了!(谢长宴慕清杳)_豪门绝嗣!带球跑的夫人回来了!谢长宴慕清杳

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1