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# ASP.NET MVC Basics Controllers Using Action Result Types

Xiaolong Yang
Xiaolong Yang
4,374 Points

Why not adding a semicolon after Content = "XXX"?

After copy and paste the "Hello from the comic books control" to the ContentResult(), why not have a semicolon after it? and why is there a semicolon after the curly braces?

2 Answers

Steven Parker
Steven Parker
231,275 Points

:point_right: Generally, semicolons only go after statements.

The braces after new ContentResult() enclose a list of initial object properties, and since there is only one no punctuation is needed. If the list had included additional properties, they would have been separated by commas.

The entire expression is part of a return statement, so after the closing brace a semicolon is used to indicate the end of that statement.

Xiaolong Yang
Xiaolong Yang
4,374 Points

Thanks for your help, I got why there is a semicolon after the brace of return, but I'm still a little confused about why there is no semicolon after Content = "Hello from the comic books controller"... is it like declaring something in this way? : ContentResult a = new ContentResult("Hello from the comic books controller")? I could write ContentResult() {Content = "XXX", ContentEncoding = "YYY"} just like something like ContentResult("XXX", "YYY")?

Just in case your still struggling with this one. ContentResult("XXX", "YYY") would be passing values into the constructor for the ContentResult class and ContentResult() {Content = "XXX", ContentEncoding = "YYY"} is assigning values to the properties of the ContentResult. As Steven mentioned you were setting multiple properties then they are comma separated but if there's only 1 value being set then you can skip the comma.

Xiaolong Yang
Xiaolong Yang
4,374 Points

Thank you ~~ got it~~