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

Can't assign methods that return value types to Func

I have a variable of type Func and I am trying to assign it a value. If I assign it to a method that returns a value type (e.g. int), I get the error 'int MethodName()' has the wrong return type If I wrap the method in a lambda call,…
Kris Harper
  • 5,672
  • 8
  • 51
  • 96
8
votes
4 answers

Passing/Specifty A Property In Generic Method?

I am trying to move some code that I wrote to a more generic method. While the method is longer, the part I am having trouble with is the following : public static void Test() { MyObjectType[] list1 = ListMyObjectTypeMethod1(); …
user3010406
  • 541
  • 2
  • 8
  • 22
8
votes
1 answer

Invoking Actions from Moq

I've got a service with a method that takes two Actions, one for success and one for failure. Each Action takes a Result parameter that contains additional information... void AuthoriseUser(AuthDetails loginDetails, Action onSuccess,…
GoatInTheMachine
  • 3,583
  • 3
  • 25
  • 35
8
votes
3 answers

Combining Action and Func in one parameter

I have many methods that require some logging with the same pattern. Some methods need to return some value, some don't. I have created a method with Action parameter to avoid copypasting all of the logic. It looks like this: private void…
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
8
votes
2 answers

Difference between various ways of instantiating a delegate (Func)?

1: Func myFunc = new Func(delegate(int x) { return x + 1; }); 2: Func myFunc = delegate(int x) { return x + 1; }; 3: Func myFunc = x => x + 1; What is the difference between them?
user1735111
8
votes
1 answer

Using a out parameter in Func Delegate

I have a method signature bool TryGetItem(string itemKey,out Item item) How can i encapsulate this signature in delegate V Func(T input, out U output) as in the post: Func with out parameter ?
Muddassir
  • 83
  • 1
  • 5
8
votes
3 answers

Get the name of a field from a class without an instance

So I use the following utility to get the name of a field/property from an instance of a class... public static string FieldName(Expression> Source) { return ((MemberExpression)Source.Body).Member.Name; } This allows me to do the…
William
  • 3,335
  • 9
  • 42
  • 74
8
votes
3 answers

C# way to write Func with void return

I have the following two functions, that are nearly identical, the only difference is that one uses func, the other action. And I'd like to combine them into one function if it is possible. private static void TryCatch(Action action) { …
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
8
votes
1 answer

Dynamically set Func<> types

is there any way to set Func<> type arguments dynamically, so i don't have to use endless if statements? Something like: Type t = Type.GetType("System.Decimal"); Func foo = new Func(some_function); Instead of: Func foo = new…
Milos Mijatovic
  • 955
  • 1
  • 17
  • 34
7
votes
5 answers

Is it possible to declare generic delegate with no parameters?

I have... Func del2 = new Func(MyMethod); and I really want to do.. Func<> del2 = new Func<>(MyMethod); so the return type of the callback method is void. Is this possible using the generic type func?
Exitos
  • 29,230
  • 38
  • 123
  • 178
7
votes
2 answers

How do you get the properties, operators and values from an Expression> predicate?

Is there any way to pull out the properties, the operator and matching value from an Expression,bool>? Given the following example: var customers = GetCustomers(); var customerQuery = customers.Where(x=> x.CustomerID == 1 &&…
djdd87
  • 67,346
  • 27
  • 156
  • 195
7
votes
8 answers

Func delegate real world uses

I've recently been playing around with the delegate Func and creating methods that return different instances Func containing lambda but what I have struggled to come up with is any good real world ideas of why one might want…
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
7
votes
2 answers

swift ios - How to run function in ViewController from AppDelegate

I am trying to run a function in certain ViewController using AppDelegate func applicationDidBecomeActive(_ application: UIApplication) { ViewController().grabData() } But somehow the function does not seem to run at all when the app has…
Victor
  • 1,603
  • 4
  • 17
  • 24
7
votes
4 answers

How to map Expression> to Expression>

How can I map from: Expression> to: Expression> where TEntity: class, new() and TDbEntity: class, new() TEntity is from Domain and TDbEntity is from Infrastructure layer, but have same properties. It is…
Omar Amalfi
  • 379
  • 1
  • 2
  • 14
7
votes
2 answers

Can I define a method to accept EITHER a Func OR an Expression>?

If I attempt to write two overloads of a method, one accepting an Expression> parameter and another accepting a Func, I will get a compiler error on trying to call the method with a lambda expression because the two signatures create…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447