C# 中的 Case 语句块级声明空间 [英] Case Statement Block Level Declaration Space in C#

查看:14
本文介绍了C# 中的 Case 语句块级声明空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否有遗漏 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/

感谢您提出有趣的问题.

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.

首先,严格来说,范围"是用来描述问题的错误词.巧合的是,我上周写了一篇关于范围"的确切误用的博客文章;这将在我关于迭代器块的系列之后发布,该系列将在整个 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.

正确使用的术语是声明空间".声明空间是一个代码区域,其中不能声明两个不同的事物具有相同的名称.此处描述的场景表明 switch section 没有定义声明空间,尽管 switch block 定义了. 由于 OP 的两个声明在同一个声明空间并且具有相同的名称,它们是非法的.

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;
}

或者这是合法的:

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 日做出的,距今不到十年.那天的注释中的注释非常简短,只是简单地说明一个 switch-case 不会创建自己的声明空间",然后给出一些示例代码来说明哪些有效,哪些无效.

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# 中的 Case 语句块级声明空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆