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
9
votes
0 answers

why C# Func lambda expression requires cast for result

I need to understand about Func type expression deeper. public class TheResult : IResultEntry { ... } With above class, why does below second method require cast? I can read the error message of course, but hard to understand. // Success public…
Youngjae
  • 24,352
  • 18
  • 113
  • 198
9
votes
1 answer

Convert Func delegate to a string

Is there any way to convert an existing Func delegate to a string like that: Func func = (i) => i*2; string str = someMethod(func); // returns "Func func = (i) => i*2" or at least smth close to it
curious
  • 107
  • 1
  • 4
9
votes
1 answer

For Func, where A extends T, A does not satisfy for T

Okay, let me set the scene: We have a function used within our code that takes a function and does some logging around it and then returns the result. It looks a little something like this. TResponse LoggedApiCall(Func
Kira Namida
  • 279
  • 3
  • 16
9
votes
3 answers

C# method accepting a predicate - does this look ok?

I'd like a method that has the following API: //get all users with a role of admin var users = myRepository.GetUsers(u => u.Role == Role.Admin); Will something like this work? IList GetUsers(Func predicate) { var…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
9
votes
1 answer

How to use a Func in an expression with Linq to Entity Framework?

I am trying to write a linq to entity extension method that takes a Func to select a property Id and compare it against a list of ids. Classes public class A { public int AId { get; set; } } public class B { public int BId { get; set;…
David
  • 15,150
  • 15
  • 61
  • 83
9
votes
4 answers

Func<> getting the parameter info

How to get the value of the passed parameter of the Func<> Lambda in C# IEnumerable _data = await accountRepo.GetAsync(); string _query = "1011"; Accounts = _data.Filter(p => p.AccountNumber == _query); and this is my extension…
Vincent Dagpin
  • 3,581
  • 13
  • 55
  • 85
9
votes
3 answers

C# - How do I define an inline method Func as a parameter?

I've written a simple SessionItem management class to handle all those pesky null checks and insert a default value if none exists. Here is my GetItem method: public static T GetItem(string key, Func defaultValue) { if…
tags2k
  • 82,117
  • 31
  • 79
  • 106
9
votes
2 answers

T of Func is inferred from output of lambda expression only when S and T are different?

When S and T are different, this works: public static void Fun(Func func) { } Fun((string s) => true); //compiles, T is inferred from return type. But, public static void Fun(Func func) { } Fun(t => true); //can't infer…
nawfal
  • 70,104
  • 56
  • 326
  • 368
8
votes
1 answer

Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols calling calling functions with swift ui

I have a SwiftUI View called MyWatchView with this stack: VStack (alignment: .center) { HStack { Toggle(isOn: $play) { Text("") } .padding(.trailing, 30.0) .hueRotation(Angle.degrees(45)) …
Derick Mathews
  • 347
  • 2
  • 3
  • 12
8
votes
1 answer

Why does adding throw inside a lambda without a return value get inferred as a Func and not as Action?

I'm running into an issue where some test code for a library I'm writing won't compile due to an ambiguous call but the usage seemed clear to me. Upon further investigation I've found that adding throw inside a lambda that has no return value seems…
Cameron Owen
  • 83
  • 1
  • 4
8
votes
4 answers

How can I force a throw to be a statement and not an expression (in a lambda expression)?

Starting from C# 7.0 the throw keyword can be used both as an expression and as a statement, which is nice. Though, consider these overloads public static void M(Action doIt) { /*use doIt*/ } public static void M(Func doIt) { /*use doIt*/…
Ulf Åkerstedt
  • 3,086
  • 4
  • 26
  • 28
8
votes
2 answers

Optimizing Func.Invoke() generated from expression tree

I am working on an automation for instantiating classes dynamically. I decided to write an expression tree that would generate a Func, that could instantiate my class for me. However, I am noticing 3x slower performance of my Func as opposed to…
Hentov
  • 473
  • 1
  • 4
  • 12
8
votes
2 answers

c# Predicate with no parameters

I'm trying to pass a Predicate to a function but it has no input. I'm actually trying to delay the computation until the call is made. Is there a way to use c# Predicate type for that? If not why. I know a way to do this with Func Func
Dolev
  • 654
  • 11
  • 20
8
votes
2 answers

C# Action and Func parameter overloads

I need a method that takes an Action (or a Func), but the Action has a mixed number of parameters. What is the most straight forward and compact way to implement these overloads: public void Execute(Action action, T param) { // TODO:…
Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130
8
votes
3 answers

Strongly typed url action

I've read multiple posts and blogs similar to Delegate-based strongly-typed URL generation in ASP.NET MVC But none of them really quite do what I'd like to do. Currently I have a hybrid approach like: // shortened for Brevity public static Exts { …
Erik Philips
  • 53,428
  • 11
  • 128
  • 150