Difference between FileSystemEventHandler(OnChanged) and Onchanged

A

Andrus

What is the difference between

watch.Changed += new FileSystemEventHandler(OnChanged);

and
watch.Changed += OnChanged;

?

Which form should I use ?

Andrus.


void WaitAndSaveFile(string file) {
using (FileSystemWatcher watch = new FileSystemWatcher()) {
watch.Path = Application.StartupPath;
watch.Filter = file;
watch.Changed += new FileSystemEventHandler(OnChanged);
// or watch.Changed += OnChanged; ? what is the difference ?

}

static void OnChanged(object sender, FileSystemEventArgs e) {
Console.WriteLine("\tNOTIFICATION: " + e.FullPath +
"' was " + e.ChangeType.ToString());
Console.WriteLine();
}
 
J

Jon Skeet [C# MVP]

Andrus said:
What is the difference between

watch.Changed += new FileSystemEventHandler(OnChanged);

and
watch.Changed += OnChanged;

?

Which form should I use ?

There's no difference in the code generated. In C# 1, the first form
was all you could use. C# 2 has delegate type inference and implicit
conversions from "method group" expressions to delegates, which is
what's happening in the second version. I always use the second version
where possible.
 
D

Daniel Cigic

There is no difference in 2.0 you can use shortcut like watch.Changed +=
OnChanged
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top