gpt4 book ai didi

c# - 如何创建元组数组?

转载 作者:可可西里 更新时间:2023-11-01 07:49:44 24 4
gpt4 key购买 nike

我知道要在 C# 中创建元组,我们使用以下格式:

Tuple <int,int>from = new Tuple<int,int>(50,350);
Tuple <int,int>to = new Tuple<int,int>(50,650);

其中每个元组是一个坐标对。我正在尝试使用元组创建多个坐标对的数组。有人可以帮我解决这个问题吗?

编辑:这是我到目前为止所尝试过的。我希望它只采用这种数组格式。

 Tuple<int, int>[] coords = new Tuple<int,int>({50,350},{50,650},{450,650});

编译器提示有问题..请告诉我它是什么?

最佳答案

在 C# 7 中

var coords = new[] { ( 50, 350 ), ( 50, 650 ), ( 450, 650 )};

enter image description here

对于命名版本,执行此操作:(感谢 entiat)

var coords2 = new(int X, int Y) [] { (50, 350), (50, 650), (450, 650) };

(int X, int Y) [] coords3 = new [] { (50, 350), (50, 650), (450, 650) };

(int X, int Y) [] coords4 = { (50, 350), (50, 650), (450, 650) };

所以我们可以用X,Y代替Item1,Item2

coords4.Select(t => $"Area = {t.X * t.Y}");

关于c# - 如何创建元组数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20490884/

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