Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
how to check string array is Null or Empty?
Posted 5-Aug-11 22:37pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

if (myArray == null)
   System.Console.WriteLine("array is not initialized");
else if (myArray.Length < 1)
   System.Console.WriteLine("array is empty");
Something like that.
Mika Wendelius at 6-Aug-11 4:48am
You have a faster keyboard :) my 5
SAKryukov at 6-Aug-11 15:15pm
Thank you, Mika.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

might helps,
static void Main(string[] args)
    string[] myStringArray = null;
    if (IsNullOrEmpty(myStringArray))
        Console.WriteLine("Null or Empty");
static bool IsNullOrEmpty(string[] myStringArray)
    return myStringArray == null || myStringArray.Length < 1;
also, you can make IsNullOrEmpty as a extension method Smile | :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If you're using an array, you can check it's length[^]
On the other hand if you're using simply a string, you can check IsNullOrEmpty[^]
SAKryukov at 6-Aug-11 15:13pm
You did not mention comparison array with null. Also, you should understand that string is not an array of character or whatever, it is not an array at all. It is immutable and implementing value semantics, also, it uses intern pool, which is not trivial thing.

OP asks about array of string and nothing about its content. Strings can be anything.
I voted 4 anyway.
See my solution here
Mika Wendelius at 6-Aug-11 16:11pm
You're right about the array and I did forget about the null check.
I wanted to add the null/empty string test even though the OP asked about the array because the way the question was formulated made me wonder if he's using string type instead of an array (referral to empty).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 380
1 Gihan Liyanage 198
2 Sergey Alexandrovich Kryukov 150
3 ChauhanAjay 120
4 BotCar 105
0 Sergey Alexandrovich Kryukov 9,056
1 OriginalGriff 7,981
2 CPallini 2,613
3 Richard MacCutchan 2,121
4 Abhinav S 1,928

Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 6 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100