delegates

Templated delegates

I have the following piece of code pattern: void M1(string s, string v) { try { // Do some work } catch(Exception ex) { // Encapsulate and rethrow exception } } The only difference is that the return type and the number and types of parameters to the methods can vary. I want to create a generic / templated method ...

Is there a downside to adding an anonymous empty delegate on event declaration?

I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check for null before raising the event. However, I am keen to understand if there are any downsides. For example, is it something that is in wi...

C# event handling (compared to Java)

Hello everyone: I am currently having a hardtime understanding and implementing events in C# using delagates. I am used to the Java way of doing things: Define an interface for a listener type which would contain a number of method definitions Define adapter class for that interface to make things easier if I'm not interested in all ...

Event Handler behavioral difference .net 1.1 vs 2.0 with null delegate

Not sure what exactly is going on here, but seems like in .NET 1.1 an uninitialized event delegate can run without issues, but in .NET 2.0+ it causes a NullReferencException. Any ideas why. The code below will run fine without issues in 1.1, in 2.0 it gives a NullReferenceException. I'm curious why does it behave differently? What ch...

How do you catch a managed exception (from a delegate) in unmanaged C++

I have unmanaged C++ calling a managed delegate via the function pointer provided by Marshal::GetFunctionPointerForDelegate. This delegate has the potential to throw an exception. I need to be able to properly handle this exception in my unmanaged C++ to ensure things like pointer cleanup, and potentially rethrow the exception up into ...

Unsubscribe anonymous method in C#

Is it possible to unsubscribe an anonymous method from an event? If I subscribe to an event like this: void MyMethod() { Console.WriteLine("I did it!"); } MyEvent += MyMethod; I can un-subscribe like this: MyEvent -= MyMethod; But if I subscribe using an anonymous method: MyEvent += delegate(){Console.WriteLine("I did it!");...

Can you explain lambda expressions?

I don't really get lambda expressions. While they've been around since the days of ALGOL, I didn't start hearing about them until fairly recently, when Python and Ruby became very popular. Now that C# has the => syntax, people in my world (.NET) are talking about lamdba expressions more and more. I've read the Wikipedia article on the ...

Using Delegates with Exchange Web Services

Has anyone used delegates with exchnage web services? I would like one user to be able to control other users' calendars in Exchange. I'm finding this problem to be a little tricky, and I'd like to see how others have been able to get it to work properly. ...

Are there anonymous, type-safe, generic delegate signatures in C# 2.0?

Consider the delegate for a generic A to B function: public delegate B Fun<A, B>(A x); I can then write a function that accepts and invokes the Fun delegate: public static B invokeFun<A, B>(A x, Fun<A, B> f) { return f(x); } (Never mind whether it is wise to write invokeFun.) Can I write invokeFun without naming the Fun delegate? ...

Ending asynchronous delegate invocation with partial type information

When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something like the following (and to save you guessing this is an async wrapper around a cache): IAsyncResult BeginPut(string key, object value) { Action<string, object> put = this.cache.Put; return put.BeginInvoke(key, value, n...

When would you use delegates in C#?

What are your usage of delegates in C#? ...

C# Generics wont allow Delegate Type Constraints

Is it possible to define a class in C# such that class GenericCollection<T> : SomeBaseCollection<T> where T : Delegate I couldn't for the life of me accomplish this last night in .NET 3.5. I tried using delegate, Delegate, Action<T> and Func<T, T> It seems to me that this should be allowable in some way. I'm trying to implement my o...

Using CalendarItemCreateOrDeleteOperationType with Exchange Web Services and Delegates

When I create a calendar appointment using delegates with Exchange Web Services, I have to set CalendarItemCreateOrDeleteOperationType. The problem is, the delegate will be a superuser account that no one actually uses, and I would like to avoid filling up its mailbox. CalendarItemCreateOrDeleteOperationType has a SendToNone option, bu...

Events and Delegates with ASP.NET master pages

How do you catch a custom event raised by a master page? On my master page I have a custom event and delegate: public event SignOutHandler SignOut; public delegate void SignOutHandler(); This is raised when a link button on the master page is clicked. if (SignOut != null) { SignOut(); } In a user control on the page I'd like to s...

Behaviour of exceptions within delegates in C# 2 hosted by MS Excel and COM

Morning all, Bit of a language theory question here... I've found some references online suggesting that exception handling and delegates in C# have some different behaviour in some cases but I cannot find any concrete documentation on the matter. We recently had some big problems with exceptions inside delegates for a Microsoft Excel ...

Caching delegate results

I have a C# method which accepts a Predicate<Foo> and returns a list of matching items... public static List<Foo> FindAll( Predicate<Foo> filter ) { ... } The filter will often be one of a common set... public static class FooPredicates { public static readonly Predicate<Foo> IsEligible = ( foo => ...) ... } ...but may ...

How do C# Events work behind the scenes?

I'm using C#, .NET 3.5. I understand how to utilize events, how to declare them in my class, how to hook them from somewhere else, etc. A contrived example: public class MyList { private List<string> m_Strings = new List<string>(); public EventHandler<EventArgs> ElementAddedEvent; public void Add(string value) { ...

Is there an actual difference in the 2 different ways of attaching event handlers in C#?

In C# is there any real difference (other than syntax) under the hood between: myButton.Click += new EventHandler(myMemberMethod); and myButton.Click += myMemberMethod; ? ...

C#: Virtual Function invocation is even faster than a delegate invocation?

It just happens to me about one code design question. Say, I have one "template" method that invokes some functions that may "alter". A intuitive design is to follow "Template Design Pattern". Define the altering functions to be "virtual" functions to be overridden in subclasses. Or, I can just use delegate functions without "virtual". T...

If events are implemented as delegates in .NET, what is the point of the .event IL section?

I've seen some very good questions on Stack Overflow concerning delegates, events, and the .NET implementation of these two features. One question in particular, "How do C# Events work behind the scenes?", produced a great answer that explains some subtle points very well. The answer to the above question makes this point: When y...