Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

andren
andren
28,558 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