As part of an exercise I am trying to define a specific behavior for a generic class when used with a specific type. More precisely, I was wondering if it is possible to define a explicit casting operator for a generic type, i.e. from list<T>
to int[]
No, I know I could simply define a method that does the work, however this is not the goal of the exercise.
Assuming the generic class list<T>
I was trying to define the following explicit casting method
class list<T> {
...
public static explicit operator int[](list<T> _t) where T : System.Int32
{
// code handling conversion from list<int> to int[]
}
}
This doesn't work however. Any ideas on how to make the compiler swallow this?