Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize Func<T1,T2> as an extension method

Is it possible to make a Func delegate an extension method? For example, just like you could create the function

bool isSet(this string x) {return x.Length > 0;}

I'd like to be able to write something like

Func<string, bool> isSet = (this x => x.Length > 0);

Of course, the above is not syntactically correct. Is there anything that is? If not, is that a limitation in syntax or compilation?

like image 905
Arithmomaniac Avatar asked Jun 25 '12 16:06

Arithmomaniac


People also ask

How do you call a Func delegate method in C#?

Func<int, int, int> Add = Sum; This Func delegate takes two parameters and returns a single value. In the following example, we use a delegate with three input parameters. int Sum(int x, int y, int z) { return x + y + z; } Func<int, int, int, int> add = Sum; int res = add(150, 20, 30); Console.

What does func mean in C#?

Func is a delegate that points to a method that accepts one or more arguments and returns a value. Action is a delegate that points to a method which in turn accepts one or more arguments but returns no value. In other words, you should use Action when your delegate points to a method that returns void.

What is TResult in C#?

TResult. The type of the return value of the method that this delegate encapsulates. This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

What is the difference between func string string and delegate?

Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter. This delegate can point to a method that takes up to 16 Parameters and returns a value.


3 Answers

Short answer: no, thats not possible.

Extension methods are syntactic sugar and can only be defined under certain circumstances (static method inside a static class). There is no equivalent of this with lambda functions.

like image 79
Philip Daubmeier Avatar answered Oct 26 '22 08:10

Philip Daubmeier


Is it possible to make a Func delegate an extension method?

No. Extension methods have to be declared as normal static methods in top-level (non-nested) non-generic static classes.

It looks like you would be trying to create an extension method only for the scope of the method - there's no concept like that in C#.

like image 31
Jon Skeet Avatar answered Oct 26 '22 08:10

Jon Skeet


To answer the question in comments on why this is wanted you coudl define the isSet func normally and just use that as a method call which will have the same effect as your extension method but with different syntax.

The syntax difference in use is purely that you'll be passing the string in as a parameter rather than calling it as a method on that string.

A working example:

public void Method()
{
    Func<string, bool> isSet = (x => x.Length > 0);

    List<string> testlist = new List<string>() {"", "fasfas", "","asdalsdkjasdl", "asdasd"};
    foreach (string val in testlist)
    {
        string text = String.Format("Value is {0}, Is Longer than 0 length: {1}", val, isSet(val));
        Console.WriteLine(text);
    }
}

This method defines isSet as you have above (but without the this syntax). It then defines a list of test values and iterates over them generating some output, part of which is just calling isSet(val). Funcs can be used like this quite happily and should do what you want I'd think.

like image 39
Chris Avatar answered Oct 26 '22 07:10

Chris