5 Wrong ways to check empty strings Špatně 5 spôsobov, ako kontrolovať prázdne struny
It is one of the common mistake that people compare a string with “” or String.Empty in VB.Net or C# to find its empty. Je to jedna z častý omyl, že ľudia porovnať reťazec s "" alebo String.Empty v VB.Net alebo C # nájsť svoje prázdny. Here are few examples. Tu je niekoľko príkladov.
// C# Wrong Ways / / C # Zlá Cesty
- if ( s == “” ) if (y == "")
- if ( s == string.Empty ) if (y == string.Empty)
- if ( s.Equals(””) ) if (s.Equals ( ""))
- if ( s.Equals ( String.Empty) if (s.Equals (String.Empty)
- if ( string.Equals(s,””) if (string.Equals (s, "")
- if ( string.Equals ( s,String.Empty )) if (string.Equals (y, String.Empty))
So what's the correct way to do it ? Takže to, čo je správny spôsob, ako to urobiť? Check for length too. Kontrolovať dĺžku taky.
// [ C# ] Correct Way / / [C #] správny spôsob
if ( s.Length == 0 ) if (s.Length == 0)
This is in continuation of our last post on To je v nadväznosti na naše posledný príspevok Check for length too when you check for null strings in VB.Net and C# Kontrolovať dĺžku aj keď kontrolovať prázdne struny v VB.Net a C #
Read below for the right approach. Prečítajte si nižšie o správny prístup.
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |

Posted on 6th April 2008 by Publikované dňa 6. apríla 2008 do Ashish Mohta Ashish Mohta , A tech blogger who writes about solving day to day problems of people who use computer. A tech blogger, ktorý píše o riešenie každodenných problémov ľudí, ktorí používajú počítač. He also writes on How to use the applications like Office, PC tips, Online tools,Browsers and more. All posts by On tiež píše o používaní aplikácií, ako je Office, PC tipy, Online nástroje, prehliadača a ďalšie. Všetky príspevky Ashish Mohta Ashish Mohta | Connect with me @ | Spojte sa so mnou @ Twitter Cvrlikání | | Linkedin Linkedin | | Facebook Facebook | | Stumble Klopýtnutí | Need more help? | Potrebujete ďalšiu pomoc? Ask your Questions at our Opýtajte sa na vaše otázky na naše Support Center Centrum pomoci




























Free Email Subscription
If you look at the disassembly of string.IsNullOrEmpty you will see what it does. Ak sa pozriete na rozmontovanie string.IsNullOrEmpty uvidíte, čo to robí.
.method public hidebysig static bool IsNullOrEmpty(string 'value') cil managed . Metódou verejnej hidebysig statický bool IsNullOrEmpty (string 'value') CIL riadené
{ (
.maxstack 8 . Maxstack 8
L_0000: ldarg.0 L_0000: ldarg.0
L_0001: brfalse.s L_000d L_0001: brfalse.s L_000d
L_0003: ldarg.0 L_0003: ldarg.0
L_0004: callvirt instance int32 System.String::get_Length() L_0004: callvirt instance int32 System.String:: get_Length ()
L_0009: ldc.i4.0 L_0009: ldc.i4.0
L_000a: ceq L_000a: CEQ
L_000c: ret L_000c: ret
L_000d: ldc.i4.1 L_000d: ldc.i4.1
L_000e: ret L_000e: ret
} )
This translates to (C#): To sa premieta do (C #):
public static bool IsNullOrEmpty(string value) verejné statické bool IsNullOrEmpty (string hodnota)
{ (
if (value != null) if (value! = null)
{ (
return (value.Length == 0); return (value.Length == 0);
} )
return true; return true;
} )
Now while you can perform this operation on your own, the whole point of the .NET Framework is the fact that this is managed code. Zatiaľ sa môžete vykonávať túto činnosť na vlastnú päsť, celý bod. NET Framework je skutočnosť, že toto je riadená kód. If for some reason Microsoft decides to change the implementation of the string class such that there is another case in which it is empty, it would change its implementation here. Ak z nejakého dôvodu Microsoft rozhodol zmeniť vykonávanie reťazec triedy také, že tam je iný prípad, v ktorom je prázdna, že by sa zmenili jej realizáciu tu. And you would never have to worry about your code breaking due to an underlying change. A ty by sa nikdy nebude musieť starať o svoj kód prelomenie vďaka fundamentálnej zmeny.
So, in short, use string.IsNullOrEmpty(). Takže v skratke, využívanie string.IsNullOrEmpty ().