Visual-Stylecop / Visual-StyleCop

Copy of StyleCop from stylecop.codeplex.com for visual studio 2015 and C# 6
http://visual-stylecop.github.io/Visual-StyleCop
Other
82 stars 22 forks source link

SA1122: Rule to prefer string.Empty over "" is missing justification #81

Open rillig opened 8 years ago

rillig commented 8 years ago

https://github.com/Visual-Stylecop/Visual-StyleCop/wiki/SA1122 currently says:

This will cause the compiler to embed an empty string into the compiled code.

The rule SA1122 is in the Readability category, therefore it should not matter what code is generated by the compiler. Furthermore, when using string.Empty instead of "", the compiler embeds a reference to string.Empty into the compiled code. I didn't check it, but it feels like that takes more bytes in the resulting binary.

The C# language specification says:

Each string literal does not necessarily result in a new string instance. When two or more string literals that are equivalent according to the string equality operator (§7.10.7) appear in the same program, these string literals refer to the same string instance.

This sounds like an argument in favor of using string literals directly.

All in all, what is missing is a convincing argument why using string literals is bad.

yariker commented 7 years ago

Another argument against this rule is that using string.Empty instead of "" inevitably leads to a mixture of styles, because you can't put string.Empty inside of an attribute, constant initializer or default parameter.

MisinformedDNA commented 5 years ago

I would suggest turning this off, by default.