I have a big collection of objects of different built-in types, e.g. int, bool[], double[] etc.
On object M I wanted to perform an operation MyMethod once with each element of the collection. However, I needed to perform different operation with arrays and different with single values.
Firstly, I tried:
public void MyMethod<T>(T value)
public void MyMethod<T>(T[] array)
but then, the first method was applied with every element of the collection, including arrays.
My next try was:
public void MyMethod<T>(T value) where T : struct
public void MyMethod<T>(T[] array)
And it had a following effect when I tried to call that method:
Error 8 The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'MyMethod(T)'
It seems like the compiler doesn't see the MyMethod(T[] array) method. Where am I wrong?
Finally, I provided an auxiliary method:
public void MyAux<T>(T value) {
if (value.GetType().IsArray) {
this.MyMethodForArray(value);
}
else {
this.MyMethodForSingleValue(value);
}
but then I got an error:
Error 8 The type arguments for method 'MyMethodForArray(T[])' cannot be inferred from the usage. Try specifying the type arguments explicitly.
How to manage this problem elegantly?