为什么我无法检查连接字符串上的空引用?

发布于 2024-07-27 22:59:15 字数 762 浏览 2 评论 0原文

可能是一个 C# 菜鸟问题,所以不要攻击我。 我试图这样做:

if (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null)
{
    // ...
}

但我不断收到System.NullReferenceException。 我想既然它返回一个字符串,我就可以检查 null 并继续。 我花了一段时间才找到正确的方法:

ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
    this.Connection.ConnectionString = cs.ConnectionString;
}

因此在其他情况下,例如检查 Session 对象的某些值,我会像这样检查 null

if (Session["EmployeeID"] != null)
{
    _EmployeeID = System.Convert.ToInt32(Session["EmployeeID"]);
}

所以我只是想知道你怎么知道什么时候可以或不能检查 null

Probably a C# noob question, so don't flame me. I was trying to do this:

if (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null)
{
    // ...
}

But I kept getting a System.NullReferenceException. I thought since it returns a string that I could just check for null and move on. It took me a while to find the right way to do it:

ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
    this.Connection.ConnectionString = cs.ConnectionString;
}

So in other instances, like checking a Session object for some value I would do a check for null like this:

if (Session["EmployeeID"] != null)
{
    _EmployeeID = System.Convert.ToInt32(Session["EmployeeID"]);
}

So I just wanted to know how do you know when you can or can't do a check for null?

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

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

发布评论

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

评论(4

半窗疏影 2024-08-03 22:59:29

所以我只是想知道你怎么样
知道什么时候可以或不能进行检查
为空?

可以随时检查是否为 null(不可为 null 的值类型除外,该类型不能为 null)。

当您希望访问对象的成员时,您应该检查是否为 null,并且您不确定包含该对象的变量是否不为 null。

空检查的几个典型位置是:

  • 在您正在编写的函数中,将一个对象作为参数传递给您。 在使用参数之前对其进行空检查。
  • 您调用一个方法(或获取属性值),返回您希望使用的对象。 在使用该返回值之前对其进行空检查。
  • 您将一个对象传递给一个方法,据记录,如果参数为 null,它将抛出 NullReferenceException。 在调用该方法之前对要传递的变量进行空检查。

So I just wanted to know how do you
know when you can or can't do check
for null?

You can check for null at any time you like (except with non-nullable value types, that cannot be null).

You should check for null at any time you wish to access the members of an object, and you are not certain that the variable containing the object is not null.

A couple of typical places for null check are:

  • You get an object passed to you as a parameters in a function you are writing. Do a null-check on the parameter before using it.
  • You call a method (or fetch a property value) returning an object that you wish to use. Do a null-check on that return value before using it.
  • You pass an object to a method where it is documented that it will throw a NullReferenceException if the parameter is null. Do a null check on the variable you wish to pass before calling the method.
初见 2024-08-03 22:59:28

您的第一个测试检查 ConnectionStringSettings 中的连接字符串是否为 null。 第二个测试检查 ConnectionStringSettings 引用本身是否为 null。 这是之间的区别:

if (person.Name == null)

if (person == null)

如果 person 为 null,则第一个将会爆炸; 第二个不会发现 person.Name 是否为 null。 如果你想检查两者,你需要:

if (person == null || person.Name == null)

某些语言(例如 Groovy)有一个 null-safe 解引用运算符,所以你可以这样做:

var x = Expr1?.Expr2?.Expr3?.Expr4;

如果你想测试是否任何 可能很长的表达式的一部分为 null。 不幸的是 C# 没有这个:(

Your first test checks whether the connection string within the ConnectionStringSettings is null. The second test checks whether the ConnectionStringSettings reference itself is null. It's the difference between:

if (person.Name == null)

and

if (person == null)

The first will go bang if person is null; the second won't spot if person.Name is null. If you want to check both, you need:

if (person == null || person.Name == null)

Some languages (such as Groovy) have a null-safe dereferencing operator, so you can do:

var x = Expr1?.Expr2?.Expr3?.Expr4;

That makes this sort of test easier, if you want to test whether any part of a potentially long expression is null. Unfortunately C# doesn't have that :(

楠木可依 2024-08-03 22:59:26

您的空引用异常发生在父对象 (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"]) 中。 一旦你也检查了,你就没事了。

试试这个:

if ((ConfigurationManager.ConnectionStrings["PrimaryConnectionString"] != null)
&& (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null))
  { etc etc }

请注意,如果您使用其中的更多方法,则将这些检查分开会很有用。

Your null reference exception happened in parent object (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"]). Once you check it also, you are fine.

try this:

if ((ConfigurationManager.ConnectionStrings["PrimaryConnectionString"] != null)
&& (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null))
  { etc etc }

Notice that if you use more methods from it, it would be useful to separate these checks.

流年已逝 2024-08-03 22:59:24

您的问题是您正在检查:

ConfigurationManager
    .ConnectionStrings["PrimaryConnectionString"]
        .ConnectionString

是否有空指针。

事实上,

ConfigurationManager
    .ConnectionStrings["PrimaryConnectionString"]

为 null,因此,当您尝试取消引用 that 来获取连接字符串时,就会出现异常。 实际上,您正在做的是:

null.ConnectionString

这是有问题的。

我倾向于避免在单个语句中进行多层取消引用,或者在整个事情周围放置一个异常处理程序以在任何时候捕获问题。

Your problem was that you were checking:

ConfigurationManager
    .ConnectionStrings["PrimaryConnectionString"]
        .ConnectionString

for a null pointer.

In actual fact,

ConfigurationManager
    .ConnectionStrings["PrimaryConnectionString"]

was null so that, when you tried to dereference that to get the connection string, that's when you got the exception. Effectively, what you're doing is:

null.ConnectionString

which is problematic.

I tend to either avoid many layers of dereferencing in a single statement or place an exception handler around the whole thing to catch problems at any point.

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