Home Hjem » » Dot Net Dot Net

5 Wrong ways to check empty strings 5 Wrong måter å se tomme strenger

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. Det er en av vanlig feil at folk sammenligne en streng med "" eller String.Empty i VB.Net eller C # å finne sin tomt. Here are few examples. Her er noen eksempler.

// C# Wrong Ways / / C # Wrong Ways

  1. if ( s == “” ) if (s == "")
  2. if ( s == string.Empty ) if (s == string.Empty)
  3. if ( s.Equals(””) ) if (s.Equals ( ""))
  4. if ( s.Equals ( String.Empty) if (s.Equals (String.Empty)
  5. if ( string.Equals(s,””) if (string.Equals (s, "")
  6. if ( string.Equals ( s,String.Empty )) if (string.Equals (s, String.Empty))

So what's the correct way to do it ? Så hva er den riktige måten å gjøre det? Check for length too. Se etter lengden også.

// [ C# ] Correct Way / / [C #] Correct Way

if ( s.Length == 0 ) if (s.Length == 0)

This is in continuation of our last post on Dette er i fortsettelsen av vår siste innlegget på Check for length too when you check for null strings in VB.Net and C# Se etter lengden også når du søker etter null strenger i VB.Net og C #

Read below for the right approach. Les nedenfor for riktig tilnærming.

  • Ryan Heaney Ryan Heaney , March 27, 2008: , 27. mars 2008:

    I agree with Shahar. Jeg er enig med Shahar. string.IsNullOrEmpty is the best, managed way to test. string.IsNullOrEmpty er den beste, styrt måte å teste. It's static and guaranteed not to throw a NullReferenceException. Det er statiske og garantert ikke til å kaste en NullReferenceException. You can't say that about .Length. Du kan ikke si det om. Lengde.

    If you look at the disassembly of string.IsNullOrEmpty you will see what it does. Hvis du ser på demontering av string.IsNullOrEmpty vil du se hva det gjør.
    .method public hidebysig static bool IsNullOrEmpty(string 'value') cil managed . metoden offentlige hidebysig static bool IsNullOrEmpty (string "verdi") cil administrert
    { (
    .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 eksempel 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#): Dette betyr (C #):
    public static bool IsNullOrEmpty(string value) public static bool IsNullOrEmpty (strengverdi)
    { (
    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. Nå mens du kan utføre denne operasjonen på egen hånd, hele poenget med. NET Framework er det faktum at dette er forvaltet kode. 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. Hvis du av annen grunn Microsoft bestemmer seg for å endre gjennomføring av strengen klassen slik at det er en annen sak som det er tomt, vil det endre dens gjennomføring her. And you would never have to worry about your code breaking due to an underlying change. Og du vil aldri trenger å bekymre deg koden bryte på grunn av en underliggende endring.

    So, in short, use string.IsNullOrEmpty(). Så, kort sagt, bruk string.IsNullOrEmpty ().

Tagged with: Merket med:

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanΜεταφράστε στα ελληνικά/GreekПереведите к русскому/RussianOversetter til Norsk/NorwegianÖversätta till Svensk/Swedishहिन्दी अनुवाद करने के लिए/Hindi
Tradueix al català/CatalanTulkot uz latviešu/LatvianPreložiť do slovenčiny/SlovakVertaal aan het Nederlands/Dutchترجمة الى العربية/ArabicTraduzca al Español/SpanishTraduisez au Français/French
Traduca ad Italiano/ItalianTraduza ao Português/Portuguese日本語に翻訳しなさい /Japanese한국어에게 번역하십시오/Korean中文翻译/Chinese Simplified中文翻译/Chinese TraditionalПереклад на українську/Ukrainian

SMS abonnerePrint This Post

Posted on 6th April 2008 by Skrevet av 6 april 2008 etter Ashish Mohta Ashish Mohta , A tech blogger who writes about solving day to day problems of people who use computer. , A tech blogger som skriver om løse daglige problemer for personer som bruker datamaskinen. He also writes on How to use the applications like Office, PC tips, Online tools,Browsers and more. All posts by Han skriver om hvordan du bruker programmer som Office, PC tips Online verktøy, Nettlesere og mer. Alle innlegg av Ashish Mohta Ashish Mohta | Connect with me @ | Kontakt med meg @ Twitter Twitter | | Linkedin Linkedin | | Facebook Facebook | | Stumble Snuble | Need more help? | Trenger du mer hjelp? Ask your Questions at our Still dine spørsmål i vår Support Center Support Center

Leave your response! Forlat ditt svar!

Be nice. Vær hyggelig. Keep it clean. Holde den ren. Stay on topic. Stay on topic. No spam. Ingen spam.

You can use these tags: Du kan bruke disse kodene:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>