I have a problem with designing my function so that it can act differently for different types. My function is used to create List of objects with different types, so it wouldn’t be a problem to create several similar functions, but if it’s possible I’d like to avoid it to make my code a bit shorter:
static const int FIRST_TYPE = 0;
static const int SECOND_TYPE = 1;
static const int THIRD_TYPE = 2;
I use those ints as an argument for the function:
public void foo(int type)
{
List<TypeIDontYetKnow> deserialized;
switch (type)
{
case FIRST_TYPE:
deserialized = new List<A>();
break;
case SECOND_TYPE:
deserialized = new List<B>();
break;
case THIRD_TYPE:
deserialized = new List<C>();
break;
}
}
Is it possible to achieve something like this?
You need a generic method