元组可用于您想要一个数据结构来保存具有属性的对象,但又不想为其创建单独的类型的情况。tuple 类是在 .net framework 4.0 中引入的。元组是一种包含不同数据类型的元素序列的数据结构。
tuple<int, string, string> person =new tuple <int, string, string>(1, "test", "test1");
一个元组最多只能包含八个元素。当您尝试包含超过八个元素时,它会给出编译器错误。
列表元组var tuplelist = new list<(int, string)>{ (1, "cow1"), (5, "chickens1"), (1, "airplane1")};
数组元组var tuplearray = new(int, string)[]{ (1, "cow1"), (5, "chickens1"), (1, "airplane1")};
嵌套元组var numbers = tuple.create(1, 2, 3, 4, 5, 6, 7, tuple.create(8, 9, 10, 11, 12, 13));tuple as a method parameterstatic void displaytuple(tuple<int,string,string> person){}
元组作为返回类型static tuple<int, string, string> gettest(){ return tuple.create(1, "test1", "test2");}
以上就是如何在 c# 中轻松初始化元组列表?的详细内容。