title: Generic type in C# not working on reading full binary file (Unhandled exception)
body: <p>I am trying to read a binary file taken at sole argument and i have just switched to c# from c++. 
  I know what the problem is:</p>
  
  <p>The problem is when i read the binary file it comes byte by byte (8 bits) and that i save into a variable (this variable is "symbol" in my code below) that may be of int/long/short on 32/64 bit architecture. 
  Suppose if i have variable of "T" type.Let's say <code>T symbol</code> and i read and store binary file in a generic type <code>"T"</code> variable called <code>"processingValue"</code> like this</p>
  
  <pre><code> byte[] bytes = stream.ReadBytes(size);  
   T processingValue = converter(bytes,0);  
  </code></pre>
  
  <p>I create the object of the class "ConstructorClass" (whose definition contains the above two lines of code is) like this :</p>
  
  <pre><code>constructorClass &lt;uint&gt; ObjSym = new ConstructorClass &lt;uint&gt; (args, BitConverter.ToUInt32);
  </code></pre>
  
  <p>And <code>definition</code> of <code>constructor</code> is like this :</p>
  
  <pre><code>public ConstructorClass(string[] args, Func &lt;byte[], int,T&gt; converter)
  {
   //those two lines of code
  } 
  </code></pre>
  
  <p>The problem occurs when if the length of the Binary file read is not the multiple of the data type i have chosen in constructorClass object creation.
  I mean suppose for <code>"uint"</code> case it works fine because it is of <code>4 bytes</code> and the binary file length is multiple of 4 byte.
  But suppose is i take <code>"short"</code>(<code>Int16</code>) then it gives Unhandled Exception because the it's size is 2 bytes and the length of Binary file is not equal to the multiple of <code>"2 bytes"</code>.So what happen on the last read of the binary file the compiler don't find and data according to the size of "short". (I mean short needs 16 bits but may be the bits found on the last read were only <code>8</code> or may be less or more but not 16 bits, so it gives Unhandled exception).</p>
  
  <p>Here i tried it on "long" (because on "int" it works fine) <code>Huffman &lt;long&gt; ObjSym = new Huffman &lt;long&gt; (args, BitConverter.ToInt64);</code>
  Then i got this :</p>
  
  <pre><code>Unhandled Exception: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
    at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in &lt;filename unknown&gt;:0 
    at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in &lt;filename unknown&gt;:0 
    at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in &lt;filename unknown&gt;:0 
    at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in &lt;filename unknown&gt;:0 
  [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
    at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in &lt;filename unknown&gt;:0 
    at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in &lt;filename unknown&gt;:0 
    at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in &lt;filename unknown&gt;:0 
    at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in &lt;filename unknown&gt;:0 
  </code></pre>
  
  <p>My code is : (please see below the definition and call to this constructor definition by creating object, also it's parameter which could be uint/long/short etc.)
  //This is  constructor definition (class is Huffman )</p>
  
  <pre><code>public Huffman(string[] args, Func &lt; byte[], int, T &gt; converter)  
  {
      front = null;
      int size = Marshal.SizeOf(typeof (T));
      Console.WriteLine("Size: {0}  ", size);
      using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) 
      {
          while (stream.BaseStream.Position &lt; stream.BaseStream.Length)
          {
              byte[] bytes = stream.ReadBytes(size);
              T processingValue = converter(bytes, 0);
              {
                  Node pt, temp;
                  bool is_there = false;
                  pt = front;
                  while (pt != null) 
                  {
                      if (pt.symbol.Equals(processingValue)) //Here i find out the frequency (freq) of each symbol (by frequency means count of number of time the symbol repeats in file but it works fine.)
                      {
                          pt.freq++;
                          is_there = true;
  
                          break;
                      }
                      temp = pt;
                      pt = pt.next;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This is my main function where i create object for constructor </p>
  
  <pre><code>public static void Main(string[] args) 
  {
      Huffman &lt;uint&gt; ObjSym = new Huffman &lt;uint&gt; (args, BitConverter.ToUInt32);
  }
  </code></pre>
  
  <p>This is my constructor class </p>
  
  <pre><code>public class Huffman &lt; T &gt; where T: struct, IComparable &lt; T &gt; , IEquatable &lt; T &gt; 
  {
      public int data_size, length, i, is_there;
       public class Node
      {
          public Node next;
          public T symbol;
          public int freq;
      }
      public Node front, rear;
  }
  </code></pre>
  
  <p>And the bianry file read contains data like this :</p>
  
  <pre><code>hp@ubuntu:~/Desktop/Huf_pointer$ xxd -b out.bin 
  0000000: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  0000006: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  000000c: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  0000012: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  0000018: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  000001e: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  0000024: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  000002a: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  0000030: 00000000 00000000 00000000 00000000 00000000 00000000  ......
  .........//Here also there is similar kind of data    ................
  00008ca: 00010011 00010011 00010011 00010011 00010011 00010011  ......
  00008d0: 00010011 00010011 00010011 00010011 00010011 00010011  ......
  00008d6: 00010011 00010011 00010011 00010011 00010011 00010011  ..... 
  </code></pre>
  
  <p>And from this kind of binary file i have to find the count of the number of times each symbol repeats (represented by my <code>freq</code> variable in code.)<strong>But that works fine i am not writing the code for that in my given below code to present my question shortly i have just given the necessary part of code only</strong>.</p>
  
  <p>I think the only solution for this is to fill the last read bytes with <code>"0"</code> (I mean add padding) to make it <strong>multiple of the data type we have chosen (uint/long/short etc.)</strong>. This could be the only way to avoid it the Unhandled exception. <strong>Could any one please help me in writing code for making last read byte as multiple of size of data type ?</strong>
  Or if you have more efficient way to avoid this Unhandled exception ? If not then could you please help me in achieving me by my idea ? I would really appreciate small piece of code for <strong>adding padding</strong>, Thanks.   </p>