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 9174583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:46:43+00:00 2026-06-17T16:46:43+00:00

Possible Duplicate: Variable declaration in c# switch statement What is the purpose of the

  • 0

Possible Duplicate:
Variable declaration in c# switch statement
What is the purpose of the extra braces in Switch case?

Today I was writing a small piece of code and, at debugging time, VS slapped me and told me I made a big mistake with my local variables names: “A local variable named ‘i’ is already defined in this scope”.

MyEnum enumerator = MyEnum.B;

switch (enumerator)
{
    case MyEnum.A:
        Int32 i = 0;
        // DoStuffsA();
        break;

    case MyEnum.B:
        Double i = 0D;
        // DoStuffsB();
        break;
}

So I said “Ok VS, let me fix it in a moment, I want to scope my cases first as I like them more!”. So I made my cosmetic change… but suddenly VS stopped whining about that error:

MyEnum enumerator = MyEnum.B;

switch (enumerator)
{
    case MyEnum.A:
    {
        Int32 i = 0;
        // DoStuffsA();
        break;
    }

    case MyEnum.B:
    {
        Double i = 0D;
        // DoStuffsB();
        break;
    }
}

Ok… I’m aware of the meaning of “scope” and I know I produced two different scopes like so. The problem is that I don’t know how switch statements are translated into IL code and I always thought that all the case statements were placed into the switch scope no matter if curly braces were used or not. So… the following oddity can be compiled, right?

MyEnum enumerator = MyEnum.B;

switch (enumerator)
{
    case MyEnum.A:
        String s = "Hello!";
        Console.WriteLine(s);
        break;

    case MyEnum.B:
        s = "Goodbye!";
        Console.WriteLine(s);
        break;
}

But not the following one:

MyEnum enumerator = MyEnum.B;

switch (enumerator)
{
    case MyEnum.A:
    {
        String s = "hello";
        Console.WriteLine(s);
        break;
    }

    case MyEnum.B:
    {
        s = "goodbye";
        Console.WriteLine(s);
        break;
    }
}

Can some explain me better the behavior of the compiler in this situation?
Many thanks!

  • 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-06-17T16:46:44+00:00Added an answer on June 17, 2026 at 4:46 pm

    Firstly, the translation to IL is somewhat irrelevant. IL doesn’t have the same concepts of scope at all. This is all about the C# language specification.

    It’s pretty simple, really: the scope of a local variable is the block in which it’s declared, and you can’t declare one local variable when another local variable of the same name is in scope.

    • In your first snippet of code, the scope of i is the whole switch statement. Your second declaration tries to declare another i variable within the same scope, thus it fails.

    • In your second snippet of code, the scope of each i variable is just the block created by the curly braces – they’re in entirely separate scopes, no nesting, so it’s fine.

    • In your third snippet of code, the variable s is in scope for the second case, as its scope is the entire switch statement – so it’s fine to use it.

    • In your fourth snippet of code, the variable s is only in the scope of the block in which it’s declared (inside case A) so it’s not in scope in case B.

    I would strongly recommend against using code like your third snippet of code though. It will confuse many people. If you really want a variable which is in scope for multiple cases, I’d declare it before the switch statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Variable declaration in c# switch statement I've always wonderd : when i
Possible Duplicate: Case Statement Block Level Declaration Space in C# For example: string danger;
Possible Duplicate: Properties and instance variable declaration Whats the difference between the following two:
Possible Duplicate: Tuple parameter declaration and assignment oddity In Scala, one can do multiple-variable
Possible Duplicate: Variable declaration placement in C I really dont understand why when I
Possible Duplicate: C/C++: Passing variable number of arguments around According to the function declaration
Possible Duplicate: Using variable in SQL LIKE statement I want it to be like
Possible Duplicate: Why are variables not local in case statements? A variable defined in
Possible Duplicate: Why can't variables be declared in a switch statement? I have a
Possible Duplicate: Javascript Variable Variables Please take a look at code below var selected_id=1;

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.