This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there an actual difference in the 2 different ways of attaching event handlers in C#?
I'm working on a .NET application and I ran into a small syntax dilemma. I noticed there were two ways of unsubscribing from an event and I'm curious. Is there any difference between the two, or are they equal? Is there any scenario, when they may act differently? Compiler doesn't seem to have problem with either of those.
object.myEvent += new EventHandler(callback_function);
object.myEvent -= new EventHandler(callback_function);
and
object.myEvent += new EventHandler(callback_function);
object.myEvent -= callback_function;
Thanks
There isn't any difference.
You could also have:
object.myEvent += callback_function;
object.myEvent -= callback_function;
The compiler does some nifty type inference which makes code readability a lot better!
Wrapping a delegate in the EventHandler is redundant. You can always drop it even though it is a part of the standard template.
Resharper gives you a warning about this
Related
This question already has answers here:
Understanding events and event handlers in C#
(13 answers)
Closed 4 years ago.
I am a newbie to asp.net, can you please explain the line of code below?
this.lblDelete.Click += new System.EventHandler(this.lblDelete_Click);
I don't know much about events and delegates.
Essentially the code attaches an event handler to an event so that it can be, well... handled.
A simple way to understand it would be.
This event:
this.lblDelete.Click
Has this event handler attached:
+= new System.EventHandler
Which calls this method:
lblDelete_Click
This question already has answers here:
Difference between null == x and x == null? [duplicate]
(5 answers)
Closed 9 years ago.
I have seen both
if(something == null)
and
if(null == something)
Does it make a difference in which order this null check happens? I do not see a difference in functionality but would love to know if there is reasoning behind it.
This is legal in C# and is colloquially known as a Yoda Condition. Many people in the C/C++ world like this because it guards at compile time against replacing == with = by accident. However, it has fallen out of favor in C# due to the fact the compiler will flag it (the single =) as an error in that instance (so long as it's not a boolean eval).
Some programmers prefer to put the constant on the left side of an equality operator to avoid accidents (a typo of = instead of ==). In the second example, having the = typo would introduce a compiler error, which is easy to fix, whereas in the first example such a typo may introduce a bug that is very difficult to find.
This practice comes directly out of C and C++ programming style. I don't know whether it would affect C#. If it's no longer relevant, then it's more likely to be a habit rather than a strategy.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)'
In C# .NET 3.5, the compiler doesn't care if I do this (assume ButtonClickHandler is a function):
button.OnButtonClicked += ButtonClickHandler;
or:
button.OnButtonClicked += new ButtonClickHandlerDelegate( ButtonClickHandler );
Are these functionally the same? I read the stackoverflow question below, but I'm not sure if it applies to this scenario as well:
The difference between implicit and explicit delegate creation (with and without generics)
Yes, the first is simply syntactic sugar for the latter. The compiler simply infers the type of the delegate and constructs it for you. The exact same IL will be emitted by the compiler.
The first, shorter and cleaner syntax (delegate inference - which I recommend you use for readability), was added in C#2 - that is why some designers (also Microsoft's) tend to use the long and more verbose syntax of newing the delegate.
Actually, I think this is a duplicate of this prior question.
In C# 4 this will produce identical code, so yes they are functionally the same.
In the first (shorter) form the compiler infers the delegate type from the method signature, which saves you the work having to do it explicitly.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an actual difference in the 2 different ways of attaching event handlers in C#?
I've been seeing a lot of code that looks like this:
foo.Drop += new DragEventHandler(fooHandler);
But in the past, I've always done this:
foo.Drop += fooHandler;
Is there a difference between these two syntaxes? If so, is there any advantage to doing it the long way?
The second is shorthand for the first; they will compile to indentical IL.
However, the second syntax is new to C# 2.0; C# 1 only supports the first.
They will both result in the same IL.
So, in answer to your question, no - there is no benefit of using the longer version.
No difference , since .Net 2 and you can use what is called Method Group Conversion which allow you to Register the method name directly to the event without making a delegate Object
They are the same, but in the second example, the compiler uses Method Group conversion to infer the delegate type for you. Syntactic sugar...
This question already has answers here:
Difference between wiring events with and without "new"
(6 answers)
Closed 1 year ago.
What is the difference between this:
this.btnOk.Click += new System.EventHandler(this.btnOK_Click);
and this?
this.btnOk.Click += this.btnOK_Click;
They both work. The former is what Visual Studio defaults to when you use the snippets. But it seems like it only ads extra verbiage, or am I missing something?
No difference. Omitting the delegate instantiation is just syntax candy; the C# compiler will generate the delegate instantiation for you under the hood.
In C# 3.0 and later this is no difference. Before C# 3.0 EventHandlers were required due to compiler limitations, but with the advent of C# 3.0, the second form is preferred unless you want to be very explicit.
I believe that C# since 3.0 has implicitly added the delegate handler. However, it can help to be more explicit, especially when there are multiple possible delegate types.
"+= Delegate_Name" is a syntax sugar. Compiler will create new wrapper for you.