Empty String or string.Empty in C#?

  Рет қаралды 124,690

Nick Chapsas

Nick Chapsas

Жыл бұрын

Should you use the empty string constant or string.Empty in C#?

Пікірлер: 55
@maacpiash
@maacpiash Жыл бұрын
That "unfortunately!" _sigh_ at the end 😄
@modernkennnern
@modernkennnern Жыл бұрын
I always use string.Empty because it makes the intention clearer
@jongeduard
@jongeduard 11 ай бұрын
I think that's basically the only point of it. Since the beginning almost 20 years ago I have been using it. However I have also always questioned it, because other languages that I used, like C, Java and JS, don't have it. So the best thing to say is that it's helpful for readability, but in any other way not an actual need.
@TheCameltotem
@TheCameltotem 8 ай бұрын
Yeah i've started using it aswell. Sometimes =""; looks like you missed something or it just don't look as clean as string.empty
@minhan4444
@minhan4444 6 ай бұрын
@@jongeduard java, C, js can make empty string become EMPTY_STRING with module, class for constants intention and readability like C# :3
@mikicerise6250
@mikicerise6250 Жыл бұрын
*spits out coffee* You could change the value of string.Empty!? MS WTF!? xD
@TehKarmalizer
@TehKarmalizer 6 ай бұрын
And he laments not being able to anymore. Man is clearly a war criminal.
@Daniel15au
@Daniel15au 2 ай бұрын
You can change a lot of things if you use reflection, since it lets you touch private fields.
@maciejp8001
@maciejp8001 Жыл бұрын
Loved the "Unfortunately"!!!
@pokefreak2112
@pokefreak2112 Жыл бұрын
Probably the most comprehensive short about a technical topic I've ever seen, good job!
@chrisspellman5952
@chrisspellman5952 Жыл бұрын
I think string.empty also gives clear intent. There's no question what should be there at that moment. Making it far more readable
@asherrfacee
@asherrfacee 9 ай бұрын
Great tips as always. I always used string.Empty because that’s how I was taught so I appreciate the deep dive
@Veddy1674
@Veddy1674 16 күн бұрын
im grateful to have programmed for years and never found out string.Empty was a thing
@Revlissilversword
@Revlissilversword Жыл бұрын
They had performance differences a long long time ago.
@ilh86
@ilh86 Жыл бұрын
It's also easier to search for references of string.Empty in the code base rather than having to do a text search on "".
@klocugh12
@klocugh12 Жыл бұрын
Gotta keep that reflection hack in mind lmao
@dongct2504
@dongct2504 11 ай бұрын
so "" create a whole new string object and string.Empty references the existing empty string object
@Daniel15au
@Daniel15au 2 ай бұрын
"" reuses one string object too. Constant (hard-coded) strings are interned by the compiler, which means that only one copy of them exists in memory at runtime. This is doable because strings are immutable - methods like ToUpperCase return a new string rather than modifying the existing one.
@MrMatthewLayton
@MrMatthewLayton Жыл бұрын
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?
@dongct2504
@dongct2504 11 ай бұрын
constants are evaluated at compile-time and directly replaced with their values wherever they are used in code. Therefore, if string.Empty were defined as a constant, every occurrence of string.Empty in the code would be replaced with an empty string during compilation. This means that any modification to the value of string.Empty in subsequent versions or updates of .NET would not be reflected in already compiled code, which could lead to unexpected behavior. By declaring string.Empty as a static readonly field, it allows for more flexibility and ensures that all references to string.Empty always point to the same empty string instance at runtime. This approach allows future updates to the .NET framework to modify the underlying implementation of string.Empty without breaking existing compiled code. Additionally, making string.Empty a static readonly field provides better performance compared to a constant, as the value doesn't need to be inlined during compilation. It also allows for lazy initialization, where the empty string object is created only when first accessed, reducing unnecessary memory allocation. Therefore, the decision to use static readonly for string.Empty in C# was made based on considerations of maintainability, flexibility, and performance...
@myname2462
@myname2462 9 ай бұрын
Thanks ChatGPT ;-)
@mischa7823
@mischa7823 Ай бұрын
@@dongct2504 Why should the value of an empty string ever change in upcomming C# and/or .net versions?
@TheBuzzSaw
@TheBuzzSaw 4 ай бұрын
I used to religiously specify string.Empty, but these days, I've settled on simply "". Works more places. Still clear intent.
@charlesmayberry2825
@charlesmayberry2825 Жыл бұрын
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
@eslof
@eslof 11 ай бұрын
c# only has isNullOrEmpty and IsNullOrWhiteSpace and they both work on a string set to "" as well as one set to string.Empty;
@LeMustache
@LeMustache 2 ай бұрын
This comment is untrue, makes absolutely 0 sense if you know how string comparison works in C#, made up non existing methods in .net and even contradicted what was said in the video.
@Herio7
@Herio7 Жыл бұрын
I was using string.Empty whenever possible, now I'm going to double down on it. This is just explicit, no bullshit way of saying It should be empty string here. You can be certain writer meant what it's saying as there is no way it's mistake. "" might be unfinished implementation, forgotten placeholder or mistake with other chars/strings. Whenever I'm forced to use "" i always comment it that I meant it.
@EEEdoman
@EEEdoman Жыл бұрын
okay that last bit is super cursed LMAO
@KoboldAdvocate
@KoboldAdvocate 9 ай бұрын
string.Empty is one of those things you don't realize you miss until you're using a language which doesn't have it
@civilpy
@civilpy 11 ай бұрын
❤❤❤❤best bro
@cn-ml
@cn-ml 8 ай бұрын
Why did they not decide to make string.Empty a constant instead of a static readonly?
@jakubtracz7946
@jakubtracz7946 Жыл бұрын
Could you please provide the source of the table with HTML codes you used? Thanks
@eslof
@eslof 11 ай бұрын
if it doesn't matter; then it matters. you're using
@Plagueheart
@Plagueheart Жыл бұрын
lol the reflection end
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
I tend to use mostly the "" version, because it just feels better and simpler than string.Empty (plus the question of not allowing people to use reflection to change it hahaha). At the end of the day it will be carved into the assembly anyways.
@KanashimiMusic
@KanashimiMusic 11 ай бұрын
Is there any reason why string.Empty isn't declared as a constant in the first place? It makes no sense to me.
@marvilous292
@marvilous292 11 ай бұрын
Me: watching as the a lots of code fly by, not knowing what it do.......................but have programmed a bit. also me: *notices a number 69420 fly by* 🤣
@kipchickensout
@kipchickensout 9 ай бұрын
Why can't they make String.Empty a constant? I tried using it in places I can't too many times, and even though it's not so important, I want to make it clear in my code that that string is supposed to be empty and shouldn't be easily changeable
@kipchickensout
@kipchickensout 2 күн бұрын
still got the same question
@rotacioskapa4251
@rotacioskapa4251 Жыл бұрын
unfortunatelly :D
@Elian-Fabian
@Elian-Fabian Жыл бұрын
Why it was decided to make string.Emtpy to be a readonly field instead of a constant?
@jas88cam
@jas88cam 10 ай бұрын
As a constant it would get inlined by the compiler, resulting in duplicate empty string objects - as a readonly static, everything has a reference to the same object. Same with Array.Empty etc: more efficient to share a single object than duplicates.
@diadetediotedio6918
@diadetediotedio6918 2 ай бұрын
@@jas88cam No? String constants points to the same object in memory, this is a very bad response to the question.
@jas88cam
@jas88cam 2 ай бұрын
@@diadetediotedio6918 the answer from one of the developers is basically that it's historical baggage in this case, and it is special-cased by the compiler rather than used as it appears. Back in Framework 3.5 and earlier, string.Empty apparently wasn't interned, but that changed later.
@diadetediotedio6918
@diadetediotedio6918 2 ай бұрын
@@jas88cam Hm, can you say for me the name of the source you found this? (as youtube does not allow for URL's directly) I find this very strange, since as I know back in 3.5 < raw strings were also shared at runtime by the compiler. In fact, after testing it see: ``` Hello, World! And reference equality between 'a' and 'b' is: True ``` I used simple and raw empty ("") strings in the test project, and they shared the same reference. This is a project in .NET Framework 2.0, so I don't think the problem was in fact "duplicating string objects". What was the special casing they did with the string.Empty that did make it special?
@immortallman3482
@immortallman3482 Жыл бұрын
I think too slow, can somebody tell me which method is better? 😅
@nickchapsas
@nickchapsas Жыл бұрын
It's the same thing but you might need to use the constant version in cases where fields are not acceptable like switches or default method parameter values.
@DimasMessias-kl4ic
@DimasMessias-kl4ic 7 ай бұрын
You have a thing for the number "69" hahaha
@PatricSjoeoe
@PatricSjoeoe Жыл бұрын
We have stylecop rule for prevent "" and use string.empty
@PankajNikam
@PankajNikam Жыл бұрын
Strings are Interned. Performance shouldn't matter in this context.
@jtoromats
@jtoromats Жыл бұрын
69420? 😂
@ashrasmun1
@ashrasmun1 Жыл бұрын
You C# guys are insane. Why does something like that even matter to you? If you have proper test coverage, you will always have an empty string there with double quotes, and if you somehow don't, you just retype it... Jesus...
@RigelOrionBeta
@RigelOrionBeta Жыл бұрын
Spoken like a true JavaScript dev 😂
@ashrasmun1
@ashrasmun1 Жыл бұрын
@@RigelOrionBeta Funny of you to say that, because I'm mainly a cpp dev :p
3 .NET "Best Practices" I Changed My Mind About
10:16
Nick Chapsas
Рет қаралды 99 М.
Why Startups Hate .NET and C#
10:38
Nick Chapsas
Рет қаралды 242 М.
Countries Treat the Heart of Palestine #countryballs
00:13
CountryZ
Рет қаралды 12 МЛН
100❤️ #shorts #construction #mizumayuuki
00:18
MY💝No War🤝
Рет қаралды 20 МЛН
This is the best way to learn C++ for free
0:40
Mehul - Codedamn
Рет қаралды 347 М.
You Need to Update Your .NET Solution Files!
6:59
Nick Chapsas
Рет қаралды 74 М.
The Right Way to Check for Null in C#
9:35
Nick Chapsas
Рет қаралды 94 М.
The Easiest Scheduling for Your .NET Applications
11:25
Nick Chapsas
Рет қаралды 67 М.
Async JS Crash Course - Callbacks, Promises, Async Await
24:31
Traversy Media
Рет қаралды 1,4 МЛН
Correct String Initialization in C#
8:40
IAmTimCorey
Рет қаралды 31 М.
Why Developers Hate "Clean Code"?
14:39
Nick Chapsas
Рет қаралды 43 М.
Learn GO Fast: Full Tutorial
1:07:53
Alex Mux
Рет қаралды 297 М.
Should you learn C++?? | Prime Reacts
20:29
ThePrimeTime
Рет қаралды 311 М.