我的问题是为什么我们需要在string []中检查null错误.是不是字符串类型默认接受空值?那么为什么我们应该通过使用Null条件运算符标记(?)来检查string []是否包含null.
例如看看这个方法:
它来自C#和NetFramework 4.6
static void TesterMethod(string[] args)
{
// We should check for null before accessing the array data!
Console.WriteLine($"You sent me {args?.Length} arguments.");
}
Run Code Online (Sandbox Code Playgroud)
我的意思是,如果我们可以为字符串赋值null为什么我们应该检查null错误?为什么要抛出错误?
Console.WriteLine($"You sent me {args.Length} arguments.");
Run Code Online (Sandbox Code Playgroud)
如果args为null,则调用it(Length)上的属性将导致NullReferenceException.args在调用之前,您必须确保不为null.
并且args是一个类型的数组string,而不是它string自己.
那么为什么我们应该通过使用Null条件运算符标记(?)来检查string []是否包含null
它不包含 null,它为 null.条件运算符检查数组,而不是数组的内容.
| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |