Home Heim » » Dot Net Dot Net

5 Wrong ways to check empty strings 5 Falsche Möglichkeiten zu prüfen, leere Saiten

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. Es ist eins der gemeinsamen Fehler, dass die Menschen vergleichen einen String mit "" oder String.Empty in VB.Net oder C # zu finden, ihre leer. Here are few examples. Hier sind einige Beispiele.

// C# Wrong Ways / / C # Falsche Wege

  1. if ( s == “” ) if (n == "")
  2. if ( s == string.Empty ) if (n == 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 ? Also, was ist der richtige Weg, dies zu tun? Check for length too. Überprüfen Sie für die Länge zu.

// [ C# ] Correct Way / / [C #] richtigen Weg

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

This is in continuation of our last post on Dies ist in Fortsetzung unserer letzten Beitrag auf Check for length too when you check for null strings in VB.Net and C# Überprüfen Sie für die Länge zu überprüfen, wenn Sie für null Zeichenketten in VB.Net und C #

Read below for the right approach. Lesen Sie unten für den richtigen Ansatz.

  • Ryan Heaney Ryan Heaney , March 27, 2008: , 27. März, 2008:

    I agree with Shahar. Ich stimme mit Shahar. string.IsNullOrEmpty is the best, managed way to test. string.IsNullOrEmpty ist die beste, verwaltet Weg zu testen. It’s static and guaranteed not to throw a NullReferenceException. Es ist statisch und garantiert nicht werfen ein NullReferenceException. You can’t say that about .Length. Man kann nicht sagen, dass etwa. Länge.

    If you look at the disassembly of string.IsNullOrEmpty you will see what it does. Wenn man sich die Demontage der string.IsNullOrEmpty Sie werden sehen, was sie tut.
    .method public hidebysig static bool IsNullOrEmpty(string ‘value’) cil managed . Methode öffentlichen hidebysig statische bool IsNullOrEmpty (string 'value') cil verwaltet
    { (
    .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 Beispiel 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#): Diese Übersetzung auf (C #):
    public static bool IsNullOrEmpty(string value) public static bool IsNullOrEmpty (String-Wert)
    { (
    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. Jetzt, während Sie können diesen Vorgang auf Ihrem eigenen, die ganze Nummer des. NET Framework ist die Tatsache, dass es sich hierbei um verwaltetem Code. 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. Wenn aus irgendeinem Grund Microsoft beschließt, die Umsetzung der String-Klasse, so dass es einen anderen Fall, in dem er leer ist, so wäre seine Umsetzung hier. And you would never have to worry about your code breaking due to an underlying change. Und Sie würden niemals Sorgen um Ihren Code brechen aufgrund eines zugrunde liegenden ändern.

    So, in short, use string.IsNullOrEmpty(). Also, kurz gesagt, verwenden Sie string.IsNullOrEmpty ().

Tags: ,

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanПереведите к русскому/RussianΜεταφράστε στα ελληνικά/GreekVertaal aan het Nederlands/Dutchترجمة الى العربية/Arabic中文翻译/Chinese Traditional中文翻译/Chinese Simplified한국어에게 번역하십시오/Korean日本語に翻訳しなさい /JapaneseTraduza ao Português/PortugueseTraduca ad Italiano/ItalianTraduisez au Français/FrenchTraduzca al Español/Spanish
SMS abonnierenPrint This Post

Posted on 6th April 2008 by Geschrieben am 6. April 2008 von Ashish Mohta Ashish Mohta , A tech blogger who writes about solving day to day problems of people who use computer. , A-Tech-Blogger schreibt über die Lösung von einem Tag auf den Problemen der Menschen, die Computer verwenden. He also writes on How to use the applications like Office, PC tips, Online tools,Browsers and more. All posts by Er schreibt auch für die Verwendung der Anwendungen wie Office, PC-Tipps, Online-Tools, Browser und vieles mehr. Alle Beiträge von Ashish Mohta Ashish Mohta | Connect with me @ | Verbinden Sie mit mir! @ Twitter Twitter | | Linkedin LinkedIn | | Facebook Facebook | | Stumble Stumble | Need more help? | Benötigen Sie weitere Hilfe? Ask your Questions at our Stellen Sie Ihre Fragen in unserem Support Center Support Center




Leave your response! Lassen Sie Ihre Antwort!

Be nice. Seien Sie nett. Keep it clean. Halten Sie es sauber. Stay on topic. Bleiben Sie beim Thema. No spam. Kein Spam.

You can use these tags: Sie können diese Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>