当前位置:网站首页>Use of metagroup object tuple in C
Use of metagroup object tuple in C
2022-04-23 03:06:00 【Dotnet cross platform】
brief introduction
Tuples use generics to simplify the definition of classes , Mostly used for the return value of the method . When a function needs to return multiple types , You don't have to use out , ref Wait for keywords , Define a Tuple type , It is very convenient to use
Example
When we program , For example, a person's information , We often create a UserInformation Class to describe a person , The traditional approach is as follows :
public class UserInformation
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
static UserInformation GetUserInformation()
{
return new UserInformation { FirstName = " Zhang San ", LastName = " Li Si " };
}
So what can we do with tuples ? As shown below
static Tuple<string, string> GetUserInformation3()
{
return new Tuple<string,string> ( " Zhang San ", " Li Si " );
}
var tuple = GetUserInformation1();
string firstName= tuple.Item1;
string lastName = tuple.Item2;
Item1 Represents the first attribute value ,Item2 Represents the second attribute value , Of course if you use Item1 and item2 Poor readability , Don't look at the method, don't know at all Item1 What does it mean , Then we can use anonymous types , As shown below :
var ( first, last) = GetUserInformation1();
first Is our first attribute value ,last Is our second attribute value . stay C#7.0 It provides a more concise Syntax , It is used to divide multiple data elements into a lightweight data structure
static (string, string) GetUserInformation1()
{
return (" Zhang San ", " Li Si ");
}
Of course, it also supports anonymous types to obtain attribute values
var(first,last)= GetUserInformation1();
To learn more tuple types, go to :https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/value-tuples
Finally, if you like my article , Please pay attention and praise , hope net The ecosystem is getting better and better !
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301333672.html
边栏推荐
- 树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
- Summary of interface automation interview questions for software testing
- Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- Numpy stack function
- c#可变参数params的介绍
- Cherno_ Game engine series tutorial (5): 101~
- 腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
- 利用栈的回溯来解决“文件的最长绝对路径”问题
- Array and collection types passed by openfeign parameters
猜你喜欢
![Niuke white moon race 5 [problem solving mathematics field]](/img/be/ca059bd1c84eaaaefa3266f9119a6b.png)
Niuke white moon race 5 [problem solving mathematics field]

Ningde's position in the times is not guaranteed?

Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
![How to use C language to realize [guessing numbers game]](/img/8c/052dcb0ce64ee1713bebb1340248e6.png)
How to use C language to realize [guessing numbers game]

再战leetcode (290.单词规律)

Array and collection types passed by openfeign parameters

TP5 inherits base and uses the variables in base

Binary tree

How does Microsoft solve the problem of multiple PC programs

Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
随机推荐
How to use C language to realize [guessing numbers game]
Two methods are used to solve the "maximum palindrome product" problem
Recursion - outputs continuously increasing numbers
Service avalanche effect
Source generator actual combat
AOT和单文件发布对程序性能的影响
一套关于 内存对齐 的C#面试题,做错的人很多!
Laravel8- use JWT
MYSQL_ From mastery to abandonment
Niuke white moon race 5 [problem solving mathematics field]
Numpy append function
Detailed explanation of distributed things
Opencv reads webcam video and saves it locally
中后二叉建树
Using stack to solve the problem of "mini parser"
c#可变参数params的介绍
編碼電機PID調試(速度環|比特置環|跟隨)
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
最通俗易懂的依赖注入之服务容器与作用域
Er and eer models