title: Is there a way to include null values in a list in C#?
body: <p>I have a text file filled with several lines of data, and I would like to split it into 5 different elements like so..</p>
  
  <p><img src="https://i.stack.imgur.com/XSP2v.jpg" alt="Data"></p>
  
  <p>I am successfully reading in the data and putting it into an array. Now I would like to split each part of the text up into different lists so I can compare the data against one another.</p>
  
  <p>I have currently managed to read in the first 4 elements of each line into their relevant lists but the 5th one is throwing me the error "System.IndexOutOfRangeException" which I can only assume is because the first line it reads in has no value for the 5th element? </p>
  
  <p>So my question is, is there a way to populate null values when writing them to a number of lists? </p>
  
  <p>I've tried manually assigning the size of the array and lists but I still get the same error.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          // Reading in file containing data from BT Code Evaluation sheet (for testing purposes).
          // Each line gets stored into a string array, each element is one line of the data.txt file.
          //String[] lines = System.IO.File.ReadAllLines(@"C:\Users\Ad\Desktop\data.txt");
  
          String[] lines = new String[5] {"monitorTime", "localTime", "actor", "action", "actor2"};
  
          lines = System.IO.File.ReadAllLines(@"C:\Users\Ad\Desktop\data.txt");
  
          char delimiter = ' ';
          List&lt;String&gt; monitorTime = new List&lt;String&gt;();
          List&lt;String&gt; localTime = new List&lt;String&gt;();
          List&lt;String&gt; actor = new List&lt;String&gt;();
          List&lt;String&gt; action = new List&lt;String&gt;();
          List&lt;String&gt; actor2 = new List&lt;String&gt;();
  
          // Foreach loop displays the lines of text in the data file.
          foreach (String line in lines)
          {
              // Writes the data to the console.
              Console.WriteLine(line);
  
              String[] data = new String[5] { "monitorTime", "localTime", "actor", "action", "actor2" };
              data = line.Split(delimiter);
  
              monitorTime.Add(data[0]);
              localTime.Add(data[1]);
              actor.Add(data[2]);
              action.Add(data[3]);
              actor2.Add(data[4]);
          }
  
          foreach (String time in monitorTime)
          {
              Console.WriteLine(time);
          }
  
          foreach (String time in localTime)
          {
              Console.WriteLine(time);
          }
  
          foreach (String name in actor)
          {
              Console.WriteLine(name);
          }
  
          foreach (String actions in action)
          {
              Console.WriteLine(actions);
          }
  
          foreach (String name in actor2)
          {
              if (name != null)
              {
                 Console.WriteLine("UNKNOWN");
              }
              else
              {
                  Console.WriteLine(actor2);
              }
          }
  
          // Creates an empty line between the data and the following text.
          Console.WriteLine("");
          // Displays message in console.
          Console.WriteLine("Press any key to analyse data and create report...");
          Console.ReadKey();
      }
  }
  </code></pre>