不确定某事 [英] Unsure about Something

查看:67
本文介绍了不确定某事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里被告知以下内容相同:


< snippet 1>


int a;

for(int i = 0; i< 10; ++ i){

a = GetValueFromSomewhere();

}


< / snippet 1>


< snippet 2>


for(int i = 0; i< 10; ++ i){

int a = GetValueFromSomewhere();

}


< / snippet 2>


在第二个片段中,我不能在循环之外使用''a''。为什么?


例如,以下内容会导致编译错误(名称''''确实

在类或命名空间中不存在 ):


< snippet 3>


for(int i = 0; i< 10; ++ i){

int a = GetValueFromSomewhere();

}

a = AnotherValue();


< /代码段3>

I''ve been told on here that the following is equivalent:

<snippet 1>

int a;
for (int i = 0; i < 10; ++i) {
a = GetValueFromSomewhere();
}

</snippet 1>

<snippet 2>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}

</snippet 2>

In the second snippet, I cannot use ''a'' outside of the loop. Why?

For example, the following causes a compile error ("The name ''a'' does
not exist in the class or namespace"):

<snippet 3>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
a = AnotherValue();

</snippet 3>

推荐答案

这是预期的。您可能想要阅读一些关于变量范围的内容。在

片段2中,由于你在循环中声明了你的变量,所以它的范围只是在循环内部的b / b
。在代码片段1中,您将变量声明在

循环之外,因此它的范围位于循环内部并且位于当前结构内部,

是否是另一个结构或者是父类。


-Darrin


C#Learner < CS **** @ learner.here>在消息中写道

news:7h ******************************** @ 4ax.com ...
This is as expected. You may want to read a little about variable scope. In
snippet 2, since you declared your variable inside the loop, it''s scope is
only inside the loop. In snippet 1, you declared the variable outside of the
loop, so it''s scope is inside the loop and inside the current structure,
whether it be another structure or the parent class.

-Darrin

"C# Learner" <cs****@learner.here> wrote in message
news:7h********************************@4ax.com...
我在这里被告知以下内容相同:

<片段1>

int a;
for(int i = 0; i< 10; ++ i){
a = GetValueFromSomewhere();
}
< / snippet 1> <对于(int i = 0; i< 10; ++ i){
int a = GetValueFromSomewhere();
/>}

< / snippet 2>

在第二个片段中,我不能在循环之外使用''a''。为什么?

例如,以下原因导致编译错误(名称''''在类或命名空间中不存在'):

i = 0; i< 10; ++ i){
int a = GetValueFromSomewhere();
}
a = AnotherValue();

< / snippet 3>
I''ve been told on here that the following is equivalent:

<snippet 1>

int a;
for (int i = 0; i < 10; ++i) {
a = GetValueFromSomewhere();
}

</snippet 1>

<snippet 2>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}

</snippet 2>

In the second snippet, I cannot use ''a'' outside of the loop. Why?

For example, the following causes a compile error ("The name ''a'' does
not exist in the class or namespace"):

<snippet 3>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
a = AnotherValue();

</snippet 3>





" ; C#Learner < CS **** @ learner.here>在消息中写道

news:7h ******************************** @ 4ax.com ...

"C# Learner" <cs****@learner.here> wrote in message
news:7h********************************@4ax.com...
我在这里被告知以下内容相同:

<片段1>

int a;
for(int i = 0; i< 10; ++ i){
a = GetValueFromSomewhere();
}
< / snippet 1> <对于(int i = 0; i< 10; ++ i){
int a = GetValueFromSomewhere();
/>}

< / snippet 2>

在第二个片段中,我不能在循环之外使用''a''。为什么?

因为你在循环范围内声明了一个。变量声明

的范围在它们声明的块中,并且只能在块中和任何底层块中的

中访问。它的保护和正确

编码目的。

您将看到任何块类型的相同结果,包括


{

int a = 10;

}

a = 5;


例如,以下导致编译错误(名称''''在类或命名空间中不存在):

<片段3>

for(int i = 0; i< 10; ++ i){
int a = GetValueFromSomewhere();
}
a = AnotherValue();
< / snippet 3>
I''ve been told on here that the following is equivalent:

<snippet 1>

int a;
for (int i = 0; i < 10; ++i) {
a = GetValueFromSomewhere();
}

</snippet 1>

<snippet 2>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}

</snippet 2>

In the second snippet, I cannot use ''a'' outside of the loop. Why?
Because you declare a within the scope of the loop. Variable declarations
are scoped within the block they were declared in and are only accessible in
that block and in any underlying block. Its there for protection and correct
coding purposes.
You will see the same result with any block type, including

{
int a = 10;
}
a= 5;

For example, the following causes a compile error ("The name ''a'' does
not exist in the class or namespace"):

<snippet 3>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
a = AnotherValue();

</snippet 3>



嗨!


C#是一种块范围的语言,所以本地化变量只能在它们定义的块和该块中的任何子块的
中可见。 (一个块

由花括号{和}分隔。在你的第二个片段中,''a''是''for''语句'块中定义的
,因此在此之外是不可见的。


希望有所帮助!

" C#Learner" < CS **** @ learner.here>在消息中写道

news:7h ******************************** @ 4ax.com ...
Hi!

C# is a block-scoped language, so local variables are only visible within
the block they are defined in and any sub-blocks within that block. (A block
is delimited by curly braces, { and }). In your second snippet, ''a'' is
defined inside the ''for'' statement''s block so is invisible outside this.

Hope that helps!
"C# Learner" <cs****@learner.here> wrote in message
news:7h********************************@4ax.com...
我在这里被告知以下内容相同:

<片段1>

int a;
for(int i = 0; i< 10; ++ i){
a = GetValueFromSomewhere();
}
< / snippet 1> <对于(int i = 0; i< 10; ++ i){
int a = GetValueFromSomewhere();
/>}

< / snippet 2>

在第二个片段中,我不能在循环之外使用''a''。为什么?

例如,以下原因导致编译错误(名称''''在类或命名空间中不存在'):

i = 0; i< 10; ++ i){
int a = GetValueFromSomewhere();
}
a = AnotherValue();

< / snippet 3>
I''ve been told on here that the following is equivalent:

<snippet 1>

int a;
for (int i = 0; i < 10; ++i) {
a = GetValueFromSomewhere();
}

</snippet 1>

<snippet 2>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}

</snippet 2>

In the second snippet, I cannot use ''a'' outside of the loop. Why?

For example, the following causes a compile error ("The name ''a'' does
not exist in the class or namespace"):

<snippet 3>

for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
a = AnotherValue();

</snippet 3>



这篇关于不确定某事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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