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

  • Home
  • SEARCH
  • 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 274379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:36:04+00:00 2026-05-12T00:36:04+00:00

Is there a reason I am missing that a block within a case statement

  • 0

Is there a reason I am missing that a block within a case statement isn’t considered a block level declaration space?

I keep getting an error (variable has already been declared) when I try

case x:
  var someVariable = 42;
break;
case y: 
   var someVariable = 40;
break;

but I can do

case x:
   try{var someVariable = 42;}catch{} 
break;
case y: 
    try{var someVariable = 40;}catch{}
break;

If C# allowed fall through statements, that would make sense, but it doesn’t, and I can’t think of a scenario where you can declare a variable in a case statement and use it outside of that block.

  • 1 1 Answer
  • 1 View
  • 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-12T00:36:05+00:00Added an answer on May 12, 2026 at 12:36 am

    UPDATE: This question was used as the inspiration for this blog post; see it for further details.

    http://ericlippert.com/2009/08/13/four-switch-oddities/

    Thanks for the interesting question.


    There are a number of confusions and mis-statements in the various other answers, none of which actually explain why this is illegal. I shall attempt to be definitive.

    First off, to be strictly correct, “scope” is the wrong word to use to describe the problem. Coincidentally, I wrote a blog post last week about this exact mis-use of “scope”; that will be published after my series on iterator blocks, which will run throughout July.

    The correct term to use is “declaration space“. A declaration space is a region of code in which no two different things may be declared to have the same name. The scenario described here is symptomatic of the fact that a switch section does not define a declaration space, though a switch block does. Since the OP’s two declarations are in the same declaration space and have the same name, they are illegal.

    (Yes, the switch block also defines a scope but that fact is not relevant to the question because the question is about the legality of a declaration, not the semantics of an identifier lookup.)

    A reasonable question is “why is this not legal?” A reasonable answer is “well, why should it be”? You can have it one of two ways. Either this is legal:

    switch(y)
    {
    case 1:  int x = 123; ... break;
    case 2:  int x = 456; ... break;
    }
    

    or this is legal:

    switch(y)
    {
    case 1:  int x = 123; ... break;
    case 2:  x = 456; ... break;
    }
    

    but you can’t have it both ways. The designers of C# chose the second way as seeming to be the more natural way to do it.

    This decision was made on July 7th, 1999, just shy of ten years ago. The comments in the notes from that day are extremely brief, simply stating “A switch-case does not create its own declaration space” and then giving some sample code that shows what works and what does not.

    To find out more about what was in the designers minds on this particular day, I’d have to bug a lot of people about what they were thinking ten years ago — and bug them about what is ultimately a trivial issue; I’m not going to do that.

    In short, there is no particularly compelling reason to choose one way or the other; both have merits. The language design team chose one way because they had to pick one; the one they picked seems reasonable to me.

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

Sidebar

Ask A Question

Stats

  • Questions 161k
  • Answers 161k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That sounds like a maintenance nightmare. I realize that there… May 12, 2026 at 11:48 am
  • Editorial Team
    Editorial Team added an answer Logic is wrong. You had to write select ' %… May 12, 2026 at 11:48 am
  • Editorial Team
    Editorial Team added an answer You were using the CustomDocumentProperties in the wrong way, and… May 12, 2026 at 11:48 am

Related Questions

We mostly tend to following the above best practice. Have a look at String
I am extending the Visual Studio 2003 debugger using autoexp.dat and a DLL to
Note: This was posted when I was starting out C#. With 2014 knowledge, I
So I've been brushing up on my Java skills as of late and have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.