c# - string.Empty vs. "" - Has this changed? -


according this answer, "" , string.empty different, in "" creates object, whereas string.empty not. answer has votes on question.

however, this answer says there no difference. it's more recent answer well.

so in 2010. case? string.empty , "" differ @ all, slightly?

edit: question meant update linked questions, found confusing no modern answer had been presented, despite debate on matter.

the language specification (c# 4.0) silent on subject.

according clr via c#, depends entirely on clr , not on c# compiler. relevant quote p. 341:

even if assembly has attribute/flag [compilationrelaxations.nostringinterning] specified, clr may choose intern strings, should not count on this. in fact, should never write code relies on strings being interned unless have written code explicitly calls string’s intern method yourself.

so using "" may or may not create new string object. depends on clr (version) being used. , there's possibility compiler folds constants, in case "" cost 1 object per assembly compilation unit, not per occurrence.

none of has relevant impact on memory use or speed, clear guideline should both referenceequals(s1, "") , referenceequals(s1, string.empty) should avoided.

and of course object.equals(s1, s2) , s1 == s2 work fine on strings.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -