C# 4.0 - 如何处理可选字符串参数
此代码无效:
private void Foo(string optionalString = string.Empty)
{
// do foo.
}
但此代码是:
private void Foo(string optionalString = "")
{
// do foo.
}
为什么?因为 string.Empty 是只读字段,而不是常量,并且可选参数的默认值必须是编译时常量。
那么,关于我的问题...(好吧,关心)
这就是我必须做的:
private const string emptyString = "";
private void Foo(string optionalString = emptyString)
{
// do foo.
if (!string.IsNullOrEmpty(optionalString))
// etc
}
你们如何处理可选的字符串参数?
为什么他们不能将 String.Empty 编译为 -时间常数?
This code is not valid:
private void Foo(string optionalString = string.Empty)
{
// do foo.
}
But this code is:
private void Foo(string optionalString = "")
{
// do foo.
}
Why? Because string.Empty is a readonly field, not a constant, and defaults for optional parameters must be a compile-time constant.
So, onto my question... (well, concern)
This is what i've had to do:
private const string emptyString = "";
private void Foo(string optionalString = emptyString)
{
// do foo.
if (!string.IsNullOrEmpty(optionalString))
// etc
}
How do you guys handle optional string parameters?
Why can they not make String.Empty a compile-time constant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
嗯... string optalParm = "" 又出了什么问题?为什么这么糟糕?在这种情况下,您真的认为空字符串需要一个符号常量吗?那这个怎么样?
你觉得这很愚蠢吗?
Ummm... what's wrong with string optionalParm = "" again? Why is that bad? Do you really think you need a symbolic constant for an empty string in this case? How about this then?
Does that seem at all silly to you?
如果您不喜欢“”值,您可以使用默认值(字符串)。
我玩过它,这是允许的。
if you don't like "" value you can use default(string).
I played with it and it is allowed.
代码分析警告 1026 表示不要使用可选参数。使用重载方法是更好的风格,如下所示:
Code Analysis warning 1026 says not to use optional parameters. It's better style to use overload methods, like this:
处理它们的最好方法是:
所以你不能使用 String.Empty。每个人都认识“”,但如果我发现
optionalString = nullString
我不知道该怎么想。 如果没有别的事,请将其命名为emptyString
——它不为空!The best way to handle them is with:
So you can't use String.Empty. Everyone recognizes "", but if I found
optionalString = nullString
I wouldn't be sure what to think. If nothing else, name the thingemptyString
--it's not null!我正在回答这个问题。
为什么不能将 String.Empty 设为编译时常量?
下面是通过 Reflector 对 mscorlib.dll 中的 String.cs 进行反汇编的代码,
所以在 Windows 平台下,string.Empty 就是“”。但你知道吗,Martian 在他们的操作系统中对 Empty 和 WhitespaceChars 有不同的定义。
I'm answering this question.
Why can they not make String.Empty a compile-time constant?
Here is the disassemble code via Reflector of String.cs in mscorlib.dll
So in windows platform, string.Empty is exactly "". But do you know, Martian have a different definition for Empty and WhitespaceChars in their OS.
如果您愿意玩输,并将 null、"" 和空白字符视为相同,那么您可以默认为
null
。当用户名和密码是可选字段时,由于与数据库的可信连接的可能性,这变得非常方便。您可以更改此逻辑以将字符串重置为null
,从而修改断言和if
。重要的是要有一致的约定。If you are willing to play lose and treat null, "", and whitespace characters to be the same, then you can default to
null
. This becomes very handy when user name and password are optional fields due to a possibility of trusted connection to a db. You could change this logic to reset strings tonull
and thus modify the assert and theif
. The important part is having a consistent convention.