在C#中,我想用空字符串初始化字符串值.
我该怎么做?什么是正确的方法,为什么?
string willi = string.Empty;
Run Code Online (Sandbox Code Playgroud)
要么
string willi = String.Empty;
Run Code Online (Sandbox Code Playgroud)
要么
string willi = "";
Run Code Online (Sandbox Code Playgroud)
或者是什么?
在.Net中为什么String.Empty只读而不是常量?我只是想知道是否有人知道该决定背后的原因是什么.
通过System.Linq.Enumerable在DotPeek中查看,我注意到一些方法是使用[__DynamicallyInvokable]属性调整的.
这个属性扮演什么角色?它是由DotPeek添加的还是它扮演另一个角色,也许告诉编译器如何最好地优化方法?
以下示例代码自然发生.突然间,我的代码成了一个非常令人讨厌的FatalExecutionEngineError异常.我花了30分钟试图隔离并最小化罪魁祸首样本.使用Visual Studio 2012作为控制台应用程序对此进行编译:
class A<T>
{
static A() { }
public A() { string.Format("{0}", string.Empty); }
}
class B
{
static void Main() { new A<object>(); }
}
Run Code Online (Sandbox Code Playgroud)
应该在.NET framework 4和4.5上产生此错误:

这是一个已知的错误,原因是什么,我可以做些什么来减轻它?我目前的工作是不使用string.Empty,但我是在吠叫错误的树吗?更改有关该代码的任何内容使其按预期运行 - 例如删除空的静态构造函数A,或将类型参数更改object为int.
我在笔记本电脑上尝试了这个代码并没有抱怨.但是,我确实尝试了我的主应用程序,它也在笔记本电脑上崩溃了.在减少问题的时候,我必须把一些东西弄错了,我会看看能否弄清楚那是什么.
我的笔记本电脑使用与上面相同的代码崩溃,使用框架4.0,但主要崩溃,即使是4.5.两个系统都使用VS'12和最新更新(7月?).
更多信息:
我偶然发现了这段代码:
static void Main()
{
typeof(string).GetField("Empty").SetValue(null, "evil");//from DailyWTF
Console.WriteLine(String.Empty);//check
//how does it behave?
if ("evil" == String.Empty) Console.WriteLine("equal");
//output:
//evil
//equal
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何编译这段代码甚至是可能的.我的理由是:
根据MSDN String.Empty是只读的,因此更改它应该是不可能的,并且编译应以"无法分配静态只读字段"或类似错误结束.
我认为Base Class Library程序集以某种方式受到保护和签名,以及什么可以防止这种攻击.下次有人可能会更改System.Security.Cryptography或其他关键类.
我认为基类库程序集是在安装.NET之后由NGEN编译的,因此更改String类的字段应该需要高级黑客并且要困难得多.
然而,这段代码编译和工作.有人可以解释我的推理有什么问题吗?
我读了Jon Skeet的测验,我想知道为什么我的第二个样本在第一个样本时不起作用.
为什么这会产生true:
object x = new string("".ToArray());
object y = new string("".ToArray());
Console.WriteLine(x == y); //true
Run Code Online (Sandbox Code Playgroud)
但是这个没有:
var k="k";
//string.intern(k); // doesn't help
object x = new string(k.ToArray());
object y = new string(k.ToArray());
Console.WriteLine(x == y); //false
Run Code Online (Sandbox Code Playgroud)
我在vs2010上使用fw 4.5.
幸运的是我也安装了vs2005,结果相同:

虽然我明白改变价值String.Empty会是一个坏主意,但我不明白为什么我不能这样做.
为了得到我的意思,请考虑以下课程:
public class SomeContext
{
static SomeContext(){}
public static readonly string Green = "Green";
public static readonly SomeContext Instance = new SomeContext();
private SomeContext(){}
public readonly string Blue = "Blue";
public static void PrintValues()
{
Console.WriteLine(new { Green, Instance.Blue, String.Empty }.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个小的控制台应用程序,试图操纵这三个只读字段.它可以成功地将蓝色和绿色变成粉红色,但是Empty保持不变:
SomeContext.PrintValues();
/// prints out : { Green = Green, Blue = Blue, Empty = }
typeof(SomeContext).GetField("Blue").SetValue(SomeContext.Instance, "Pink");
typeof(SomeContext).GetField("Green", BindingFlags.Public | BindingFlags.Static).SetValue(null, "Pink");
typeof(String).GetField("Empty", BindingFlags.Public | BindingFlags.Static).SetValue(null, "Pink");
SomeContext.PrintValues();
/// prints out : { …Run Code Online (Sandbox Code Playgroud) 我正在改进我的代码并注意到在某些地方我有可选参数,其默认值为空字符串.我将其更改为类中的默认值为空字符串,猜猜是什么!显然,空引号和string.Empty.之间存在差异.什么鸭子?!(打字错误)
private void Khaboom(String parameter = "") { ... }
private void Bazinga(String parameter = String.Empty) { ... }
Run Code Online (Sandbox Code Playgroud)
谁能向我解释为什么不鸭Khaboom工作,同时Bazinga不?
错误消息引发了这样的错误:
'parameter'的默认参数值必须是编译时常量.
嗯...... 是的!