为什么我不能用的String.Empty作为可选参数相反空引号? [英] Why can't I use string.Empty as an optional parameter contrary to empty quotation marks?
问题描述
我琢磨我的代码后发现,在一些地方我有一个空字符串的默认值可选参数。我改变了从类默认值空字符串和猜测什么!显然,还有空引号和的String.Empty
之间的差异。什么鸭子? (错字意)
I'm refining my code and noticed that at some places I had optional parameters with the default value of an empty string. I changed that to a default value from the class for empty string and guess what! Apparently there's a difference between empty quotation marks and string.Empty
. What the duck?! (typo intended)
private void Khaboom(String parameter = "") { ... }
private void Bazinga(String parameter = String.Empty) { ... }
有人能解释我为什么做鸭子 Khaboom
工作,而 Bazinga
不?!
Can someone explain to me why the duck does Khaboom
work while Bazinga
doesn't?!
错误消息哼唧这样的:
默认参数值的'参数'必须是一个编译时间常数。
Default parameter value for 'parameter' must be a compile-time constant.
唉...这是!
推荐答案
空缺
定义如下:
public static readonly string Empty
这不是一个常数。这是一个只读字段。
It's not a constant. It's a readonly field.
这篇关于为什么我不能用的String.Empty作为可选参数相反空引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!