[Solved] C# code allow fun syntax, and also void method can allowed return


The method builds because it’s perfectly valid C#.

Those are labels. They are part of the goto construct that C# inherited from C / C++ that allows execution to jump to a specific point within the method. It’s use is generally discouraged.

From 8.4 Labeled statements

A labeled-statement permits a statement to be prefixed by a label. Labeled statements are permitted in blocks, but are not permitted as embedded statements.

labeled-statement:

 identifier : statement

A labeled statement declares a label with the name given by the identifier. The scope of a label is the whole block in which the label is declared, including any nested blocks. It is a compile-time error for two labels with the same name to have overlapping scopes.

Further Reading

  • Does anyone still use [goto] in C# and if so why?

Regarding the updated question. Notice that there is no value supplied in the return statement. It’s perfectly valid to use return in this way within a void method. That simply causes execution to stop and the control to be transferred back the caller. In fact, you can think of every method having an implicit return statement at then end to return control back to the caller.

It would be an error if you attempted to return a specific value:

return 0; 

Produces the error:

Since ‘MyPage.Page_Load’ returns void, a return keyword must not be followed by an object expression

3

solved C# code allow fun syntax, and also void method can allowed return