C#7.0 ValueTuples与匿名类型

编程入门 行业动态 更新时间:2024-10-28 12:29:53
本文介绍了C#7.0 ValueTuples与匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

看看新的C#7.0 ValueTuples,我想知道它们是否将完全取代Anonymous Types.我知道ValueTuples是结构,因此其行为与作为类的Anonymous Types有所不同.但是,我看不到用例,在这种情况下,我宁愿使用Anonymous Type而不是ValueTuple.

Looking at the new C# 7.0 ValueTuples, I am wondering if they will completely replace Anonymous Types. I understand that ValueTuples are structs and therefore behave a bit differently than Anonymous Types which are classes. I don't see a use-case, however, in which I would prefer using an Anonymous Type over a ValueTuple.

在C#7.0中是否有使用Anonymous Type优于使用ValueTuples的用例?

Are there any use-cases where using an Anonymous Type would still be beneficial over using ValueTuples in C# 7.0?

推荐答案

匿名类型是不可变的,元组不是.这反映在以下事实上:匿名类型公开属性,元组公开字段.数据绑定几乎总是需要属性.

Anonymous types are immutable, tuples are not. This is reflected in the fact that anonymous types expose properties, tuples expose fields. Data binding almost always requires properties.

大量现有代码仅适用于引用类型,不适用于值类型.特别想到的是Entity Framework中的预测:对值类型的预测根本没有实现.

Plenty of existing code only works with reference types, not with value types. What in particular comes to mind are projections in Entity Framework: projections to value types are simply not implemented.

更多推荐

C#7.0 ValueTuples与匿名类型

本文发布于:2023-11-04 10:40:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   ValueTuples

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!