为什么 ValueTuple 不能是 const?

Ale*_*nko 6 c# valuetuple c#-7.3

声明为字段的 ValueTuple 类型可以是可变的:

class Foo {
  (int, int) bar = (0, 1);
}
Run Code Online (Sandbox Code Playgroud)

或只读:

class Foo {
  readonly (int, int) bar = (0, 1);
}
Run Code Online (Sandbox Code Playgroud)

这种(不变)可变性适用于每个成员。我希望它也能扩展到 const 声明:

class Foo {
  const (int, int) bar = (0, 1);
}
Run Code Online (Sandbox Code Playgroud)

但是这个语句不能编译。在某些情况下,它是不受欢迎的功能,还是只是未实现的功能?

编辑 好的,我现在明白了。我的问题是基于这样一个假设,即 C# 编译器对 ValueTuples 的处理方式与其他类型不同,关于关键字 readonly。所以,如果它已经是一个例外,为什么不为 consts 设置另一个例外。但是,实际上,这种逻辑似乎适用于所有结构体。所以这行不通:

class Foo {
  readonly ExampleStruct field;
  void Method() {
    field.structField = 2;
  }
}
struct ExampleStruct {
  public int structField;
}
Run Code Online (Sandbox Code Playgroud)

Ren*_*nat 5

有一组有限的类型,可以在 c# 中用作 const(更详细的在这里)。

值元组根本不在其中。