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
- if ( s == “” ) if (n == "")
- if ( s == string.Empty ) if (n == 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 (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.
Tags: check empty strings Check leere Saiten , empty strings leere Saiten

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






























Free Email Subscription


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 ().