title: How to pad an array in C#?
body: <p>I am preparing a code that splits a string into arrays of chars and then shuffles each array according to a certain pattern. </p>
  
  <p>I have used some code to split an array to different char arrays according to the "keysize" and it is working fine, except for the last array which might be missing some elements.</p>
  
  <p>If the last array is shorter than other arrays an exception will be thrown because at some point the swapping function might be trying to swap a[0] with a[x] which doesn't exist. </p>
  
  <p>So how can I pad the last array to be the same size?</p>
  
  <p>Here is my splitting code which I found in a question here, where I have edited to use <em>char</em> instead of <em>int</em>.</p>
  
  <pre><code>public static char[][] Split(char[] source, int keysize)
      {
          int fullArrayCount = source.Length / keysize;
          int totalArrayCount = fullArrayCount;
          int remainder = source.Length - (fullArrayCount * keysize);
          if (remainder &gt; 0)
          {
              totalArrayCount++;
          }
          char[][] output = new char[totalArrayCount][];
          for (int i = 0; i &lt; fullArrayCount; i++)
          {
              output[i] = new char[keysize];
              Array.Copy(source, i * keysize, output[i], 0, keysize); // Array.Copy(Original Array,,Target array,"Where to start"0,Howmuch)
          }
          if (totalArrayCount != fullArrayCount)
          {
  
              output[fullArrayCount] = new char[remainder];
             // output[fullArrayCount] = new char[keysize];
              MessageBox.Show((keysize-remainder).ToString());
              Array.Copy(source, fullArrayCount * keysize,output[fullArrayCount], 0, remainder);
  
          }
          return output;
      }
  </code></pre>