This may be a basic question but why can’t I cast a generic type back to it’s original type when passing a list of value types into a generic method ?
IList<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
Inverse<int>(list);
public void Inverse<T>(IList<T> list)
{
for (i = 0; i <= list.Count / 2; i++)
{
int a = list[i] as Int16; //=> does not work
int b = (int)list[i]; //=> does not work either
}
}
I’d expect it not to, you’ve completely missed the point of a generic method there as you’re assuming the types in the IList are ‘int’.
If you did:
T a = (T)list[i]then it would work.