2

有谁知道我为什么会收到这些错误:

修饰符“静态”对此项目无效

修饰符“只读”对此项无效

在以下代码的第 3 行:

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

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

4

1 回答 1

2

static并且readonly都是仅在对象的实现中使用的修饰符,而不是在定义中使用。当您声明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.
}
于 2013-11-13T14:19:58.640 回答