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 Overflow
🌐
Reddit
reddit.com › r/learnprogramming › [c#] how can i check if an array (of defined size) is empty?
r/learnprogramming on Reddit: [C#] How can I check if an array (of defined size) is empty?
September 26, 2011 -

This 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.

Discussions

checking to see if a place in an array is null - RESOLVED
} but how can i check if a particular slot in an array is empty or even exists? for example (this is wrong but gets across what i'm asking): ... The operator == is undefined for the argument types(s) int, null What is the proper way to check to see if is [1] even exists? More on forum.processing.org
🌐 forum.processing.org
May 20, 2013
IsNullOrEmpty equivalent for Array? C# - Stack Overflow
Is there a function in the .NET library that will return true or false as to whether an array is null or empty? (Similar to string.IsNullOrEmpty). I had a look in the Array class for a function su... More on stackoverflow.com
🌐 stackoverflow.com
Best Practice to check for null/empty values
Then the If function is used to ... value or the empty string. All that replies your existing if-else. If Properties("DisplayName") is actually an array but is empty then the above code may still fail. You should test that scenario. 3 comments Show comments for this answer Report a concern ... It is an array and I will have to try that "if" method, have not seen that before, I appreciate that contribution. ... The above code works with null or empty values ... More on learn.microsoft.com
🌐 learn.microsoft.com
1
0
January 12, 2023
Array.IsNullOrEmpty()
You switched accounts on another tab or window. Reload to refresh your session. ... In corefx the method string.IsNullOrEmpty as a replacement to checking if a string is null and has a length of 0. Maybe we should have a similar API for Array that is implemented in the exact same way for string More on github.com
🌐 github.com
11
February 12, 2016
🌐
Unity
discussions.unity.com › unity engine
How to check for empty array elements (C#)? - Unity Engine - Unity Discussions
July 18, 2010 - Could someone give me a hint please how I can check what elements of an array are empty and / or still unassigned? if (myArray[2] == null) does not work (error message “Unreachable code detected”) :shock: And how can I set any used elements of this array back to an empty / undefined state? ...
🌐
Processing Forum
forum.processing.org › topic › checking-to-see-if-a-place-in-an-array-is-null
checking to see if a place in an array is null - RESOLVED
May 20, 2013 - the array element exists but it is null The code below tests for all three possibilities - comment out some of the lines 3-5 to try different arrays. diffenerent lines ... bizarre. i had some time to work on this little problem/lesson this morning and still had v.k.'s example loaded and it works fine now. as does pauline's. not really sure why now and not yesterday. maybe the first thing to do before posting is restarting Processing (and/or my computer).
🌐
C# Corner
c-sharpcorner.com › article › how-to-check-if-an-array-is-empty-in-c-sharp
How To Check If An Array Is Empty In C#
February 2, 2023 - In this article, we will explore various methods to determine if an array is empty in C#. The Length property, Count() extension method of LINQ or the IsNullOrEmpty() method can be used to check if the array is empty.
Top answer
1 of 10
81

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.

2 of 10
47

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)) {
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1160345 › best-practice-to-check-for-null-empty-values
Best Practice to check for null/empty values - Microsoft Q&A
January 12, 2023 - Then the If function is used to return either the (non-nothing) value or the empty string. All that replies your existing if-else. If Properties("DisplayName") is actually an array but is empty then the above code may still fail. You should test that scenario. 3 comments Show comments for this answer Report a concern ... It is an array and I will have to try that "if" method, have not seen that before, I appreciate that contribution. ... The above code works with null or empty values in an array.
🌐
Meziantou's blog
meziantou.net › checking-if-a-collection-is-empty-in-csharp.htm
Checking if a collection is empty in C# - Meziantou's blog
January 29, 2024 - In this post, I describe how to check if a collection is empty in C#, whatever the type of the collection
Find elsewhere
🌐
GitHub
github.com › dotnet › runtime › issues › 16359
Array.IsNullOrEmpty() · Issue #16359 · dotnet/runtime
February 12, 2016 - In corefx the method string.IsNullOrEmpty as a replacement to checking if a string is null and has a length of 0. Maybe we should have a similar API for Array that is implemented in the exact same way for string This has the same associa...
Published   Feb 12, 2016
🌐
Turbo360
turbo360.com › home › blog › logic app best practices, tips, and tricks: #29 how to validate if an array is empty
How to validate if an Array is empty in Logic Apps
May 23, 2023 - Another regular validation in our business process is to validate if an Array of objects is null or empty. This is relatively easy to accomplish in programming languages like C# using a simple condition:
🌐
Quora
quora.com › How-do-I-check-which-array-index-is-empty-and-then-refill-that-index-in-C
How to check which array index is empty and then refill that index in C# - Quora
Answer (1 of 2): Arrays in C# are never “empty” but if the declared type of the array is an object, then the member at any given index might be null rather than an actual object.
🌐
Quora
quora.com › How-do-you-check-if-a-string-array-is-empty
How to check if a string array is empty - Quora
Answer (1 of 2): In Java, you can check if a string array is empty by using the [code ]length[/code] property of the array, which returns the number of elements in the array. Here's an example: [code]Copy codeString[] myArray = new String[0]; ...
🌐
Medium
sheldonrcohen.medium.com › avoiding-null-the-case-for-returning-empty-lists-or-arrays-in-c-6b51648ea5ca
Avoiding null: The Case for Returning Empty Lists or Arrays in C# | by Sheldon Cohen | Medium
September 2, 2024 - The potential performance cost of creating empty collections can be avoided by returning singleton instances of empty arrays or lists. This approach can save you a lot of time and frustration in the long run. No more unnecessary null checks or unexpected NullReferenceException!
🌐
CodeProject
codeproject.com › Questions › 237141 › how-to-check-string-array-is-Null-or-Empty
how to check string array is Null or Empty?
May 21, 2022 - Do not try and find the page. That’s impossible. Instead only try to realise the truth - For those who code; Updated: 1 Jul 2007
🌐
Unity
forum.unity.com › unity engine
Check if entire array is empty
November 26, 2018 - Hi guys How do I check if an entire array is empty? This is one solution I tried but I only want it to count once. for (int i = 0; i < essenceArray.Length; i++) { if (essenceArray[i] != null) …
🌐
Unity
discussions.unity.com › unity engine
Checking if an Array is null? - Unity Engine - Unity Discussions
December 28, 2018 - I haven an array Transform[] PatrolRoute; I notice it doesn’t allow me to do this below. if(PatrolRoute){ } but it does let me do this if(PatrolRoute != null){ } Can someone explain that to me? And will != null…
🌐
Extensionmethod
extensionmethod.net › csharp › array › isnullorempty
IsNullOrEmpty - csharp - ExtensionMethod.NET
November 21, 2023 - Extensionmethod IsNullOrEmpty. Indicates whether the specified array is null or empty (has no items). Returns true if the array parameter is null or empty; otherwise, false.. Authored by jepozdemir
🌐
Code Maze
code-maze.com › home › how to declare an empty array in c#
How to Declare an Empty Array in C# - Code Maze
April 6, 2023 - Arrays are used to store multiple variables of the same type. Sometimes our solution design may require the use of an empty array. For example, as an initial value for a dynamic-sized collection, or in cases where a method would usually return a list of results, an empty array could indicate no results.
🌐
GeeksforGeeks
geeksforgeeks.org › c# › c-sharp-string-isnullorempty-method
C# | IsNullOrEmpty() Method
March 25, 2025 - // C# program to demonstrate the use of // String.IsNullOrEmpty() method using System; class Geeks { static void Main(string[] args) { // Empty String string s1 = ""; // Non-Empty String string s2 = "Geek"; // Check for null or empty string ...