Questions tagged [func]

Func is a family of delegate types in the .Net framework.

Func is a family of types in the framework. They are in the System namespace and can be used to represent a function that returns a value and has zero or more parameters.

1154 questions
47
votes
3 answers

void Func without arguments

There are some similar questions but not exactly like mine. Is there a Func equivalent for a function without a return value (i.e. void) and without parameters? The related question is Func not returning anything? but this does not answer for a void…
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
45
votes
4 answers

How can I pass in a func with a generic type parameter?

I like to send a generic type converter function to a method but I can't figure out how to do it. Here's invalid syntax that explains what I like to achieve, the problem is I don't know how to specify the generic type together with my func: public…
joeriks
  • 3,382
  • 8
  • 32
  • 42
43
votes
5 answers

Action/Func vs Methods, what's the point?

I know how to use Action and Func in .NET, but every single time I start to, the exact same solution can be achieved with a regular old Method that I call instead. This excludes when an Action or Func is used as an argument for something I don't…
James P. Wright
  • 8,991
  • 23
  • 79
  • 142
39
votes
8 answers

Can someone explain what the C# "Func" does?

I'm reading the Pro MVC 2 book, and there is an example of creating an extension method for the HtmlHelper class. Here the code example: public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func pageUrl) { …
delete
38
votes
2 answers

The request message was already sent. Cannot send the same request message multiple times

Is there anything wrong with my code here? I keep getting this error: System.InvalidOperationException: The request message was already sent. Cannot send the same request message multiple times. My HttpRequestMessage is inside a Func so I figured…
Prabhu
  • 12,995
  • 33
  • 127
  • 210
30
votes
1 answer

Concatenate two Func delegates

I have this Class: public class Order { int OrderId {get; set;} string CustomerName {get; set;} } I declare below variables, too Func predicate1 = t=>t.OrderId == 5 ; Func predicate2 = t=>t.CustomerName == "Ali"; Is…
Masoud
  • 8,020
  • 12
  • 62
  • 123
28
votes
3 answers

How to declare a generic delegate with an out parameter

Func, just don't compile, how to declare that i want the second parameter be an out one? I want to use it like this: public class Foo() { public Func DetectMethod; }
Benny
  • 8,547
  • 9
  • 60
  • 93
27
votes
2 answers

What's the actual type of lambda in C#?

I read that C# lambdas can be imlicitly converted to Action or Func , but lambda cannot be executed directly Define a lambda function and execute it immediately For example : int n = (()=>5)(); //doesn't work int n = ((Func)(()=>5))();…
ig-melnyk
  • 2,769
  • 2
  • 25
  • 35
27
votes
5 answers

Using FluentValidation's WithMessage method with a list of named parameters

I am using FluentValidation and I want to format a message with some of the object's properties value. The problem is I have very little experience with expressions and delegates in C#. FluentValidation already provides a way to do this with format…
Jason
  • 4,557
  • 5
  • 31
  • 40
25
votes
6 answers

What's so great about Func<> delegate?

Sorry if this is basic but I was trying to pick up on .Net 3.5. Question: Is there anything great about Func<> and it's 5 overloads? From the looks of it, I can still create a similar delgate on my own say, MyFunc<> with the exact 5 overloads and…
David Browen
24
votes
6 answers

Why is Func<> created from Expression> slower than Func<> declared directly?

Why is a Func<> created from an Expression> via .Compile() considerably slower than just using a Func<> declared directly ? I just changed from using a Func declared directly to one created from an…
MartinF
  • 5,929
  • 5
  • 40
  • 29
22
votes
3 answers

Assigning a Func to an Expression and vice versa

I was tampering with Expressions and I got confused at some points We can assign same LamdaExpression to both Expression and/or Func. But we cannot assign a Func to an Expression (or an Expression to Func). Why cannot we do that? I looked for if a…
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
21
votes
2 answers

Extension method that extends T - bad practice?

I've read that it is usually bad practice to extend System.Object, which I do agree with. I am curious, however, if the following would be considered a useful extension method, or is it still bad practice? It is similar to extending System.Object…
Sean Thoman
  • 7,429
  • 6
  • 56
  • 103
21
votes
1 answer

Does Ninject support Func (auto generated factory)?

Autofac automatically generates factories for Func; I can even pass parameters. public class MyClass { public MyClass(Func a, Func b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If…
user593358
21
votes
4 answers

Isn't Func and Predicate the same thing after compilation?

Haven't fired up reflector to look at the difference but would one expect to see the exact same compiled code when comparing Func vs. Predicate I would imagine there is no difference as both take a generic parameter and return bool?
Sean Chambers
  • 8,572
  • 7
  • 41
  • 55
1
2
3
76 77