views:

102

answers:

3

Is it possible to obtain the function behind a C# operator? For example in F# you can do

let add = (+);;

val add : (int -> int -> int)

Is it possible in c# to do something like this:

Func<int, int, int> add = (+);

or

Func<int, int, int> add = Int32.(+)

?

+5  A: 

You can use lambda expressions:

Func<int, int, int> func = (val1, val2) => val1 + val2;
Andrew Bezzub
+3  A: 

Here's an indirect way to do what you want:

Func<int, int, int> add = (int a, int b) => a + b;

When reflected, it can be seen that F# doesn't access the Int32.operator+ directly either (that's probably impossible), but does something like the C# code above just using an internal delegate type instead (Microsoft.FSharp.Core.FastFunc).

Saulius
A: 

To add to the other answers, addition of ints is done by the CIL add instruction, rather than a method call, so there isn't any sense in which it would be possible to acquire 'the actual MethodInfo'.

AakashM