Concatenate two Func delegates
And: Func<Order, bool> predicate3 = order => predicate1(order) && predicate2(order); Or: Func<Order, bool> predicate3 = order => predicate1(order) || predicate2(order);
And: Func<Order, bool> predicate3 = order => predicate1(order) && predicate2(order); Or: Func<Order, bool> predicate3 = order => predicate1(order) || predicate2(order);
You are calling the same func parameter twice: var response = await ProcessRequestAsync(func); //… response = await ProcessRequestAsync(func); In this case func returns the same request every single time. It doesn’t generate a new one every time you call it. If you truly need a different request each time then func needs to return a … Read more
A Func<int, string> like Func<int, String> pageUrl = i => “Page” + i; is a delegate accepting int as its sole parameter and returning a string. In this example, it accepts an int parameter with name i and returns the string “Page” + i which just concatenates a standard string representation of i to the … Read more
I think other answers here talk about what an Action/Func is and its use. I will try to answer how to choose between Action/Func and method. The differences first: 1) From a raw performance point of view, delegates are slower compared to direct method calls, but it’s so insignificant that worrying about it is a … Read more
You cannot have instances of generic functions or actions – all type parameters are defined upfront and cannot be redefined by the caller. An easy way would be to avoid polymorphism altogether by relying on down-casting: public void SomeUtility(Func<Type, object, object> converter) { var myType = (MyType)converter(typeof(MyType), “foo”); } If you want type safety, you … Read more
Your wording is confusing. You perhaps mean “a function without a return type and no parameters.” There is simply System.Action. Action action = () => Console.WriteLine(“hello world”); action(); From your comment: But I need to fill in a <T> type in an Action and void is not a possibility (I will edit my question, I … Read more
See “Slices: usage and internals” var av = []int{1,5,2,3,7} That is a slice, not an array. A slice literal is declared just like an array literal, except you leave out the element count. That explains why the sort function will modify the content of what is referenced by the slice. As commented below by Kirk, … Read more
This kind of replaces my previous answer because this, although it’s a slightly longer route – gives you a quick method call and, unlike some of the other answers, allows you to pass through different instances (in case you’re going to encounter multiple instances of the same type). IF you don’t want that, check my … Read more
It cannot be done by Func but you can define a custom delegate for it: public delegate object MethodNameDelegate(ref float y); Usage example: public object MethodWithRefFloat(ref float y) { return null; } public void MethodCallThroughDelegate() { MethodNameDelegate myDelegate = MethodWithRefFloat; float y = 0; myDelegate(ref y); }
They’re also handy for refactoring switch statements. Take the following (albeit simple) example: public void Move(int distance, Direction direction) { switch (direction) { case Direction.Up : Position.Y += distance; break; case Direction.Down: Position.Y -= distance; break; case Direction.Left: Position.X -= distance; break; case Direction.Right: Position.X += distance; break; } } With an Action delegate, you … Read more