Answer by user6110389 for Is there a better alternative than this to 'switch...
switch-case-type pattern is one that indicates the need for inversion of control.switch (a){ case type1 t: Action1(t); break; case type2 t: Action2(t); break; case type3 t: Action3(t); break;}is...
View ArticleAnswer by pablocom for Is there a better alternative than this to 'switch on...
Try to go that way:public void Test(BaseType @base){ switch (@base) { case ConcreteType concrete: DoSomething(concrete); break; case AnotherConcrete concrete: DoSomething(concrete); break; }}
View ArticleAnswer by Desmond for Is there a better alternative than this to 'switch on...
With C# 8 onwards you can make it even more concise with the new switch. And with the use of discard option _ you can avoid creating innecesary variables when you don't need them, like this: return...
View ArticleAnswer by PilgrimViis for Is there a better alternative than this to 'switch...
C# 8 pattern matching enhancements made it possible to do it like this. This syntax is more concise.public Animal Animal { get; set; }var animalName = Animal switch{ Cat cat => "Tom", Mouse mouse...
View ArticleAnswer by Chan for Is there a better alternative than this to 'switch on type'?
If you know the class you are expecting but you still don't have an object you can even do this:private string GetAcceptButtonText<T>() where T : BaseClass, new(){ switch (new T()) { case...
View ArticleAnswer by jean-maurice Destraz for Is there a better alternative than this to...
Should work withcase type _:like:int i = 1;bool b = true;double d = 1.1;object o = i; // whatever you wantswitch (o){ case int _: Answer.Content = "You got the int"; break; case double _:...
View ArticleAnswer by alhpe for Is there a better alternative than this to 'switch on type'?
You can use pattern matching in C# 7 or above:switch (foo.GetType()){ case var type when type == typeof(Player): break; case var type when type == typeof(Address): break; case var type when type ==...
View ArticleAnswer by mdimai666 for Is there a better alternative than this to 'switch on...
I use public T Store<T>() { Type t = typeof(T); if (t == typeof(CategoryDataStore)) return (T)DependencyService.Get<IDataStore<ItemCategory>>(); else return default(T); }
View ArticleAnswer by Natalie Perret for Is there a better alternative than this to...
I would create an interface with whatever name and method name that would make sense for your switch, let's call them respectively: IDoable that tells to implement void Do().public interface IDoable{...
View ArticleAnswer by James Harcourt for Is there a better alternative than this to...
Yes - just use the slightly weirdly named "pattern matching" from C#7 upwards to match on class or structure:IObject concrete1 = new ObjectImplementation1();IObject concrete2 = new...
View ArticleAnswer by Davide Cannizzo for Is there a better alternative than this to...
As per C# 7.0 specification, you can declare a local variable scoped in a case of a switch:object a = "Hello world";switch (a){ case string myString: // The variable 'a' is a string! break; case int...
View ArticleAnswer by mcintyre321 for Is there a better alternative than this to 'switch...
You're looking for Discriminated Unions which are a language feature of F#, but you can achieve a similar effect by using a library I made, called OneOf https://github.com/mcintyre321/OneOfThe major...
View ArticleAnswer by Serge Intern for Is there a better alternative than this to 'switch...
Yes, thanks to C# 7 that can be achieved. Here's how it's done (using expression pattern):switch (o){ case A a: a.Hop(); break; case B b: b.Skip(); break; case C _: return new ArgumentException("Type C...
View ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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