-3

Hi All yesterday i was post one question : like c# allow ; ; ; ; ; ; ; ; ; ; ; ; (C# Empty Statement)

Why C# allow this fun? ; ; ; ;

and found the answer : https://stackoverflow.com/a/20551989/2218635

But today i saw another one fun(bug)

See this below image

enter image description here

Why visual studio allow this? Code build success. Why it's build?

and another one fun code always run

private void install()
    {
        http://www.stackoverflow.com
        return;
    }

Edit :

See above method is a void method , I know void method does not return . But Why it's not shown any error? and I did't assign any variable for the"http://www.stackoverflow.com".but why it's not give me a error ?

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

1 Answers1

9

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


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

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Why the second fun is successfully build? – Ramesh Rajendran Dec 13 '13 at 05:43
  • Yup, That's good , but I did't assign any variable for the"http://www.stackoverflow.com".but why it's not give me a error – Ramesh Rajendran Dec 13 '13 at 06:00
  • 2
    @RameshRajendran There's no variable in your function at all. `http:` is a label `//stackoverflow.com` is a comment. Both are perfectly valid elements of C# syntax, but they are not related to variables, and in this case, they don't do anything in your method. – p.s.w.g Dec 13 '13 at 06:02