gpt4 book ai didi

c# - 匿名类型和元组

转载 作者:IT王子 更新时间:2023-10-29 04:13:40 27 4
gpt4 key购买 nike

匿名类型和元组有什么区别?

最佳答案

自从 C# 7 发布以来,对这个答案进行了一点更新。元组现在拥有超能力,有时可以替代匿名类型和类。以这个接受并返回具有命名属性的元组的方法为例。

为了帮助说明什么是可能的,Whatever 函数转换输入形状和值。

void Main()
{
var tupleInput = (Neat: 123, Cool: true);
var tupleOutput = Whatever(tupleInput);
Debug.Assert(tupleOutput.Something == 133);
Debug.Assert(tupleOutput.Another == "True");
}

(int Something, string Another) Whatever((int Neat, bool Cool) data)
{
return (Something: data.Neat + 10, Another: data.Cool.ToString());
}

太好了。

关于c# - 匿名类型和元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2613829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com