Is it possible to create a single multi-type set of multi-type lambda functions in C # 4.0?

For example, something like:

Dictionary<string, Func<T1, T2, bool>> comparisons; comparisons.add("<", (x, y) => x < y); comparisons.add("==", (x, y) => x == y); comparisons.add(">", (x, y) => x > y); 

At this point, I don’t know enough about C # lambdas and generic generic containers to be able to map them correctly. Is it possible?

+6
source share
5 answers

Generic types must be known at compile time, so you cannot create dynamic delegates. If you specify a data type, you can create a delegate dictionary:

 Dictionary<string, Func<int, int, bool>> comparisons; comparisons.add("<", (x, y) => x < y); comparisons.add("==", (x, y) => x == y); comparisons.add(">", (x, y) => x > y); 

You can use the IComparable interface to allow different types, but then you can only use it for the CompareTo method to implement the operators:

 Dictionary<string, Func<IComparable, IComparable, bool>> comparisons; comparisons.add("<", (x, y) => x.CompareTo(y) < 0); comparisons.add("==", (x, y) => x.CompareTo(y) == 0); comparisons.add(">", (x, y) => x.CompareTo(y) > 0); 

This, of course, gives less restrictions on the data used, you can, for example, pass the value of string and a DateTime operator delegate, and it compiles just fine. It is not until you run it so that it does not work.

+3
source

Yes, it is absolutely true to have something like this:

 Dictionary<string, Func<int, int, bool>> comparisons = new Dictionary<string, Func<int, int, bool>>(); comparisons.Add("<", (x, y) => x < y); comparisons.Add("==", (x, y) => x == y); comparisons.Add(">", (x, y) => x > y); 

In your example, however, you need to use Func<int, int, bool> , since you take two parameters and return a boolean value.

You can also put this in a common implementation, but then you will need to somehow restrict it so that something has to execute the <, == and> operators.

+7
source
 Func<int, int, bool> t = null; var comparisons = new Dictionary<string, Func<int, int, bool>> { {"<", (x, y) => x < y}, {"==", (x, y) => x == y}, {">", (x, y) => x > y} }; t = comparisons["<"]; bool result = t(1,2); 
0
source

Yes, this can be done, but you can only do this if all types of type parameters are given specific types. For example, this works:

 Dictionary<string, Func<int, int, bool>> comparisons = new Dictionary<string, Func<int, int, bool>>(); 

There is no way to do this (in pseudo-C ++ syntax):

 Dictionary<string, Func<?, ?, bool>> comparisons = new Dictionary<string, Func<?, ?, bool>>(); 
0
source

I see no reason to have T1 and T2; if you have another type that you usually cannot compare, can you?

If you want to keep it universal, you can do something like this (I store the comparison dictionary in the field here):

 class MyClass<T> where T:IComparable<T> { private Dictionary<string, Func<T, T, bool>> comparisons = new Dictionary<string, Func<T, T, bool>> { {"<", (x, y) => x.CompareTo(y) < 0}, {"==", (x, y) => x.Equals(y)}, {">", (x, y) => x.CompareTo(y) > 0} }; } 
0
source

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


All Articles