Quantcast
Channel: Is there a better alternative than this to 'switch on type'? - Stack Overflow
Viewing all articles
Browse latest Browse all 33

Is there a better alternative than this to 'switch on type'?

$
0
0

Seeing as C# can't switch on a Type (which I gather wasn't added as a special case because is relationships mean that more than one distinct case might apply), is there a better way to simulate switching on type other than this?

void Foo(object o){    if (o is A)    {        ((A)o).Hop();    }    else if (o is B)    {        ((B)o).Skip();    }    else    {        throw new ArgumentException("Unexpected type: "+ o.GetType());    }}

Viewing all articles
Browse latest Browse all 33

Latest Images

Trending Articles





Latest Images