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
13
votes
1 answer

Assign a method with default values to Func<> without those parameters?

I would like to be able to do the following: Func tryMethodFunc = TryMethod; Where TryMethod has a signature like: bool TryMethod(int value, int value2 = 0, double value3 = 100.0) I'm not opposed to breaking the method up into a curried…
Firestrand
  • 1,305
  • 10
  • 19
12
votes
2 answers

Cannot assign a delegate of one type to another even though signature matches

My morbid curiosity has me wondering why the following fails: // declared somewhere public delegate int BinaryOperation(int a, int b); // ... in a method body Func addThem = (x, y) => x + y; BinaryOperation b1 = addThem; // doesn't…
Hobbes
  • 370
  • 2
  • 7
12
votes
5 answers

use Func<> (or Action<>) or create own delegate?

Which one is better in, say, parameter type in a method (not related to LINQ). Apparently Func is better since it's simpler, more descriptive, and if everyone is using this everything will become compatible (good). However I notice Microsoft uses…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
12
votes
4 answers

Encapsulating Action and Func?

I'm trying to make a design for some sort of IExecutable interface. I will not get into details, but the point is that I have several Actions that need to be executed from a base class. They may take different parameters (no big deal), and they…
Alpha
  • 7,586
  • 8
  • 59
  • 92
12
votes
2 answers

How to invoke Expression> against a collection

I have an interface that defines a repository from the Repository pattern: interface IRepository { List GetAllCustomers(Expression> expression); } I've implemented it against Entity Framework: class…
mason
  • 31,774
  • 10
  • 77
  • 121
12
votes
2 answers

Func delegate doesn't chain methods

Lets imagine simple delegate calls: void Main() { Func tfunc = null; tfunc += Add; // bind first method tfunc += Sub; // bind second method Console.WriteLine(tfunc(2, 2)); } private string Add(int a, int b) { …
Dariusz
  • 15,573
  • 9
  • 52
  • 68
11
votes
1 answer

Extracting Func<> from Expression<>

I wanna extract the Func<> from the following Expression : Expression, IOrderedQueryable>> order = q => q.OrderByDescending(c=>c.FullName); Func, IOrderedQueryable> orderFunc = ? How can I…
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
11
votes
2 answers

Difference between Func<> with delegate and lambda expression

while deepening myself to more advanced features of C#, I came across some code, which I didn't exactly know the difference of. It's about these two lines: Func giveLength = (text => text.Length); and Func giveLength =…
Memet Olsen
  • 4,578
  • 5
  • 40
  • 50
10
votes
4 answers

python positional args and keyword args

I am reading the source codes of mercurial, and found such a func def in commands.py: def import_(ui, repo, patch1=None, *patches, **opts): ... in python, postional args must be put ahead of keyword args. But here, patch1 is a keyword argument,…
guoqiao
  • 1,309
  • 12
  • 14
10
votes
1 answer

How to moq a Func

Trying to unit test a class whose constructor takes in a Func. Not sure how to mock it using Moq. public class FooBar { public FooBar(Func fooBarProxyFactory) { _fooBarProxyFactory = fooBarProxyFactory; …
kenwarner
  • 28,650
  • 28
  • 130
  • 173
10
votes
3 answers

Dictionary with Func as key

I am wondering if this is a sane choice of key for a dictionary? What I want to do is use an expression as the key in a dictionary, something like: var map3 = new Dictionary, int>(); map3.Add((x) => x % 2 == 0, 1); …
user380689
  • 1,766
  • 4
  • 26
  • 39
10
votes
1 answer

How can I Create a Expression.Property of a child object

normally I create an expresion in this way. ParameterExpression pe = Expression.Parameter(typeof(object1), "x"); string Name = "property1"; MemberExpression left = Expression.Property(pe, (object1).GetProperty(Name)); it produces left = x =>…
Diana G
  • 101
  • 1
  • 5
9
votes
1 answer

What does os.O_TRUNC do?

func OpenFile(name string, flag int, perm FileMode) (*File, error) f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE|O_TRUNC, 0755) Does "O_TRUNC" empty the entire file before writing? What do they mean with "truncates"? Also, does the…
user12316120
9
votes
2 answers

Func(Of Tin, Tout) using a lambda expression with ByRef argument gives incompatible signature error

Why does this: Private [Function] As Func(Of Double, String) = Function(ByRef z As Double) z.ToString gives the following error: Nested function does not have a signature that is compatible with delegate String)'. While this: Private [Function]…
Brian Mulcahy
  • 1,453
  • 1
  • 16
  • 27
9
votes
1 answer

Static method signature type arguments and partial application

I've been looking into Functional Programming lately and wanting to bring some concepts to my C# world. I'm trying to compose functions to create services (or whatever you'd call them) instead of creating classes with injectable dependencies. I've…
Aage
  • 5,932
  • 2
  • 32
  • 57