You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
string.Empty is a static readonly empty string. Since this cannot be used in attributes, I would like to suggest that we create a constant version of this.
API Proposal
namespaceSystem{publicsealedclassString:ICloneable,IComparable,IComparable<string>,IConvertible,IEquatable<string>,System.Collections.Generic.IEnumerable<char>{// Current:publicstaticreadonlystringEmpty="";// Proposal:// Naming could also be "ConstEmpty" instead of "ConstantEmpty"publicconststringConstantEmpty="";}}
Well I used to prefer string.Empty to "" most of the time (but that does not work in Attributes). Although the static call is not much slower performance-wise.
Although the static call is not much slower performance-wise.
The static call will almost surely be optimized away. Returning ""generates the exact same assembly code as returning string.Empty. Also the IL instructions ldstr "" and ldsfld String.Empty take the same amount of space.
I persume you want to add this constant field to completely avoid writing "" in your code, based on the knowledge that magic strings are a bad practice. But the empty string is not a magic string per se, you could view its two consecutive double quotes as a symbol like || or &&.
Reacted by Huo Yaoyuan, WhiteBlackGoose, Alex and Roman Sakno
Activity
stephentoub commentedon Oct 16, 2021
Why not just use
""? Why is a new const better?deeprobin commentedon Oct 16, 2021
Well I used to prefer
string.Emptyto "" most of the time (but that does not work in Attributes). Although the static call is not much slower performance-wise.teo-tsirpanis commentedon Oct 17, 2021
The static call will almost surely be optimized away. Returning
""generates the exact same assembly code as returningstring.Empty. Also the IL instructionsldstr ""andldsfld String.Emptytake the same amount of space.I persume you want to add this constant field to completely avoid writing
""in your code, based on the knowledge that magic strings are a bad practice. But the empty string is not a magic string per se, you could view its two consecutive double quotes as a symbol like||or&&.danmoseley commentedon Oct 17, 2021
@deeprobin thanks for the proposal but the consensus seems to be that “” is sufficient and arguably more readable as well.