C# Querying With LINQ Functional Programming in C# Delegates

Instantiating a Delegate

In this lesson Carling instantiates a delegate like this:

SayGreeting sayGreeting = new SayGreeting(SayHello);

In the Microsoft docs (delegates C# Reference) it instead instantiates a delegate to a named method like this:

SayGreeting sayGreeting = SayHello;

Am I correct in assuming that this is essentially the same thing, since the compiler can infer the type based on the fact that it has already been specified on the left hand side of the assignment operator?

Thus the second method is just a shorthand way of doing the same thing, otherwise referred to as "Syntactic Sugar"?

2 Answers

28,451 Points

Yes, that assumption is correct. C# is a language that has had a large amount of "Syntactic Sugar" built into it over the years. According to this page support for that syntax was added in C# 2.0.

Honestly that's one of the reasons I like C# as a language. Microsoft is not afraid to add new syntax that exists solely to make the code more pleasant to write, even though it technically does not add any new functionality over the existing syntax.

Thank you for the reply