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# 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
217,469 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~~