1

I have created an array like this

string[] directories = new string[15];

And then I want to do something with it like so

for (int i = 0; i < directories.Length; i++) {
   //code
}

The user can input as many directories in the array as they want, but if they don't put 14 elements in it, the rest of the array is obviously going to be NULL and the for loop doesn't stop untill it reaches the 14th element. How can I make the loop stop at the last directory in the array and not count the NULL?

I've tried this but it returns the following error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

for (int i = 0; i < directories .Length; i++) {
   //code

   string directory = directories[i];

   if (directory.Equals(null)){
      return;
   }

   // more code
}

Thank you and sorry for the lack of experience and bad English.

winter
  • 207
  • 1
  • 4
  • 13
  • Your English is quite good in this post, don't worry :) – GrayCat Nov 20 '19 at 10:08
  • Does this answer your question? [How many elements of array are not null?](https://stackoverflow.com/questions/2391743/how-many-elements-of-array-are-not-null) – xdtTransform Nov 20 '19 at 10:18

5 Answers5

6

Try:.

directories.Count(x => x != null):

If you can always guarantee that after the first null, everything else is null, then the following will work to replace the if statement in your code, and be more efficient than the linq above:

if (directory == null)
    break;

The break keywords prevents any more looping in c#.

If you really want to have a set of values in an array followed by a set of nulls, have you considered using a list rather than an array?

Ross Gurbutt
  • 969
  • 1
  • 8
  • 15
2

If directory is null, you won't be able to call Equals().

Just try directory == null

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
0

A quick and easy way to check for directories (if they are directories and the strings aren't null) would be something like this, using Linq:

namespace FooApp {

    using System.IO;
    using System.Linq;

    class MyFoo {

        public static void Main(string[] args) {

            foreach (var dir in args.Where(a => !string.IsNullOrEmpty(a) && Directory.Exists(a))) {
                // Valid directory. Do as you please
            }

        }

    }
}

EDIT: If you don't want to check if a directory is valid and just want to check if the string is non-null,you can leave Directory.Exists() out.

SimonC
  • 1,547
  • 1
  • 19
  • 43
0

If you can skip index you can use Linq's Where:

foreach (var dir in directories.Where(x => x != null) 
{

}

If you need index of items:

for (int i = 0; i < directories.Length; i++) 
{
    string directory = directories[i];
    if (directory == null)
    {
        return; // Note that rest of items will be ignored. method will end code outside for loop will not be executed
        //break; // exit loop on first null. code outside for loop will be executed
        //continue; // use continue to skip null item and process rest of items. code outside for loop will be executed
    }

    // more code
}
Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
-1

Use a List <string> instead of string[]:

string strDir = "./";
List<string> directories = new List<string>();
directories.Add(strDir);

for (int i = 0; i < directories.Length; i++) {
//code
}

edit: Of course a null-check can be implemented, however in my opinion it is not necessary due to

The user can input as many directories in the array as they want

Pedro Isaaco
  • 404
  • 3
  • 10