当前位置:网站首页>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
边栏推荐
- Learn regular expression options, assertions
- Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
- Get together to watch (detailed version) eat a few cents a day
- Niuke white moon race 6 [solution]
- REINFORCE
- How to use C language to realize [guessing numbers game]
- Wepy learning record
- Opencv fills the rectangle with a transparent color
- Encapsulation of ele table
- 基于ele封装下拉菜单等组件
猜你喜欢

tf. keras. layers. Timedistributed function

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system

Sonic cloud real machine tutorial

Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel

基于ele封装下拉菜单等组件

基于多态的职工管理系统源码与一些理解
![[format] simple output (2)](/img/24/64739f5e6bbd54bfa9fb78b8c53c94.png)
[format] simple output (2)

Configuring Apache Web services for servers such as Tianyi cloud

Linux redis - redis database caching service

【Hcip】OSPF常用的6种LSA详解
随机推荐
tf. keras. layers. Density function
Codeforces Round #784 (Div. 4) (A - H)题解
Cherno_ Game engine series tutorial (5): 101~
Binary tree
Microservices (distributed architecture)
Winsock programming interface experiment: implementation of ipconfig
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
C# 11 的这个新特性,我愿称之最强!
tf. keras. layers. Inputlayer function
OCR recognition PDF file
The usage of case when and select case when is very easy to use
The space between the left and right of the movie ticket seats is empty and cannot be selected
[Euler plan question 13] sum of large numbers
Configuring Apache Web services for servers such as Tianyi cloud
Er and eer models
Huawei machine test question -- deformation of hj53 Yang Hui triangle
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
SQL statement - DDL
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
tf. keras. layers. Conv? D function