How to implement type switching

I have some code snippets that need to be executed if the data type of the variable matches a specific base type. I am currently doing this using an if-else loop

Example:

if(a is float) { // do something here } else if (a is int) { // do something here } else if (a is string) { // do something here } 

Since I have too many types against which I have to compare, using If else is pretty awkward. Since C # does not allow type switching, is there an alternative way to do this?

+4
source share
4 answers

Recover the code and use the method overload :

 void SomeCode() { ... Action(3.0f); // calls float overload Action("hello"); // calls string overload ... } void Action(float a) { ... } void Action(int a) { ... } void Action(string a) { ... } 

EDIT:
Using the dynamic keyword (.NET 4), it works this way (full console application):

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SomeCode(); } static void SomeCode() { object o = null; switch (new Random().Next(0, 3)) { case 0: o = 3.0f; break; case 1: o = 3.0d; break; case 2: o = "hello"; break; } Action((dynamic)o); // notice dynamic here } static void Action(dynamic a) { Console.WriteLine("object"); } static void Action(float a) { Console.WriteLine("float"); } static void Action(int a) { Console.WriteLine("int"); } static void Action(string a) { Console.WriteLine("string"); } } } 
+5
source

You can create a Dictionary<Type, Action<object>> and save the types (return values ​​from GetType method a together with delegates who execute any code that you want to run for this type.

For example, look at this:

 private readonly Dictionary<Type, Action<object>> typeActions = new Dictionary<Type, Action<object>>() { { typeof(int), (a) => { Console.WriteLine(a.ToString() + " is an integer!"); } }, { typeof(float), (a) => { Console.WriteLine(a.ToString() + " is a single-precision floating-point number!"); } } }; 

This dictionary can then be used elsewhere in your code:

 Action<object> action; if (typeActions.TryGetValue(a.GetType(), out action)) { action(a); } 

Note that you still have to cast a to the appropriate type inside your actions.

EDIT: as Chris noted correctly, this will not recognize a.GetType() if a belongs to a subclass of the registered type. If you need to enable this, you will have to go through a type hierarchy:

 Action<object> action = null; for (Type t = a.GetType(); t = t.BaseType; t != null) { if (typeActions.TryGetValue(t, out action)) { break; } } if (action != null) { action(a); } 

If you need to cover common types and / or interfaces, this is also possible, but the code will become more and more complex.

+2
source

My solution is not much different from yours, but I did it like this:

 while(true){ var typeOfVarToCheck = a.GetType(); // a comes from Op Code if(typeOfVarToCheck==typeof(type1)){ //Do Something break; } if(typeOfVarToCheck==typeof(type2)){ //Do Something break; } //Do default break; } 
0
source

You must convert the type to a string.

  var typeObj = //Value... switch (typeObj.GetType().ToString()) { case "System.Int32": //Some Code case "System.String": //Some Code default: break; } 
0
source

Source: https://habr.com/ru/post/1494957/


All Articles