C# 中列表的 null 异常

发布于 2024-10-01 01:29:20 字数 381 浏览 1 评论 0原文

嘿,我正在尝试使用 C# 中的通用列表,由于某种原因,在为列表分配内存后,我收到了 unhandeledNullException。

 //edit

我发现我的问题是什么,我没有正确使用这些属性。 如果可以说 GeoInfo 是我的类的私有成员,我该如何对其进行属性处理, 我尝试过:

 private List<GeoInfo> GEOINFOS { get; set; } // edit i forgot to change it back 
// but i want to have my geoinfos private and my properties public

提前感谢您的帮助

hey i am trying to work with a generic list in C# and for some reason after allocating memory for the list i am getting unhandeledNullException.

 //edit

i found out what was my problem i did not use the properties currectly.
if lets say GeoInfo is a private member of my class, how do i do properties to it,
i tried :

 private List<GeoInfo> GEOINFOS { get; set; } // edit i forgot to change it back 
// but i want to have my geoinfos private and my properties public

thanks in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-10-08 01:29:20

您已将属性设为私有。如果您希望它们是公共的,请尝试:

public List<GeoInfo> GeoInfos { get; set; }

本地存储在对象中的自动实现的值将是私有的;但财产本身是公开的。

因为您声明的是属性访问器。

如果你想显式地编写所有内容,你可以使用 3.0 之前的旧方式来完成它,

private List<GeoInfo> geoInfos = new List<GeoInfo>;
public List<GeoInfo> GeoInfos {
  get { return geoInfos; }
  set { geoInfos = value; }
}

这仍然依赖于在某处初始化的 geoInfos(如构造函数)——否则 nullPointerException 将返回。

您可以在 getter 中对其进行惰性求值:

private List<GeoInfo> geoInfos = new List<GeoInfo>;
public List<GeoInfo> GeoInfos {
  get { if (geoInfos == null) {
          geoInfos = new List<GeoInfo>;
        } 
        return geoInfos; 
      }
  set { geoInfos = value; }
}

这确保您不必在构造函数中指定调用,并且不必担心在获取元素之前显式设置元素的执行顺序。

但如果您使用自动生成的属性,则必须在某个时候显式设置引用。正如其他地方所建议的,最好的选择是构造函数。

You've made the properties private. If you want them to be public try:

public List<GeoInfo> GeoInfos { get; set; }

The auto-implemented value that is stored locally in the object will be private; but the properties themselves are public.

Because what you are declaring there are the property accessors.

If you want to write everything explicitly, you could do it the old pre 3.0 way

private List<GeoInfo> geoInfos = new List<GeoInfo>;
public List<GeoInfo> GeoInfos {
  get { return geoInfos; }
  set { geoInfos = value; }
}

This still relies on geoInfos being initialized somewhere (like the constructor) -- or nullPointerException will return.

You could do lazy-evaluation on it right in the getter:

private List<GeoInfo> geoInfos = new List<GeoInfo>;
public List<GeoInfo> GeoInfos {
  get { if (geoInfos == null) {
          geoInfos = new List<GeoInfo>;
        } 
        return geoInfos; 
      }
  set { geoInfos = value; }
}

This ensures that you don't have to specify a call in the constructor, and you don't have to worry about the execution sequence setting the element explicitly prior to getting it.

But if you use the auto-generated-properties, you will have to explicitly set the reference at some point. AS suggested elsewhere, the best bet is the constructor.

叹倦 2024-10-08 01:29:20

如果您希望属性是私有的,请使用

private List<GeoInfo> GEOINFOS { get; set; }

但是,没有太多理由为私有成员变量使用 auto 属性(并且也不要忘记初始化该列表)。如果您希望验证正常,但您只是将该属性用作私有变量。

您的空引用问题可能来自未初始化基础属性变量。这不会自动完成,所以

public MyClass()
{
    GEOINFOS = new List<GeoInfo>();
}

还有一件事:对于 C# 来说,属性的命名约定很奇怪。保持一致并使用 GeoInfos 怎么样?

If you want a property to be private, use

private List<GeoInfo> GEOINFOS { get; set; }

However, there's not a lot of reason to use an auto property for a private member variable (and don't forget to initialize that list as well). If you want validation fine, but you're just using that property as a private variable.

Your null reference issue probably comes from not initializing the underlying property variable. That does not get done automatically, so

public MyClass()
{
    GEOINFOS = new List<GeoInfo>();
}

One more thing: your naming convension for a property is odd for C#. How about keeping things consistent and sing GeoInfos?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文