I need to validate if my string arrays are null or empty. Following is my code. Both don't work. Though the array is not initialized with any values, it shows as if it contains values. How can I fix it?
string abc[] = new string[3];
first code
if(abc != null)
{
}
second code
if(IsNullOrEmpty(abc))
{
}
public static bool IsNullOrEmpty<T>(T[] array)
{
return array == null || array.Length == 0;
}
Though the array is not initialized with any values it shows as if it contains valueswhere does it show that?bool IsNullOrEmpty(string[] array) { return array == null || array.Any(x => String.IsNullOrEmpty(x)); }. Array elements may benullorString.Empty(if this is what you want to check), array itself can be justnullor 0 length (but not in your code). Feel free to replace.Anywith.All(see MSDN).