Reputation: 9398
Thread1.WorkerReportsProgress = true;
Thread1.ProgressChanged += new ProgressChangedEventHandler(Function2HandleWhenProgressChanges); //When progress changes, define a function to handle it.
Thread1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Function2HandleWhenThreadIsFinished); //The function to run when the thread is finished
Thread1.DoWork += new DoWorkEventHandler(Thread1_DoWorkDo); //The function defining what the thread must do.
Now, I understand that ProgressChangedEventHandler is a delegate. A delegate, in turn, turns out to be a class.
1. "ProgressChangedEventHandler" belongs to which class ? I have not defined any in my code.
2. Is "ProgressChanged" an event ? If so, which class does this belong to ?
3. If I don't specify "new ProgressChangedEventHandler" still the code compiles ?. Something like below.
Thread1.WorkerReportsProgress = true;
Thread1.ProgressChanged += Function2HandleWhenProgressChanges; //When progress changes, define a function to handle it.
Thread1.RunWorkerCompleted += Function2HandleWhenThreadIsFinished; //The function to run when the thread is finished
Thread1.DoWork += Thread1_DoWorkDo; //The function defining what the thread must do.
Upvotes: 0
Views: 1379
Reputation: 941317
A delegate, in turn, turns out to be a class.
It is not a class, it is a type. Which explains why you ask these other questions. Think of it as a type description of a method, the return type and arguments are important. A delegate type helps the compiler to make sure you assign the right kind of event handler method. The method must have the exact same return type and arguments as the delegate type. The compiler complains if that's not the case. This kind of type safety is very important in .NET.
"ProgressChangedEventHandler" belongs to which class?
It is a delegate type, not a class. It is declared in the .NET framework. Adding a reference to System.dll and putting using System.ComponentModel
at the top of your program allows you to use it without spelling out the full type name.
Is "ProgressChanged" an event ?
Yes, it is an event of the BackgroundWorker class. Along with DoWork and RunWorkerCompleted, two other events you almost always subscribe.
If I don't specify "new ProgressChangedEventHandler" still the code compiles ?.
That's called "syntax sugar". The C# compiler can tell what kind of delegate type is required from the event type and will automatically generate the "new ProgressChangedEventHandler" part of the statement as required. Very convenient. IntelliSense however will always generate it. Even the full statement is syntax sugar, you never explicitly assign the Delegate.Target property. It is inferred by the compiler to either null or this, depending on whether the target method is static or not.
Upvotes: 1
Reputation: 8741
The short answer to your questions are:
The Event handlers objects don't belong to any class necessarily. They are defined an appropriate namespace within the framework just like any other object. You can define a delegate anywhere you can define a class, so you can place it in a namespace or nested inside of another class. The location only really depends on what makes sense. In your specific example, the ProgressChangedEventHandler
belongs to the System.ComponentModel
namespace and is not nested inside of another class.
Yes, ProgressChanged
in an event, which is also part of the System.ComponentModel
namespace.
Yes, that shorthand works. The 2 formats you are comparing are generally considered the same notation and compile to the same IL, so feel free to use that if you prefer. There is actually a good SO question that discusses this very notation as I had the same exact question about a week ago
Upvotes: 1
Reputation: 14581
1) it doesn't belong to anyone. It is a separately defined type in .NET framework
2) yes it is. It is defined on BackgroundWorker
class (which can be witnessed by pressing F1 to look it up in MSDN or by the fact that you call it on an instance of BackgroundWorker
class)
3) It is syntactic sugar. Compiler helps you to do less typing.
Googling for "events and delegates .net" will show a plenty of very good explanations.
Upvotes: 4