有谁知道我为什么会收到这些错误:
The modifier 'static' is not valid for this item
The modifier 'readonly' is not valid for this item
以下代码的第 3 行:
public class YYY
{
private static readonly struct ZZZ
{
private int x = 0;
private int y = 0;
private int z = 0;
}
}
当我研究这个问题时,我只找到了我不太了解的接口(interface)的答案,但我只想在我的类中创建一个静态只读结构字段。
最佳答案
static
和 readonly
都是仅在对象的实现中使用的修饰符,而不是在定义中使用。当您声明要使用的 ZZZ
结构对象时,您可以添加修饰符 static
和 readonly
。
public class YYY
{
private struct ZZZ
{
private int x = 0;
private int y = 0;
private int z = 0;
}
private static readonly ZZZ myZZZ = new ZZZ(); //The declaration of a ZZZ instance.
}
关于c# - 修饰符 'XXXX' 对此项目无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086940/