当前位置:网站首页>C#中元组对象Tuple的使用
C#中元组对象Tuple的使用
2022-04-23 03:02:00 【dotNET跨平台】
简介
元组使用泛型来简化类的定义,多用于方法的返回值。在函数需要返回多个类型的时候,就不必使用out , ref等关键字了,直接定义一个Tuple类型,使用起来非常方便
例子
在我们编程时,比如一个人的信息,我们常常创建一个UserInformation类去描述一个人,传统的做法如下:
public class UserInformation
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
static UserInformation GetUserInformation()
{
return new UserInformation { FirstName = "张三", LastName = "李四" };
}
那么我们使用元组可以怎么做呢?如下所示
static Tuple<string, string> GetUserInformation3()
{
return new Tuple<string,string> ( "张三", "李四" );
}
var tuple = GetUserInformation1();
string firstName= tuple.Item1;
string lastName = tuple.Item2;
Item1代表第一个属性值,Item2代表第二个属性值,当然如果使用Item1和item2可读性比较差,不看方法根本不知道Item1代表什么意思,这时候我们就可以使用匿名类型了,如下所示:
var ( first, last) = GetUserInformation1();
first是我们的第一个属性值,last是我们的第二个属性值。在C#7.0中它提供了更简洁的语法,用于将多个数据元素分组成一个轻型数据结构
static (string, string) GetUserInformation1()
{
return ("张三", "李四");
}
当然也是支持匿名类型获取属性值的
var(first,last)= GetUserInformation1();
学习更多元组类型请前往:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/value-tuples
最后大家如果喜欢我的文章,还麻烦给个关注并点个赞, 希望net生态圈越来越好!
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124310720
边栏推荐
- Introduction to ACM [TSP problem]
- Encapsulate components such as pull-down menu based on ele
- Numpy stack function
- Kubernetes - Introduction to actual combat
- Niuke white moon race 6 [solution]
- [if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
- Difference between relative path and absolute path (often asked in interview)
- Er and eer models
- Binary tree
- Shell script learning -- practical case
猜你喜欢

How to write the expected salary on your resume to double your salary during the interview?

C# WPF UI框架MahApps切换主题

Kubernetes study notes

Android 高阶面试必问:全局业务和项目的架构设计与重构

Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet

Sonic cloud real machine tutorial

PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

Processes and threads

Q-Learning & Sarsa
随机推荐
C# WPF UI框架MahApps切换主题
Summary of software test interview questions
c#可变参数params的介绍
What is the difference between varchar and char?
Winsock programming interface experiment: implementation of ipconfig
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
HLS / chisel practice CORDIC high performance computing complex square root
MySQL complex query uses temporary table / with as (similar to table variable)
JS learning notes
Shell script learning notes - regular expressions
C# 11 对 ref 和 struct 的改进
Huawei machine test question -- deformation of hj53 Yang Hui triangle
Close the computer port
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Log cutting - build a remote log collection server
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
第46届ICPC亚洲区域赛(昆明) B Blocks(容斥+子集和DP+期望DP)
Classification of technology selection (2022)
Learn regular expression options, assertions
The express project changes the jade template to art template