C# 7.0中解构功能详解
作者:莫问今朝乄 时间:2022-08-11 21:06:34
本文为大家分享了C# 7.0中的解构功能,供大家参考,具体内容如下
解构元组
C#7.0新增了诸多功能,其中有一项是新元组(ValueTuple),它允许我们可以返回多个值,并且配合解构能更加方便的进行工作,如下面例子
static void Main(string[] args)
{
(var name, var age) = GetUser();
Console.WriteLine($"name:{name}\nage:{age}");
}
public static (string name,int age) GetUser()
{
return ("张三", 11);
}
可以看到解构元组可以写出优雅的代码,并且可以使用类型推断,但在这里解构元组并不是重点,所以不过多关注,下面说一个有趣的功能
解构对象
解构能力并不是只能解构元组,他还有一个更加有意思的功能,就是解构对象。是不是听起来挺有意思。
static void Main(string[] args)
{
var user = new User
{
Name = "张三",
Age = 11,
Email = "zhangsan@126.com",
Sex="男"
};
(var name, var email) = user;
Console.WriteLine($"name:{name}\nemail:{email}");
}
上面代码是不是很惊奇,反正我第一次看到时特别惊讶,也感觉特别有意思,那么这到底怎么实现的呢,其实只是在类中添加一个解构函数(Deconstruct)就可以,解构参数方法名称必须是Deconstruct,返回值必须是void,参数列表必须是out
public class User
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
//解构函数
public void Deconstruct(out string name,out string email)
{
name = Name;
email = Email;
}
}
是不是感觉特别简单.
解构函数还支持重载
class Program
{
static void Main(string[] args)
{
var user = new User
{
Name = "张三",
Age = 11,
Email = "zhangsan@126.com",
Sex="男"
};
(var name1, var email1) = user;
Console.WriteLine($"name:{name1}\temail:{email1}");
(var name2,var age2, var email2) = user;
Console.WriteLine($"name:{name2}\tage:{age2}\temail:{email2}");
}
}
public class User
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
//解构函数
public void Deconstruct(out string name,out string email)
{
name = Name;
email = Email;
}
//结构函数重载
public void Deconstruct(out string name,out int age,out string email)
{
name = Name;
age = Age;
email = Email;
}
}
但是解构不支持参数一致的重载
哪怕参数类型不一致
感觉像参数类型推断错误,但是
所以解构函数并不参数数量相同的重载,哪怕参数类型不一致。
来源:https://www.cnblogs.com/yan7/p/9501638.html
标签:C#,解构
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java中多进程编程的实现
2021-12-22 01:46:06
Spring使用AOP完成统一结果封装实例demo
2021-12-16 18:49:06
![](https://img.aspxhome.com/file/2023/0/71630_0s.png)
SpringBoot+docker环境变量配置详解
2022-06-13 07:37:20
![](https://img.aspxhome.com/file/2023/6/84756_0s.png)
Java 实战项目锤炼之医院门诊收费管理系统的实现流程
2022-08-10 11:35:08
![](https://img.aspxhome.com/file/2023/0/74990_0s.png)
java寻找迷宫路径的简单实现示例
2021-07-06 13:17:50
nacos使用占位符${}进行参数配置的方法
2022-07-15 08:59:07
![](https://img.aspxhome.com/file/2023/4/63334_0s.png)
Java语言中&&与& ||与|的区别是什么
2022-06-10 17:44:09
使用Spring Boot Mybatis 搞反向工程的步骤
2022-05-15 06:54:35
![](https://img.aspxhome.com/file/2023/9/86809_0s.png)
Java 通过AQS实现数据组织
2023-04-05 22:19:29
![](https://img.aspxhome.com/file/2023/2/63072_0s.png)
SpringBoot实现文件上传下载功能小结
2022-11-09 07:40:07
Android 实现永久保存数据的方法详解
2023-06-23 22:13:23
springboot搭建访客管理系统的实现示例
2023-09-02 13:10:41
![](https://img.aspxhome.com/file/2023/3/58463_0s.png)
java.lang.String类的使用
2021-12-11 03:57:26
Java7之forkjoin简介_动力节点Java学院整理
2023-08-31 14:08:28
![](https://img.aspxhome.com/file/2023/1/58341_0s.png)
详解c#与python的交互方式
2023-12-13 09:16:11
![](https://img.aspxhome.com/file/2023/0/72450_0s.png)
DWR异常情况处理常见方法解析
2022-10-14 02:10:50
SpringBoot ApplicationContext接口深入分析
2021-10-21 23:00:03
![](https://img.aspxhome.com/file/2023/6/61056_0s.png)
idea 有时提示找不到类或者符号的解决
2023-11-29 04:10:43
![](https://img.aspxhome.com/file/2023/9/60779_0s.jpg)
c#多线程之线程基础
2022-10-06 20:34:44
在Spring Boot中从类路径加载文件的示例
2023-11-11 21:53:44