如何初始化const数组,而不是readonly,得到错误CS0134

时间:2013-10-28 06:37:17

标签: c# .net arrays

我有以下类与PropertyGrid的数据对象一起使用:

public enum WebUILanguage
{
    EnglishUS,
    German,
    //Actual list is obviously longer
}

[DefaultPropertyAttribute("SaveOnClose")]
public class MyData
{
    private HashSet<WebUILanguage> _SupportedUILanguages = new HashSet<WebUILanguage>(_k_SupportedUILanguages);

    //Area to define all default values    
    const WebUILanguage[] _k_SupportedUILanguages = {   //Error CS0134: A const field of a reference type other than string can only be initialized with null.
        WebUILanguage.EnglishUS,
        WebUILanguage.German
    };

    [CategoryAttribute("User Interface"),
    DefaultValueAttribute(_k_SupportedUILanguages),
    DescriptionAttribute("Supported user interface languages.")]
    [RefreshProperties(RefreshProperties.All)]
    public WebUILanguage[] SupportedUILanguages
    {
        get { return _SupportedUILanguages.ToArray(); }
        set 
        { 
            //Do 'set' magic here
        }
    }
}

但编译器不允许我初始化一个常量数组 - 上面代码中的_k_SupportedUILanguages常量。这是什么语法?

PS。请注意,我不需要一个readonly数组。它必须是常量!

0 个答案:

没有答案