当前位置:网站首页>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
边栏推荐
- 利用栈的回溯来解决“文件的最长绝对路径”问题
- A set of C interview questions about memory alignment. Many people make mistakes!
- Use of MySQL command line client and common commands
- How to deploy a website with only a server and no domain name?
- Niuke white moon race 6 [solution]
- ASP.NET 6 中间件系列 - 自定义中间件类
- MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
- Q-Learning & Sarsa
- [software testing] understand the basic knowledge of software testing
- 微软是如何解决 PC 端程序多开问题的——内部实现
猜你喜欢
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
Response processing of openfeign
LNMP MySQL allows remote access
C#中切片语法糖的使用
Laravel new route file
AC & A2C & A3C
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Blazor University (11)组件 — 替换子组件的属性
Small companies don't make formal offers
随机推荐
使用split来解决“最常见的单词”问题
Using stack to solve the problem of "mini parser"
C# 11 对 ref 和 struct 的改进
Maui initial experience: Cool
AOT和单文件发布对程序性能的影响
Service avalanche effect
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
How to count the number of all files in a directory under win10 system
TP5 customization in extend directory succeeded and failed. Return information
REINFORCE
Miniapi of. Net7 (special section): NET7 Preview3
Numpy stack function
A set of C interview questions about memory alignment. Many people make mistakes!
How to use C language to realize [guessing numbers game]
c#语法糖模式匹配【switch 表达式】
ASP.NET 6 中间件系列 - 执行顺序
Use split to solve the "most common words" problem
中后二叉建树
对.NET未来的一点感悟