This line:
string abc[] = new string[3];
creates a non-null, non-empty array (it is of size 3 and contains 3 null references).
So of course IsNullOrEmpty() is returning false.
Perhaps you want to also check if the array contains only null references? You could do so like this:
public static bool IsNullOrEmpty<T>(T[] array) where T: class
{
if (array == null || array.Length == 0)
return true;
else
return array.All(item => item == null);
}
Answer from Matthew Watson on Stack OverflowThis is my code:
public class X
{
public static string[] UnescChar = new string[255];
//===========================================================
public static string ConvertToValidString(string InString)
{
Regex re = new Regex("&#[xX][A-F0-9a-z][A-F0-9a-z];");
string NString = string.Empty;
int i = 0;
// Empty String ? return Empty String...
if (InString.Length > 0)
NString = InString;
else
return string.Empty;
//Condition to fill Char Table
if (UnescChar == null ) //PROBLEM!
{
// Populate Esc Char Table
for (i = 0; i <= 254; i++)
UnescChar[i] = Convert.ToChar(i).ToString();
}I am defining an array of if size [255], I need to evaluate the array to see if it is null/initialized. My problem is that since I defined the arrays size, technically the array is not empty, but all its slots are null. Is there a way for me to evaluate if the array is null/initialized? If it is null, I need to populate the table. If not, skip the for loop.
I found something that might help here, http://forums.asp.net/t/1173032.aspx/1 but it threw an error and left me more confused.
The error is:
Error 7 Could not find an implementation of the query pattern for source type 'string[]'. 'Where' not found.
Do you actually gain anything by using an array instead of, say, a List? With C#, Lists are indexable (have the [] operator defined), and you can just call List.empty to see if there are any entries... Though I guess you could just do that with Count...
if (UnescChar == null ) //PROBLEM!
That's not really a problem. The problem is that you're initializing the array before you need it. Change your code to this:
class X
{
public static string[] UnescChar;
public static string ConvertToValidString(string InString)
{
// ... other crap ....
if (UnescChar == null )
{
UnescChar = new string[255];
...
}
}
Another way to fix your code is to simply build your "Esc Char Table" in the initializer.
public static string[] UnescChar = Enumerable
.Range(0,254)
.Select(i => Convert.ToChar(i).ToString() )
.ToArray();
Now you don't need any of that other code.
checking to see if a place in an array is null - RESOLVED
IsNullOrEmpty equivalent for Array? C# - Stack Overflow
Best Practice to check for null/empty values
Array.IsNullOrEmpty()
There isn't an existing one, but you could use this extension method:
/// <summary>Indicates whether the specified array is null or has a length of zero.</summary>
/// <param name="array">The array to test.</param>
/// <returns>true if the array parameter is null or has a length of zero; otherwise, false.</returns>
public static bool IsNullOrEmpty(this Array array)
{
return (array == null || array.Length == 0);
}
Just place this in an extensions class somewhere and it'll extend Array to have an IsNullOrEmpty method.
Update for 2025: These days you can use the null conditional operator and null coalescing operator to simplify the check inline, e.g. (array?.Length ?? 0) == 0 - the array?.Length part evaluates to the length of the array if it is not null, or null otherwise. The (...) ?? 0 part evaluates to the left hand expression if its value is not null, otherwise zero. By combining the two, you get the length of the array if it is not null, or zero if the array is null. The brackets are required due to operator precedence (?? has lower precedence than ==).
Naturally, you could still wrap this in an extension method:
/// <summary>Indicates whether the specified array is null or has a length of zero.</summary>
/// <param name="array">The array to test.</param>
/// <returns>true if the array parameter is null or has a length of zero; otherwise, false.</returns>
public static bool IsNullOrEmpty(this Array array)
{
return (array?.Length ?? 0) == 0;
}
For correct behaviour with very large arrays, one should use LongLength instead of Length.
Since it came up in the comments, it is important to note that calling an extension method on a null object is 100% valid and will not throw a NullReferenceException in the same way that calling an instance method on a null object would. This is because extension methods are implemented as static methods, so when you type array.IsNullOrEmpty() what you're really getting under the hood is MyExtensionMethods.IsNullOrEmpty(array), which operates in exactly the same way as the String.IsNullOrEmpty() static method.
With Null-conditional Operator introduced in VS 2015, the opposite IsNotNullOrEmpty can be:
if (array?.Length > 0) { // similar to if (array != null && array.Length > 0) {
but the IsNullOrEmpty version looks a bit ugly because of the operator precedence:
if (!(array?.Length > 0)) {
if (list == null || list.Count == 0) return; if ((list?.Count ?? 0) == 0) return; if (!(list?.Count > 0)) return; // Something else?
Between these three, I'd prefer the first one. Although I wish there was a nicer way to use the null propagator here (is there?!?)...