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
20
votes
7 answers

In few words, what can be said about Func<>

I've been seing Func<> for sometime now, and I've manage to avoid it (for now). But, now it looks like I can't dodge it forever. For instance, I tried Dynamic Linq, but almost everything was in terms of Func<>. I've tried one of my book (C#…
Richard77
  • 20,343
  • 46
  • 150
  • 252
18
votes
2 answers

A delegate for a function with variable parameters

I have a function of this sort void func(params object[] parameters) { //Function Body } It can accept parameters of the following sort func(10, "hello", 30.0); func(10,20); and so on. I wanted to create an Action delegate for the above…
SohamC
  • 2,367
  • 6
  • 22
  • 34
18
votes
3 answers

How to convert System.Linq.Enumerable.WhereListIterator to List?

In the below example, how can I easily convert eventScores to List so that I can use it as a parameter for prettyPrint? Console.WriteLine("Example of LINQ's Where:"); List scores = new List { 1,2,3,4,5,6,7,8 }; var evenScores =…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
16
votes
4 answers

A List<> of Func<>s, compile error with generic return type, but why?

This is a bit of a lengthy question, so please bear with me. I need to create a mapping between a set of strings and corresponding generic method calls for each string. However I've run into a compile issue, explained lower down. In my scenario I am…
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
15
votes
5 answers

Why is Action/Func better than a regular Method in .Net?

I much prefer using an Action or a Func if I need a quick reusable piece of code, however others on my team don't like them or understand them. At the moment my only real argument is about preference and more up to date code practices, but those are…
James P. Wright
  • 8,991
  • 23
  • 79
  • 142
15
votes
4 answers

C#: Elegant way to wrap method calls

Apologies for the fairly ambiguous title but what I'm trying to achieve is probably better stated in code. I have a WCF client. When I'm calling methods I would like to wrap each call in some error handling code. So, instead of exposing the methods…
djskinner
  • 8,035
  • 4
  • 49
  • 72
15
votes
2 answers

Pass selector (function name) to function in swift

I want to pass the selector for a function to another function. Right now I'm just passing the string and doing several if statements. If I could pass the actual function selector to the function, I could eliminate all the if statements. My current…
Works for a Living
  • 1,262
  • 2
  • 19
  • 44
14
votes
4 answers

Convert Func to Func

I think my mind is exploding trying to figure out Funcs... If this makes no sense, I apologize, right now it make sense to me but its been a long day already .... 1) Assuming you are given a func which takes in T and outputs a string: Func
Peter
  • 699
  • 9
  • 19
14
votes
3 answers

Lambda\Anonymous Function as a parameter

I'm a very new to C#. Just playing around with it. Not for a real purpose. void makeOutput( int _param) { Console.WriteLine( _param.ToString()); } //... // Somewhere in a code { makeOutput( /* some not c# code for an example for what…
Focker
  • 215
  • 1
  • 4
  • 14
14
votes
1 answer

How do I declare a Func Delegate which returns a Func Delegate of the same type?

I'd like to write a method which does some work and finally returns another method with the same signature as the original method. The idea is to handle a stream of bytes depending on the previous byte value sequentially without going into a…
Marwie
  • 3,177
  • 3
  • 28
  • 49
14
votes
2 answers

Using LINQ's Zip with a closure that doesn't return a value

Disclaimer: this question is driven by my personal curiosity more than an actual need to accomplish something. So my example is going to be contrived. Nevertheless I think it's an issue that might very well crop up. Let's say we are using Zip to…
s.m.
  • 7,895
  • 2
  • 38
  • 46
13
votes
7 answers

Is Func appropriate to use as a ctor arg when applying Dependency Injection?

Example: public class BusinessTransactionFactory where T : IBusinessTransaction { readonly Func _createTransaction; public BusinessTransactionFactory(Func createTransaction) { …
Rookian
  • 19,841
  • 28
  • 110
  • 180
13
votes
6 answers

C#: Func<> instead of methods?

This is a curiosity questions for you all in the know: Is there any harm/downside to using a Func instead of a method? Simple example: private static Func> Foo = (i1, i2, dbc) => (i1 != 0) ? dbc.Bar(i2) : new…
mnsr
  • 12,337
  • 4
  • 53
  • 79
13
votes
3 answers

Action as Func in C#

I have a parametric method that takes a Func as an argument SomeType SomeMethod( Func f ) {...} I would like to pass an Action without having to overload the method. But this takes to the problem, how do you represent and Action as a Func?…
Cristian Garcia
  • 9,630
  • 6
  • 54
  • 75
13
votes
2 answers

How to get Method Name of Generic Func passed into Method

I'm trying to get the method name of a function passed into an object using a .Net closure like this: Method Signature public IEnumerable GetData(Func> WebServiceCallback) where T : class { // either gets me…
Barry McDermid
  • 319
  • 3
  • 10
1 2
3
76 77