anonymous-delegates

Anonymous Delegates and generic Lists in C#

Can you explain me code below : private static List<Post> _Posts; public static Post GetPost(Guid id) { return _Posts.Find(delegate(Post p) { return p.Id == id; }); } What is the point to find an object in a generic list by that way ? He can simply iterate the list. How this delegated method called for each element ...

Is there an equivilent of C# Anonymous Delegates in Java?

In C#, you can define small pieces of code called delegates anonymously (even though they are nothing more than syntactic sugar). So, or example, I can do this: public string DoSomething(Func<string, string> someDelegate) { // Do something involving someDelegate(string s) } DoSomething(delegate(string s){ return s += "asd"; }); D...

Event handling with an anonymous delegate

For the record: I found a similar question here but I have to elaborate a bit more on on the subject. My concrete scenario is this: In Silverlight 4, The myFrameworkElement.FindName("otherElementName") method seems to work fine now, but I encountered a problem. It still returns null when the element is not yet added to the visual tree ...

lambda expressions in C#?

I'm rather new to these could someone explain the significance (of the following code) or perhaps give a link to some useful information on lambda expressions? I encounter the following code in a test and I am wondering why someone would do this: foo.MyEvent += (o, e) => { fCount++; Console.WriteLine(fCount); }; foo.MyEvent -= (o, e) =...

Func<sometype,bool> to Func<T,bool>

If i have: public static Func<SomeType, bool> GetQuery() { return a => a.Foo=="Bar"; } and a generic version public static Func<T, bool> GetQuery<T>() { return (Func<T,bool>)GetQuery(); } Is there a way to cast my strongly typed Func of SomeType to a Func of T? The only way I have found so far is to try and combine it with a mock...

how to use anonymous generic delegate in C# 2.0

Hi. I have a class called NTree: class NTree<T> { delegate bool TreeVisitor<T>(T nodeData); public NTree(T data) { this.data = data; children = new List<NTree<T>>(); _stopTraverse = false; } ... public void Traverse(NTree<T> node, TreeVisitor<T> visitor) { try {...

Please help me understand anonymous delegates?

I've downloaded the VCSharpSample pack from Microsoft and started reading on Anonymous Delegates. I can more or less understand what the code is doing, but I don't understand the reason behind it. Maybe if you gave me some examples where it would result in cleaner code and easier maintainability then I could wrap my head around it. :) C...

Mapping factory methods with anonymous functions/delegates using Dictionary for faster lookup?

Currently, I have a static factory method like this: public static Book Create(BookCode code) { if (code == BookCode.Harry) return new Book(BookResource.Harry); if (code == BookCode.Julian) return new Book(BookResource.Julian); // etc. } The reason I don't cache them in any way is because BookResource is sensitive to cultu...

C# anonymus delegates efficiency/safety

Hello I have progress form which delegate: // in ProgressForm thread public delegate void executeMethod(object parameters); private executeMethod method; public object parameters; // ... private void ProgressForm_Shown(object sender, EventArgs e) { method.Invoke(parameters); } Which way ...