title: Exception occurred Index was outside the bounds of the array
body: <p>I get the following error when running my code:</p>
  
  <p>Exception occurred Index was outside the bounds of the array.</p>
  
  <p>My code is below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO;
  namespace ASCII
  
  {
      class CharacterFrequency
      {
          private char ch;
          private int frequency;
  
          public char Ch
          {
              get { return ch; }
              set { ch = value; }
          }
          public int Frequency
          {
              get { return frequency; }
              set { frequency = value; }
          }
      }
  
      class Program
      {
          public string InputFileName = "wap.txt";
          public string OutputFileName = "Output.txt";
          public string FilePath = "";
  
          public static SortedDictionary&lt;char, ulong&gt; Count(string stringToCount)
          {
              SortedDictionary&lt;char, ulong&gt; characterCount = new SortedDictionary&lt;char, ulong&gt;();
  
  
              foreach (var character in stringToCount)
              {
                  if (!characterCount.ContainsKey(character)) // added character to dictionary if only character is absent in charactercount
                  {
                      characterCount.Add(character, 1);
                  }
                  else
                  {
                      characterCount[character]++; // increemetned count
                  }
              }
              return characterCount;
          }
  
          static void Main(string[] args)
          {
              Program p = new Program();
              CharacterFrequency obj = new CharacterFrequency();
              StreamWriter streamWriter = new StreamWriter(p.OutputFileName);
  
              try
              {
                  p.InputFileName = args[0]; // get input
                  p.OutputFileName = args[1]; // get output
                  p.FilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + p.InputFileName; // get file path
                  if (File.Exists(p.FilePath)) // checked if file exist
                  {
                      string data = File.ReadAllText(p.FilePath); // read the data in string
                      var count = Program.Count(data); // pass the string to count method
                      foreach (var character in count)
                      {
                          streamWriter.WriteLine(character.Key + "(" + (int)character.Key + ")" + "\t" + character.Value); // write data to output file
                      }
                      streamWriter.Close();
                      Console.ReadLine();
                  }
                  else
                  {
                      Console.WriteLine("Please provide input File"); // if input file absent sent message to user to place input file
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Exception occured " + ex.Message.ToString());
              }
          }
      }
  }
  </code></pre>
  
  <p>The program is supposed to read an ASCII text file and count the number of times each character appears in the text file. It should output a text file with this information and the character frequency objects should be stored in array. Any help would be appreciated. </p>