C#中的案例声明块级别声明空间 [英] Case Statement Block Level Declaration Space in C#
问题描述
我是否有理由不将case语句中的块视为块级声明空间?
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;
但是我可以做
case x:
try{var someVariable = 42;}catch{}
break;
case y:
try{var someVariable = 40;}catch{}
break;
如果C#允许使用直通语句,那将是有道理的,但那没有意义,而且我想不出一种情况,您可以在case语句中声明变量并在该块之外使用它.
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.
推荐答案
更新:此问题用作此博客文章的启发;看到它以获取更多详细信息.
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/
感谢有趣的问题.
在其他各种答案中也有许多困惑和错误陈述,但没有一个可以真正解释为什么这是非法的.我将尽力而为.
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.
首先,严格来说,"scope"是用于描述问题的错误词.巧合的是,我上周写了一篇博客文章,内容涉及对范围"的确切误用.它将在我的迭代器模块系列文章之后发布,该系列文章将在整个7月运行.
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.
正确使用的术语是"声明空间".声明空间是一个代码区域,在其中不能声明两个不同的事物具有相同的名称.此处描述的情况是以下事实的症状:尽管开关 block 定义了开关 section ,但没有定义声明空间.两个声明在同一个声明空间中并且具有相同的名称,它们是非法的.
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.
(是,切换块也定义了范围,但该事实与问题无关,因为问题与声明的合法性有关,而不与<标识符查询的strong>语义.)
(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;
}
或者这是合法的:
switch(y)
{
case 1: int x = 123; ... break;
case 2: x = 456; ... break;
}
但是您不能同时使用这两种方式. C#的设计师选择第二种方法似乎是更自然的方法.
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.
这项决定是在1999年7月7日做出的,距十年前还差.当天的注释中的注释非常简短,仅说明"一个开关箱不会创建自己的声明空间",然后提供一些示例代码来显示有效和无效的代码.
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.
这篇关于C#中的案例声明块级别声明空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!