问题在C#中变量的作用域与其他语言 [英] Question on C# Variable Scope vs. Other Languages

查看:143
本文介绍了问题在C#中变量的作用域与其他语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我说,我以前从未使用过C#,我不知道这件事情了。

First of all, let me say that I've never used C# before, and I don't know about it much.

我是为我的学习编程语言考试与Sebesta的编程语言第九版一书的概念。我读了以下摘录后范围声明顺序(第246页)我有点有点纳闷:

I was studying for my "Programming Languages" exam with Sebesta's "Concepts of Programming Languages 9th ed" book. After I read the following excerpt from "Scope declaration order (on 246th page)", I got a little bit puzzled:

.. 。例如,在C99,C ++,Java的所有局部变量的范围是从他们的声明,其中这些声明出现在块的两端。然而,在C#中的一个块中声明的任何变量的作用域是整个街区,无论在块声明的位置,只要它不是一个嵌套块。同样是方法正确的。中的请注意,C#还要求所有变量之前声明它们被使用。因此,虽然一个变量的范围从声明块或子程序中出现该声明的顶部延伸,变量仍不能其声明

"...For example, in C99, C++, Java the scope of all local variables is from their declarations to the ends of the blocks in which those declarations appear. However, in C# the scope of any variable declared in a block is the whole block, regardless of the position of the declaration in the block, as long as it is not in a nested block. The same is true for methods. Note that C# still requires that all variables be declared before they are used. Therefore, although the scope of a variable extends from the declaration to the top of the block or subprograms in which that declaration appears, the variable still cannot be used above its declaration"

为什么C#的设计师做出这样的决定呢?是否有这样一个不寻常的决定任何具体的原因/优势?

Why did designers of C# make such decision? Is there any specific reason/advantage for such an unusual decision?

推荐答案

看一看埃里克利珀的有关的declaration 的空间,这给一些背景这些规则。

Have a look at Eric Lippert's posts about declaration spaces, which give some more background to these rules.

我通常引用链接的博文时,最相关的一点,但我觉得这些真的给一个更好的答案时,所有一起阅读。

I'd normally quote the most relevant bit when linking to blog posts, but I think these really give a better answer when all read together.

希望他将通过弹出并给予了很好的总结。

Hopefully he'll pop by and give a good summary.

这篇关于问题在C#中变量的作用域与其他语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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