5 Wrong ways to check empty strings 5 Wrong veidus, kā pārbaudīt tukšs virknes
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. Tā ir viena no kopējā kļūda, ka cilvēki salīdzināt string ar "vai String.Empty in VB.Net vai C #, lai atrastu tās tukšs. Here are few examples. Lūk, daži piemēri.
// C# Wrong Ways / / C # Wrong Veidi
- if ( s == “” ) if (s == "")
- if ( s == string.Empty ) if (s == string.Empty)
- if ( s.Equals(””) ) ja (s.Equals ( ""))
- if ( s.Equals ( String.Empty) ja (s.Equals (String.Empty)
- if ( string.Equals(s,””) ja (string.Equals (s, "")
- if ( string.Equals ( s,String.Empty )) ja (string.Equals (s, String.Empty))
So what's the correct way to do it ? Tātad, kāda ir pareizi to darīt? Check for length too. Check garuma too.
// [ C# ] Correct Way / / [C #] Pareiza Way
if ( s.Length == 0 ) ja (s.Length == 0)
This is in continuation of our last post on Tas ir turpinājums mūsu pēdējā pastu Check for length too when you check for null strings in VB.Net and C# Check garuma pārāk kad jūs pārbaudiet, null virknes, kas VB.Net un C #
Read below for the right approach. Lasīt tālāk par tiesībām pieeju.
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |

Posted on 6th April 2008 by Posted par 6 aprīlis 2008 by Ashish Mohta Ashish Mohta , A tech blogger who writes about solving day to day problems of people who use computer. , A tech blogger, kas raksta par risināšana ikdienas problēmas, kas saistītas ar cilvēkiem, kuri lieto datoru. He also writes on How to use the applications like Office, PC tips, Online tools,Browsers and more. All posts by Viņš arī raksta par to, kā izmantot lietojumiem, piemēram, Office, PC padomus, Online instrumentus, pārlūkprogrammas un vairāk. Visi amatu ar Ashish Mohta Ashish Mohta | Connect with me @ | Savienoties ar mani @ Twitter Čivināt | | Linkedin Linkedin | | Facebook Facebook | | Stumble Paklupt | Need more help? | Vajadzīga palīdzība? Ask your Questions at our Jautājiet pēc mūsu Support Center Support Center




























Free Email Subscription
If you look at the disassembly of string.IsNullOrEmpty you will see what it does. Ja jūs meklējiet pie dēlīšiem string.IsNullOrEmpty jūs redzētu, kas tā dara.
.method public hidebysig static bool IsNullOrEmpty(string 'value') cil managed . metode valsts hidebysig statisko bool IsNullOrEmpty (string "vērtība") cil pārvalda
{ (
.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 piemēram 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#): Tas līdz (C #):
public static bool IsNullOrEmpty(string value) valsts statisko bool IsNullOrEmpty (string vērtība)
{ (
if (value != null) if (value! = null)
{ (
return (value.Length == 0); atgriešanos (value.Length == 0);
} )
return true; atgriezties 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. Tagad, kamēr jūs varētu veikt šo operāciju par savu, viss point of the. NET Framework ir fakts, ka tas tiek pārvaldīts kodu. 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. Ja kāda iemesla dēļ Microsoft nolemj mainīt īstenošanu, string klase tāda, ka tur ir vēl viens gadījums, kad tas ir tukšs, tas varētu mainīties tās īstenošanu šeit. And you would never have to worry about your code breaking due to an underlying change. Un jūs nekad nebūtu jāuztraucas par savu kodu laušanas sakarā ar to, ka notikušo izmaiņu.
So, in short, use string.IsNullOrEmpty(). Tātad, īsi sakot, izmantot string.IsNullOrEmpty ().