有人知道我为什么会出现这些错误吗:
修饰符"static"对此项无效
修饰符"readonly"对此项无效
在以下代码的第3行:
public class YYY
{
private static readonly struct ZZZ
{
private int x = 0;
private int y = 0;
private int z = 0;
}
}
当我研究这个问题时,我只找到了我不太了解的接口的答案,但我只想在我的类中创建一个静态只读结构字段。
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.
}