Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,524 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