func

C# - How do I define an inline method Func<T> 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<T>(string key, Func<T> defaultValue) { if (HttpContext.Current.Session[key] == null) { HttpContext.Current.Session[key] = defaultValue.Inv...

What's so great about Func<> delegate?

Hi, Sorry if this is basic but I was trying to pick up on .Net 3.5. Question: Is there anything great about Func<> and it's 5 overloads? From the looks of it, I can still create a similar delgate on my own say, MyFunc<> with the exact 5 overloads and even more. eg: public delegate TResult MyFunc<TResult>() and a combo of various overl...

converting a .net Func<T> to a .net Expression<Func<T>>

Going from a lambda to an Expression is easy using a method call... public void GimmeExpression(Expression<Func<T>> expression) { ((MemberExpression)expression.Body).Member.Name; // "DoStuff" } public void SomewhereElse() { GimmeExpression(() => thing.DoStuff()); } But I would like to turn the Func in to an expression, only i...

Explanation of Func

Hi all, I was wondering if someone could explain what Func<int, string> is and how it is used with some clear examples. Thanks in advance ...

Does C have __func__ functionality for names of the arguments of a function?

Does the "C" standard support something similar to __func__ for the function arguments' names? ...

How do you use Func<> and Action<> when designing applications?

All the examples I can find about Func<> and Action<> are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be solved only in a more complex way, i.e. I know how they work and I can see they are terse and ...

How to convert System.Linq.Enumerable.WhereListIterator<int> to List<int>?

In the below example, how can I easily convert eventScores to List<int> so that I can use it as a parameter for prettyPrint? Console.WriteLine("Example of LINQ's Where:"); List<int> scores = new List<int> { 1,2,3,4,5,6,7,8 }; var evenScores = scores.Where(i => i % 2 == 0); Action<List<int>, string> prettyPrint = (list, title) => { ...

Pass method, created with reflection, as Func parameter

Hello, I've got a method (fyi, I'm using c#), accepting a parameter of type "Func", let's say it's defined as such: MethodAcceptingFuncParam(Func<bool> thefunction); I've defined the function to pass in as such: public bool DoStuff() { return true; } I can easily call this as such: MethodAcceptingFuncParam(() => { return Do...

Can I set a Func<> function with runtime variables to omit passing them as parameters in C#?

Hello, I have a numerical analysis program which for simplicity calculates an algorithm similar to: y = ax^3 + bx^2 + cx + d; I calculate the values of a,b,c,d at runtime and would like to pass the following equivalent as a Func<double, double>. Where I can set a value for X, and get Y. y = 12x^3 + 13x^2 + 14x + 15; Where 12,13,14...

Partial Application of Infix Functions in F#

In haskell it is posible to partially apply an infix function using sections, for instance given the infix function < (less than) one can partially apply any of the function's arguments: (5 <) , (< 5) In other words, in haskell we have the following shorthand notation: op :: a -> b -> c (`op` y) === \x -> x `op` y (x `op`) === \y -> x...

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<User> GetUsers(Func<User, bool> predicate) { var users = GetAllUsers(); return users.Where(predicate).ToList(); ...

Calling Begin/EndInvoke on Action and Func in Silverlight

Does anyone know why I can't call BeginInvoke / EndInvoke on Action and Func delegates in my Silverlight app? I keep getting a NotSupportedException. Is there a workaround? ...

How can I pass a mouse-click method as a parameter?

I want to make an extension method which fills a stackpanel with buttons. In order to do this I have to pass in a mouse-click-handler. What type does the mouseClickHandler parameter have to be here? I know it's something like these but they all don't work: delegate Func<object, RoutedEventArgs> Action<> Code: public static void F...

How do I apply a String extension to a Func<String>

Hi All, I have a constructor signature like this public NavigationLink(Func<String> getName, Func<UrlHelper, String> getURL, Func<bool> isVisible, IEnumerable<NavigationLink> subItems) Inside that constructor I am assigning the getName to a property of the containin...

Func delegate with ref variable

public object MethodName(ref float y) { //method } How do I defined a Func delegate for this method? ...

Example of a Good Func Spec?

Hey, I'm writing my func spec, and I was wondering if there are any good samples of a complete and well-written func spec? Like "This is a standard You're supposed to aspire to" type of spec. I know that Joel has a skeleteon of a func spec on his website, but I am looking for something more complete because I'm not of the appropriate am...

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

Hello, 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# 2008/Deitel&Deitel) and also MSDN but I'm not getting it yet. They all jump straight in th...

C# ambiguity in Func + extension methods + lambdas

I've been trying to make my way through this article: http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx ... And something on page 1 made me uncomfortable. In particular, I was trying to wrap my head around the Compose<>() function, and I wrote an example for myself. Consider the following two Func's: Func<d...

How do i refactor this code by using Action<t> or Func<t> delegates

I have a sample program, which needs to execute 3 methods in a particular order. And after executing each method, should do error handling. Now i did this in a normal fashion, w/o using delegates like this. class Program { public static void Main() { MyTest(); } private static bool MyTest() { ...

Methods of sending web-generated config files to servers and restarting services.

Hi, We're writing a web-based tool to configure our services provided by multiple servers. This includes interfaces configuration, dhcp configs etc. etc. Having configs in database and views that generate proper output, how to send it/make it available for servers? I'm thinking about sending it through scp and invoking reload command t...