静态列表显示为空

我有一个名为常量的静态类。


public static class Constants

{

    public static string sampleString= "";

    public static List<int> sampleList= new List<int> {1,2,3};

}

如果我在外面调用我的静态列表: Constants.sampleList它会给我一个空异常,但Constants.sampleString可以毫无问题地调用。


我在这里错过了什么吗?


慕尼黑的夜晚无繁华
浏览 113回答 5
5回答

慕沐林林

不确定这是否与我们的情况相同,但我们有一个类可以从配置中访问密钥string&nbsp;sampleString&nbsp;=&nbsp;WebConfigurationManager.AppSettings["SampleString"]由于一些合并问题,示例字符串已从我们的 web.config 中删除。当您访问变量下面的类中的任何变量时,会发生空指针错误。在配置中添加密钥解决了这个问题。

湖上湖

当我运行此代码时:void Main(){&nbsp; &nbsp; Console.WriteLine(Constants.sampleList.Contains(1));}public static class Constants{&nbsp; &nbsp; public static string sampleString= "";&nbsp; &nbsp; public static List<int> sampleList= new List<int> {1,2,3};}我进入True控制台。您需要提供演示您所面临问题的代码。

largeQ

如果您readonly在方法中添加关键字。该方法只能在构造函数内部或在属性声明阶段进行实例化。public&nbsp;static&nbsp;readonly&nbsp;List<int>&nbsp;sampleList=&nbsp;new&nbsp;List<int>&nbsp;{1,2,3};如果您尝试重新初始化或对 进行新分配sampleList,C# 编译器会给您编译器错误。更好地使用属性public&nbsp;static&nbsp;readonly&nbsp;List<int>&nbsp;SampleList&nbsp;{get;&nbsp;set;}&nbsp;=&nbsp;new&nbsp;List<int>&nbsp;{1,2,3};

白衣非少年

没有奇迹,如果sampleList是null(并且您抛出了异常),那么您null在某处分配给它。尽量不要暴露 易受攻击的public 字段:Constants&nbsp; public static class Constants&nbsp; {&nbsp; &nbsp; &nbsp; public static string sampleString = "";&nbsp; &nbsp; &nbsp; public static List<int> sampleList = new List<int> {1,2,3};&nbsp; }&nbsp; ...&nbsp; Constants.sampleList = null;&nbsp; &nbsp;// We can easly assign null&nbsp; ...&nbsp; Constants.sampleList.Add(123); // <- And have an unxpected exception但是要么把它们变成属性:&nbsp; public static class Constants&nbsp; {&nbsp; &nbsp; &nbsp; private static string s_SampleString = "";&nbsp; &nbsp; &nbsp; private static List<int> s_SampleList = new List<int> {1,2,3};&nbsp;&nbsp; &nbsp; &nbsp; public static string sampleString {&nbsp; &nbsp; &nbsp; &nbsp; get {return s_SampleString;}&nbsp; &nbsp; &nbsp; &nbsp; set {s_SampleString = value ?? "";}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; public static List<int> sampleList {&nbsp; &nbsp; &nbsp; &nbsp; get {return s_SampleList;}&nbsp; &nbsp; &nbsp; &nbsp; // I doubt you actually want set here&nbsp; &nbsp; &nbsp; &nbsp; set {s_SampleList = value ?? new List<int>();}&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; }或者,至少,将字段标记为readonly(您只能分配一次):&nbsp; public static class Constants&nbsp; {&nbsp; &nbsp; &nbsp; private static string s_SampleString = "";&nbsp; &nbsp; &nbsp; public static string sampleString {&nbsp; &nbsp; &nbsp; &nbsp; get {return s_SampleString;}&nbsp; &nbsp; &nbsp; &nbsp; set {s_SampleString = value ?? "";}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; // Manipulate with me, but not reassign&nbsp; &nbsp; &nbsp; public static readonly List<int> sampleList = new List<int> {1,2,3};&nbsp;&nbsp; }无论哪种情况,您仍然可以使用列表进行操作:&nbsp; Constants.sampleList.Add(4);&nbsp; Constants.sampleList.RemoveAt(0);但是您可以避免分配null给列表:将分配空列表(带有属性的代码),或者您将遇到编译时错误(带有 的代码readonly)

跃然一笑

在我的例子中,我private static readonly Dictionary<byte, string>在一个结构内部定义了一个来保存预定义的常量。这通常可以正常工作,但是我还在我的结构中定义了一个 MinValue 来表示一条数据的最小值。以这种方式完成时,静态字典未初始化,除非在静态 MinValue 属性上方定义。我可能对编译器的要求太多了,应该对其进行重构。在大型结构中很难诊断,因为我没想到 C# 会出现这种行为。重现示例:public struct MyStruct{&nbsp; &nbsp; public string Str;&nbsp; &nbsp; public static readonly MyStruct MinValue = new MyStruct(0);&nbsp; &nbsp; public MyStruct(byte val)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Str = _predefinedValues[val]; // null reference exception&nbsp; &nbsp; }&nbsp; &nbsp; private static readonly Dictionary<byte, string> _predefinedValues = new Dictionary<byte, string>()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; {0x00, "test 1"},&nbsp; &nbsp; &nbsp; &nbsp; {0x01, "test 2"},&nbsp; &nbsp; &nbsp; &nbsp; {0x02, "test 3"},&nbsp; &nbsp; };}这里的解决方案是双重的:不调用结构上的任何构造函数并在不访问_predefinedValues列表的情况下显式设置每个字段。或者,将列表向上移动到 MinValue 字段声明之上实际上也可以修复它(奇怪吧?)我怀疑在尝试分配它时堆栈上发生了一些奇怪的事情,这可能是一件奇怪的事情。
打开App,查看更多内容
随时随地看视频慕课网APP