abd*_*yum 0 c# parameter-passing visual-studio-2015
我很想知道哪一个更好用,因为我必须在c#的代码中再次检查它
if(args.Length<1) //i use
Run Code Online (Sandbox Code Playgroud)
VS
if(args.Length == 0 || args == null)
Run Code Online (Sandbox Code Playgroud)
作为替代.
或者如果有可能的话,也可以
if (args.exists) // or more simple?
Run Code Online (Sandbox Code Playgroud)
第一种形式会受到可能的NullReferenceExceptionif argsis null - 您不能访问其Length属性.
第二种形式应该颠倒为正确:
if (args == null || args.Length == 0) ...
Run Code Online (Sandbox Code Playgroud)
第三种形式在语法上是不正确的.你可能会想到LINQ函数Any:
if (args.Any()) ...
Run Code Online (Sandbox Code Playgroud)
但是你必须首先测试null.
我不清楚你究竟在测试什么.如果要检查数组中是否有某些内容,则条件应如下所示:
if (args != null && args.Length > 0)
{
// Free to access args[0] here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
884 次 |
| 最近记录: |