Quantcast
Channel: Is there a better alternative than this to 'switch on type'? - Stack Overflow
Browsing index pages (33 articles)

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

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...

View Article


Answer by Jon Skeet for Is there a better alternative than this to 'switch on...

One option is to have a dictionary from Type to Action (or some other delegate). Look up the action based on the type, and then execute it. I've used this for factories before now.

View Article


Answer by Pablo Fernandez for Is there a better alternative than this to...

Create a superclass (S) and make A and B inherit from it. Then declare an abstract method on S that every subclass needs to implement.Doing this the "foo" method can also change its signature to Foo(S...

View Article

Answer by Zachary Yates for Is there a better alternative than this to...

With C# 7, which shipped with Visual Studio 2017 (Release 15.*), you are able to use Types in case statements (pattern matching):switch(shape){ case Circle c: WriteLine($"circle with radius...

View Article

Answer by Jonas Kongslund for Is there a better alternative than this to...

I would eitheruse method overloading (just like x0n), oruse subclasses (just like Pablo), orapply the visitor pattern.

View Article


Answer by Hallgrim for Is there a better alternative than this to 'switch on...

I such cases I usually end up with a list of predicates and actions. Something along these lines:class Mine { static List<Func<object, bool>> predicates; static...

View Article

Answer by sep332 for Is there a better alternative than this to 'switch on...

You should really be overloading your method, not trying to do the disambiguation yourself. Most of the answers so far don't take future subclasses into account, which may lead to really terrible...

View Article

Answer by Marc Gravell for Is there a better alternative than this to 'switch...

After having compared the options a few answers here provided to F# features, I discovered F# to have a way better support for type-based switching (although I'm still sticking to C#).You might want to...

View Article


Answer by plinth for Is there a better alternative than this to 'switch on...

I agree with Jon about having a hash of actions to class name. If you keep your pattern, you might want to consider using the "as" construct instead:A a = o as A;if (a != null) { a.Hop(); return;}B b =...

View Article


Answer by Sunny Milenov for Is there a better alternative than this to...

Create an interface IFooable, then make your A and B classes to implement a common method, which in turn calls the corresponding method you want:interface IFooable{ public void Foo();}class A :...

View Article

Answer by JaredPar for Is there a better alternative than this to 'switch on...

Switching on types is definitely lacking in C# (UPDATE: in C#7 / VS 2017 switching on types is supported - see Zachary Yates's answer). In order to do this without a large if/else if/else statement,...

View Article

Answer by jgarcia for Is there a better alternative than this to 'switch on...

Another way would be to define an interface IThing and then implement it in both classes here's the snipet:public interface IThing{ void Move();}public class ThingA : IThing{ public void Move() {...

View Article

Answer by Paul Batum for Is there a better alternative than this to 'switch...

If you were using C# 4, you could make use of the new dynamic functionality to achieve an interesting alternative. I'm not saying this is better, in fact it seems very likely that it would be slower,...

View Article


Answer by Evren Kuzucuoglu for Is there a better alternative than this to...

Given inheritance facilitates an object to be recognized as more than one type, I think a switch could lead to bad ambiguity. For example:Case 1{ string s = "a"; if (s is string) Print("Foo"); else if...

View Article

Answer by Daniel A.A. Pelsmaeker for Is there a better alternative than this...

With JaredPar's answer in the back of my head, I wrote a variant of his TypeSwitch class that uses type inference for a nicer syntax:class A { string Name { get; } }class B : A { string LongName { get;...

View Article


Answer by nawfal for Is there a better alternative than this to 'switch on...

As Pablo suggests, interface approach is almost always the right thing to do to handle this. To really utilize switch, another alternative is to have a custom enum denoting your type in your...

View Article

Answer by Sergey Berezovskiy for Is there a better alternative than this to...

You can create overloaded methods:void Foo(A a) { a.Hop(); }void Foo(B b) { b.Skip(); }void Foo(object o) { throw new ArgumentException("Unexpected type: "+ o.GetType()); }And cast the argument to...

View Article


Answer by scobi for Is there a better alternative than this to 'switch on type'?

I liked Virtlink's use of implicit typing to make the switch much more readable, but I didn't like that an early-out isn't possible, and that we're doing allocations. Let's turn up the perf a...

View Article

Answer by Edward Ned Harvey for Is there a better alternative than this to...

For built-in types, you can use the TypeCode enumeration. Please note that GetType() is kind of slow, but probably not relevant in most situations.switch (Type.GetTypeCode(someObject.GetType())){ case...

View Article

Answer by jruizaranguren for Is there a better alternative than this to...

This is an alternate answer that mixes contributions from JaredPar and VirtLink answers, with the following constraints:The switch construction behaves as a function, and receives functions as...

View Article
Browsing index pages (33 articles)


Latest Images