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 ...
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...
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 ...
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...
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 ...
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!");...
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 ...
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.
...
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? ...
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...
What are your usage of delegates in C#?
...
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...
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...
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...
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 ...
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 ...
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)
{
...
In C# is there any real difference (other than syntax) under the hood between:
myButton.Click += new EventHandler(myMemberMethod);
and
myButton.Click += myMemberMethod;
?
...
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...
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...