C Switch On Type

Posted on

Possible Duplicate:
C# - Is there a better alternative than this to 'switch on type'?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. The syntax for a switch statement in C programming language is as follows −. Dec 24, 2010  Sure. You need to come up with some way to query the information if you're stuck in C03, or use auto in C11 and up. So long as you don't try to return different types within the same instantiation you'll have no problem.

C# doesn't support switching on the type of an object. What is the best pattern of simulating this:

Thanks!

Community
AdamAdam

marked as duplicate by ChrisF, Michael MyersSep 26 '12 at 16:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

I usually use a dictionary of types and delegates.

It's a little less flexible as you can't fall through cases, continue etc. But I rarely do so anyway.

Noctis
9,8553 gold badges32 silver badges69 bronze badges
Mark HMark H
12.5k3 gold badges25 silver badges43 bronze badges

There is a simple answer to this question at Switch case on type c# which use a dictionary of types to look-up a lambda function.

Here is how it might be used

C Switch Data Type

There is also a generalized solution to this problem in terms of pattern matching (both types and run-time checked conditions) at switch / pattern matching idea

Community
cdigginsSwitchcdiggins
12.3k3 gold badges78 silver badges85 bronze badges

Update:This got fixed in C# 7.0 with pattern matching

Old answer:

It is a hole in C#'s game, no silver bullet yet.

You should google on the 'visitor pattern' but it might be a little heavy for you but still something you should know about.

Here's another take on the matter using Linq: http://community.bartdesmet.net/blogs/bart/archive/2008/03/30/a-functional-c-type-switch.aspx

Otherwise something along these lines could help

etc.

gjvdkampgjvdkamp
5,6931 gold badge27 silver badges40 bronze badges

I did it one time with a workaround, hope it helps.

Ghyath SerhalGhyath Serhal

C# 8 Switch

5,7226 gold badges38 silver badges54 bronze badges

I have used this form of switch-case on rare occasion. Even then I have found another way to do what I wanted. If you find that this is the only way to accomplish what you need, I would recommend @Mark H's solution.

If this is intended to be a sort of factory creation decision process, there are better ways to do it. Otherwise, I really can't see why you want to use the switch on a type.

Here is a little example expanding on Mark's solution. I think it is a great way to work with types:

IAbstractIAbstract

Switch C Example

Switch
15.6k12 gold badges69 silver badges120 bronze badges

Not the answer you're looking for? Browse other questions tagged c# or ask your own question.