Empty String or string.Empty in C#?

preview_player
Показать описание
Should you use the empty string constant or string.Empty in C#?
Рекомендации по теме
Комментарии
Автор

That "unfortunately!" _sigh_ at the end 😄

maacpiash
Автор

I always use string.Empty because it makes the intention clearer

modernkennnern
Автор

*spits out coffee* You could change the value of string.Empty!? MS WTF!? xD

mikicerise
Автор

Great tips as always.
I always used string.Empty because that’s how I was taught so I appreciate the deep dive

asherrfacee
Автор

Probably the most comprehensive short about a technical topic I've ever seen, good job!

pokefreak
Автор

I think string.empty also gives clear intent. There's no question what should be there at that moment. Making it far more readable

chrisspellman
Автор

Loved the "Unfortunately"!!!

maciejp
Автор

Good summary of "" vs string.Empty. On that last funny comment, I once worked with a programming language called SanScript where for a long time you could say (SanScript syntax converted to something like C#) :

procedure Foo (INOUT int bar) {
bar = 2;
}
...
main() {
Foo(1);
// prints "2"
}
Yup. After you called Foo the literal 1 now had the value 2 for the rest of the script. It was a pretty simple bug, literals in that language were just like variables except they were initialized by the compiler and you _shouldn't_ have been able to set them. The bug was simply that the compiler didn't check that the reference you were passing in to satisfy the INOUT parameter was actually a writable value. As you can imagine we had a lot of fun with that one until it was fixed.

xlerb
Автор

They had performance differences a long long time ago.

Revlissilversword
Автор

It's also easier to search for references of string.Empty in the code base rather than having to do a text search on "".

ilh
Автор

im grateful to have programmed for years and never found out string.Empty was a thing

veddy
Автор

What I'm curious about is why the framework designers decided to make string.Empty a static readonly, instead of a const. I feel like that would have made more sense, given other default values like int.MaxValue, or double.E are const, and as you suggested, it would be more usable; i.e. in switch cases. Do you know why the choice for static readonly rather than const?

MrMatthewLayton
Автор

Gotta keep that reflection hack in mind lmao

klocugh
Автор

I used to religiously specify string.Empty, but these days, I've settled on simply "". Works more places. Still clear intent.

TheBuzzSaw
Автор

As a c# old timer, you got so used to using String.Empty because you were "bad" if you didn't, it's hard to change even though it doesn't matter now.

XoloJay
Автор

so "" create a whole new string object and string.Empty references the existing empty string object

dongct
Автор

Also Empty being a "special" type of string, allows a "if(string.IsEmpty)" check rather than a check against just if(empty == "") which reduced the chance of an error in some cases. This can also make intention clearer for some cases. As you mentioned case switches require a constant. So in that case you'd not use the string.Empty. This is a great quick rundown, however both have a place and like most things in life the answer to which you use is dependent on context

charlesmayberry
Автор

string.Empty is one of those things you don't realize you miss until you're using a language which doesn't have it

KoboldAdvocate
Автор

okay that last bit is super cursed LMAO

EEEdoman
Автор

Why did they not decide to make string.Empty a constant instead of a static readonly?

cn-ml