title: Assigning specific type object into an generic array
body: <p>I have a generic type called <code>Vector&lt;T&gt;</code>, I created it as so, cause the <code>T</code> might be <code>float</code> or <code>Complex</code> :</p>
  
  <pre><code>public class Vector&lt;T&gt;
      {
          #region Properties
          public ulong Length
          {
              get
              {
                  return _length;
              }
          }
          public VectorType VectorType
          {
              get
              {
                  return _vectorType;
              }
          }
  
          #endregion
  
          #region Indexers
          public T this[ulong index]
          {
              get
              {
                  return _data[index];
              }
              set
              {
                  _data[index] = value;
              }
          }
  
          #endregion
  
          #region Constructors
  
          public Vector(VectorType vectorType, T[] data)
          {
              if (!((data is float[]) || (data is Complex[])))
              {
                  throw new InvalidDataException("Data must be array of float or array of Complex");
              }
              _data = new T[_length = (ulong)data.Length];
              for (ulong i = 0; i &lt; _length; i++)
              {
                  _data[i] = data[i];
              }
              _vectorType = vectorType;
          }
  
  
          public Vector(VectorType vectorType, Vector&lt;T&gt; vector)
          {
              _data = new T[_length = vector.Length];
              for (ulong i = 0; i &lt; _length; i++)
              {
                  _data[i] = vector[i];
              }
              _vectorType = vectorType;
          }
  
          #endregion
  
          #region Methods
  
              //Unity Matrix, this vector has 1/N everywhere
              public static Vector&lt;float&gt; e(VectorType vectorType, ulong length)
              {
                  var data = new float[length];
                  for (ulong i = 0; i &lt; length; i++)
                  {
                      data[i] = (float)1 / length;
                  }
                  var vectorE = new Vector&lt;float&gt;(vectorType, data); 
                  return vectorE;
              }
  
              public float Sum()
              {
                  float sum = 0;
                  if (_data is float[])
                  {
                      sum = (_data as float[]).Sum();
                  }
                  else
                  {
                      if (_data is Complex[])
                      {
                          for (ulong i = 0; i &lt; _length; i++)
                          {
                              sum += (float)
                                  Math.Sqrt(Math.Pow((_data[i] as Complex?).Value.Real, 2) +
                                            Math.Pow((_data[i] as Complex?).Value.Imaginary, 2));
                          }
                      }
                  }
                  return sum;
              }
  
              public bool CheckIfSochasitc()
              {
                  return Math.Abs(Sum() - 1) &lt; float.Epsilon;
              }
  
              public void Normalize()
              {
                  var sum = Sum();
  
                  if (_data is float[])
                  {
                      for (ulong i = 0; i &lt; _length; i++)
                      {
                          float x = ((float) _data[i])/sum;
                          _data[i] =  (T)x;
                      }
                  }
  
              }
  
          #endregion
  
          #region Operators
          //I omitted the code inhere to avoid overload
          #endregion
  
          #region Fields
  
              private  ulong _length;
              private readonly VectorType _vectorType;
              private T[] _data;
  
          #endregion
      }
  
      public enum VectorType
      {
          Row,Column        
      }
  </code></pre>
  
  <p>My problem is that I have a generic array (if I can call it so) :</p>
  
  <pre><code>private T[] _data;
  </code></pre>
  
  <p>And I have the Normalize() method:</p>
  
  <pre><code>public void Normalize()
            {
               var sum = Sum();
               if (_data is float[])
               {
                  for (ulong i = 0; i &lt; _length; i++)
                  {
                      //Here is the problem
                      _data[i] =  ((_data[i] as float?) / sum);
                  }
               }
            }
  </code></pre>
  
  <p>This doesn't work saying <code>can't cast float to T</code> tried to search but couldn't find helpful aide, any clarification I'd be thankful.                       </p>
  
  <p><strong>Update :</strong></p>
  
  <p>The Sum() method always returns a float</p>