修饰符'XXXX'对此项无效

  • 本文关键字:无效 XXXX c# static
  • 更新时间 :
  • 英文 :


有人知道我为什么会出现这些错误吗:

修饰符"static"对此项无效

修饰符"readonly"对此项无效

在以下代码的第3行:

public class YYY
{
    private static readonly struct ZZZ
    {
        private int x = 0;
        private int y = 0;
        private int z = 0;
    }
}

当我研究这个问题时,我只找到了我不太了解的接口的答案,但我只想在我的类中创建一个静态只读结构字段。

staticreadonly都是修饰符,仅用于对象的实现,而不用于定义。当您声明将要使用的ZZZ结构对象时,此时您可以添加修饰符staticreadonly

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.
}

相关内容

最新更新