Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6762487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:20:00+00:00 2026-05-26T14:20:00+00:00

I have trouble understanding what the following code means (and partly, why it even

  • 0

I have trouble understanding what the following code means (and partly, why it even compiles).

We have the following snippet:

if (true) return;
{
    ... // Unreachable code detected
}

Why does this even compile?

Am I correct in thinking that the compiler assumes an else in this construct? If not, how does it work?

I think it must be logically equivalent to

if (true) 
    return;
else
{
    ...; // Unreachable code detected.
}

I’m in doubt, because the compiler doesn’t seem to interpret the following as an if-else

if (condition) 
{
    ...
}
{
    ...
}

It does compile, but the second block gets executed no matter what.

Is this behavior explicitly stated in the C# specs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:20:01+00:00Added an answer on May 26, 2026 at 2:20 pm

    It doesn’t assume an else here, it’s just that any code of the form:

    if(condition)
    {
        code A
    
        return;
    }
    
    code B
    

    is logically equivalent to:

    if(condition)
    {
        code A
    
        return;
    }
    else
    {
        code B
    }
    

    Because code B will only ever be reached if condition evaluates to false.

    Your confusion might also be coming from having a braced block of code that is not preceded by a keyword.

    code A
    
    {
        code B
    }
    

    This is perfectly legal; code blocks do not require control flow keywords, and you can nest your code within braces wherever you like. While it’s not terribly common, it’s legal. Note that it does create a new variable scope in your code, and (just like all other code blocks) code outside of these blocks will not be able to access any variables declared within the block.

    For example:

    code A
    
    {
        int foo = 10;
    
        code B
    }
    
    int bar = foo; // compiler error, as foo is not a variable within this scope
    

    While not advisable, you could use this to declare two identically-named but different variables:

    {
        int foo = 10;
    
        code A
    }
    {
        string foo = "bar";
    
        code B
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble understanding the compilers. The following code does work in UNIX under
I have some trouble understanding the output from the following code: int main(void) {
I have trouble with the following piece of code. When I go through with
I have trouble understanding the following thing: in my localized application I have an
I'm having trouble understanding the following bit of code that I was hoping would
I have the following problem, and am having trouble understanding part of the equation:
I'm having trouble understanding why I get an error in the following code. I'm
The idea of MVC itself seems clear to me but I have trouble understanding
I have trouble finding way to correctly refactor this code so that there would
I have some trouble understanding the need for std::result_of in C++0x. If I understood

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.