The most efficient way to combine two objects in C #

I have two objects that can be represented as int, float, bool or string. I need to do an add on these two objects, with the results being the same as C #. For example, 1+ β€œFoo” will be equal to the string β€œ1Foo”, 2 + 2.5 will be equal to float 5.5, and 3 + 3 will be equal to int 6. I am currently using the code below, but this seems like an incredible excess. Can someone simplify or point me to some way to do this efficiently?

private object Combine(object o, object o1) { float left = 0; float right = 0; bool isInt = false; string l = null; string r = null; if (o is int) { left = (int)o; isInt = true; } else if (o is float) { left = (float)o; } else if (o is bool) { l = o.ToString(); } else { l = (string)o; } if (o1 is int) { right = (int)o1; } else if (o is float) { right = (float)o1; isInt = false; } else if (o1 is bool) { r = o1.ToString(); isInt = false; } else { r = (string)o1; isInt = false; } object rr; if (l == null) { if (r == null) { rr = left + right; } else { rr = left + r; } } else { if (r == null) { rr = l + right; } else { rr = l + r; } } if (isInt) { return Convert.ToInt32(rr); } return rr; } 
+4
source share
2 answers

You can simply overload the method with the various types that you want to use. Its type is safe and simple.

  private string Combine(string o1, string o2) { return o1 + o2; } private string Combine(string o1, int o2) { return o1 + o2; } private string Combine(string o1, float o2) { return o1 + o2; } private string Combine(float o1, string o2) { return o1 + o2; } private float Combine(float o1, int o2) { return o1 + o2; } private float Combine(float o1, float o2) { return o1 + o2; } private string Combine(int o1, string o2) { return o1 + o2; } private float Combine(int o1, float o2) { return o1 + o2; } private int Combine(int o1, int o2) { return o1 + o2; } 
+4
source

Can you use .NET 4.0? If so, then it is very simple to use dynamic typing:

 private object Combine(dynamic o, dynamic o1) { // Assumes an appropriate addition operator, found at execution time return o + o1; } 

Another alternative is to have a delegate card for each pair of possible types. Unfortunately, before .NET 4.0 there is no Tuple type, so you will need to define your own TypePair type as the map key. Of course, then you need to make sure that you cover all possible pairs ... but at least the compiler can help when you have the appropriate AddDelegate method:

 private void AddDelegate<T1, T2>(Func<T1, T2, object> sumFunction) { // Put the function in the map ... } AddDelegate<int,int>((x, y) => x + y); AddDelegate<int,float>((x, y) => x + y); AddDelegate<int,string>((x, y) => x + y); AddDelegate<float,int>((x, y) => x + y); AddDelegate<float,float>((x, y) => x + y); AddDelegate<float,string>((x, y) => x + y); ... 

Btw, I took bool from this, since the β€œadding” between bool and a float (for example) makes no sense. You can decide how you want to combine them.

As Mitch says, I would redefine your design decisions - are you sure you really need it? This is a rather strange requirement. Can you tell us something about the bigger picture? We can offer alternative approaches.

+8
source

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


All Articles