什么是相当于下面的代码片段的简写?
if (strValue == ""){
throw new Exception("Mandatory 'strValue' parameter empty");
}
Run Code Online (Sandbox Code Playgroud)
除非你删除了空格和大括号(并且在此过程中牺牲了可读性),否则它可能就像你可以得到它一样短.
至于正确性......这可能会更好:
.NET 4.0:
if (string.IsNullOrWhiteSpace(strValue)){
throw new ArgumentException("Mandatory 'strValue' parameter empty");
}
Run Code Online (Sandbox Code Playgroud)
.NET <4.0:
if (string.IsNullOrEmpty(strValue)){
throw new ArgumentException("Mandatory 'strValue' parameter empty");
}
Run Code Online (Sandbox Code Playgroud)
另请注意,简单地抛出是不好的做法Exception- 如果存在BCL,则从BCL中选择适当的异常类要好得多,如果不存在,则自定义一个.(感谢@djacobson)
if(strValue=="")throw new Exception("Mandatory 'strValue' parameter empty");
Run Code Online (Sandbox Code Playgroud)
你所能做的就是删除大括号和空格:)