Ala*_*ore 2 c# arrays tuples static-initialization
我想初始化 ValueTuples 的静态只读数组,我想使用此答案中的方法:
var tupleList = new (int Index, string Name)[]
{
(1, "cow"),
(5, "chickens"),
(1, "airplane")
};
Run Code Online (Sandbox Code Playgroud)
但它不适用于静态成员,我必须声明 tupleList 的类型。我可以像这样将其作为元组来执行,但我不知道如何将其作为 ValueTuple 来执行:
static readonly Tuple<uint, string>[] tupleList= new Tuple<uint, string>[]
{
new Tuple<uint, string>(0x1, "string1"),
...
};
Run Code Online (Sandbox Code Playgroud)
但如果我能找出正确的类型,我更愿意使用更干净的格式,到目前为止,我已经尝试了多种类型,但没有成功。
您不能使用“更干净” var(隐式,但仍然强类型)类型,但您可以按照评论者的建议初始化元组。您可以摆脱的“最干净”是这样的,它在数组上使用类型推断:
(int Index, string Name)[] tupleList = {
(1, "cow"),
(5, "chickens"),
(1, "airplane")
};
Run Code Online (Sandbox Code Playgroud)
这至少可以让您避免两次指定类型,这是人们使用var.
该语言不支持在声明成员变量时使用var。对于静态字段和实例字段都是如此。
这有一个历史原因。这并不是说如果您提出更改并且经过社区审查就不能更改。
| 归档时间: |
|
| 查看次数: |
2522 次 |
| 最近记录: |