Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case on type c# [duplicate]

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

Hello suppose i get a big if/else on class type. it's there a way to do it with a switch case ?

Example :

function test(object obj) { if(obj is WebControl) {  }else if(obj is TextBox) {  } else if(obj is ComboBox) {  } 

etc ...

I would like to create something like

switch(obj) { case is TextBox: break; case is ComboBox: break;  } 

}

like image 771
Cédric Boivin Avatar asked Aug 31 '11 03:08

Cédric Boivin


People also ask

What is a switch case in C?

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.

Can Switch case be used for Strings in C?

No you can't.

What is switch on type?

Switching on an objects type is useful when you are executing different actions based for different types. The same can be achieved using if/else statements, though it tends to get a bit messy if you have more than two or three types.


1 Answers

Update C# 7

Yes: Source

switch(shape) {     case Circle c:         WriteLine($"circle with radius {c.Radius}");         break;     case Rectangle s when (s.Length == s.Height):         WriteLine($"{s.Length} x {s.Height} square");         break;     case Rectangle r:         WriteLine($"{r.Length} x {r.Height} rectangle");         break;     default:         WriteLine("<unknown shape>");         break;     case null:         throw new ArgumentNullException(nameof(shape)); } 

Prior to C# 7

No.

http://blogs.msdn.com/b/peterhal/archive/2005/07/05/435760.aspx

We get a lot of requests for addditions to the C# language and today I'm going to talk about one of the more common ones - switch on type. Switch on type looks like a pretty useful and straightforward feature: Add a switch-like construct which switches on the type of the expression, rather than the value. This might look something like this:

switch typeof(e) {          case int:    ... break;          case string: ... break;          case double: ... break;          default:     ... break;  } 

This kind of statement would be extremely useful for adding virtual method like dispatch over a disjoint type hierarchy, or over a type hierarchy containing types that you don't own. Seeing an example like this, you could easily conclude that the feature would be straightforward and useful. It might even get you thinking "Why don't those #*&%$ lazy C# language designers just make my life easier and add this simple, timesaving language feature?"

Unfortunately, like many 'simple' language features, type switch is not as simple as it first appears. The troubles start when you look at a more significant, and no less important, example like this:

class C {} interface I {} class D : C, I {}  switch typeof(e) { case C: … break; case I: … break; default: … break; } 

Link: https://blogs.msdn.microsoft.com/peterhal/2005/07/05/many-questions-switch-on-type/

like image 116
Steve Avatar answered Oct 04 '22 16:10

Steve