5 Wrong ways to check empty strings 5 Mal formas de comprobar cadenas vacías
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 uno de los error común que la gente comparar con una cadena "" o String.Empty o VB.Net en C # para encontrar su vacío. Here are few examples. Aquí están algunos ejemplos.
// C# Wrong Ways / / C # incorrecto Medios
- if ( s == “” ) if (s == "")
- if ( s == string.Empty ) if (s == 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 ? Por lo tanto, ¿cuál es la forma correcta de hacerlo? Check for length too. Compruebe también para la longitud.
// [ C# ] Correct Way / / [C #] forma correcta
if ( s.Length == 0 ) if (s.Length == 0)
This is in continuation of our last post on Esto es en la continuación de nuestro último post sobre Check for length too when you check for null strings in VB.Net and C# Compruebe que la longitud demasiado cuando para comprobar la nula cadenas en VB.Net y C #
Read below for the right approach. Lea a continuación para el enfoque correcto.
Tags: Etiquetas: check empty strings comprobar cadenas vacías , empty strings cadenas vacías

Posted on 6th April 2008 by Publicado el 6 de abril de 2008 por Ashish Mohta Ashish Mohta , A tech blogger who writes about solving day to day problems of people who use computer. , A tecnología blogger que escribe sobre la solución de un día para otro los problemas de las personas que utilizan el ordenador. He also writes on How to use the applications like Office, PC tips, Online tools,Browsers and more. All posts by Él también escribe sobre el uso de aplicaciones como la Oficina, PC consejos, herramientas en línea, Navegadores y mucho más. Todos los puestos de Ashish Mohta Ashish Mohta | Connect with me @ | Conectar conmigo @ Twitter Twitter | | Linkedin LinkedIn | | Facebook Facebook | | Stumble Stumble | Need more help? | ¿Necesita más ayuda? Ask your Questions at our Formula tus preguntas a nuestro Support Center Centro de Soporte
- 10 handpicked Thanksgiving Gift Ideas 10 Día de Acción de Gracias a mano de ideas de regalos
- Thanksgiving Wallpapers and Screensavers Free Download Gracias fondos de escritorio y salvapantallas Descarga gratuita






























Free Email Subscription


If you look at the disassembly of string.IsNullOrEmpty you will see what it does. Si nos fijamos en el desmontaje del string.IsNullOrEmpty verá lo que hace.
.method public hidebysig static bool IsNullOrEmpty(string ‘value’) cil managed . hidebysig método público estático bool IsNullOrEmpty (string 'valor') CIL gestionados
{ (
.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 ejemplo 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#): Esto se traduce en (C #):
public static bool IsNullOrEmpty(string value) public static bool IsNullOrEmpty (valor de cadena)
{ (
if (value != null) if (valor! = 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. Ahora, mientras usted puede realizar esta operación por su cuenta, todo el punto de la. NET Framework es el hecho de que se trata de código administrado. 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. Si por alguna razón, Microsoft decide cambiar la aplicación de la cadena de la clase de tal manera que hay otro caso en el que está vacío, que cambiaría su aplicación aquí. And you would never have to worry about your code breaking due to an underlying change. Y usted nunca tendrá que preocuparse por romper su código debido a un cambio subyacentes.
So, in short, use string.IsNullOrEmpty(). Así que, en definitiva, el uso string.IsNullOrEmpty ().