未分配的变量混乱 [英] Unassigned variables Confusion

查看:72
本文介绍了未分配的变量混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,如果我在使用之前没有指定* local *变量,

编译器将生成使用未分配的局部变量。错误。我没有得到的是为什么编译器不会隐式地为我的未分配变量分配一个默认值




此时你正准备回复我,并告诉我这是

只是编译器保护我免于引入可能的错误的一种方式

阻止我忘记分配值。问题是如果

这是真的,为什么它不能对字段级变量做同样的事情?我是b $ b意思是,我当然可以搞砸了吗?


我一直在网上搜索一个很好的答案而且我一直不幸在

找到一个好的回应。谢谢你的时间。

I understand that if I don''t assign a *local* variable before I use it, the
compiler will generate a "Use of unassigned local variable" error. What I
don''t get is why doesn''t the compiler just implicitly assign a default value
to my unassigned variable?

At this point you are getting ready to reply to me and tell me that this is
just a way for the compiler to protect me from introducing a possible bug by
preventing me from forgetting to assign the value. The problem is that if
that was true, why doesn''t it do the same thing for field level variables? I
mean, surely I could screw up there too right?

I have been scouring the web for a good answer and I have been unlucky in
finding a good response. Thank you for you time.

推荐答案

嗨Rene,


字段级变量将被初始化为默认值构建

实例时的值。由于编译器负责这个,你没有

来明确赋值。


HTH,

Rakesh Rajan


" Rene"写道:
Hi Rene,

Field level variables will be initialized to default values when the
instance is contructed. Since the compiler takes care of this, you don''t have
to explicitly assign the values.

HTH,
Rakesh Rajan

"Rene" wrote:
我明白如果我在使用它之前没有分配* local *变量,
编译器将生成一个使用未分配的局部变量 ;错误。我没有得到的是为什么编译器没有隐式地为我的未分配变量分配一个默认值

此时你准备好了回复我并告诉我,这只是编译器保护我免于引入可能的错误的一种方式,这可以防止我忘记分配值。问题是,如果这是真的,为什么它不能对字段级变量做同样的事情?我的意思是,我当然可以在那里搞砸了吗?

我一直在网上搜索一个很好的答案,我很不走运,找到了一个好的回答。谢谢你的时间。
I understand that if I don''t assign a *local* variable before I use it, the
compiler will generate a "Use of unassigned local variable" error. What I
don''t get is why doesn''t the compiler just implicitly assign a default value
to my unassigned variable?

At this point you are getting ready to reply to me and tell me that this is
just a way for the compiler to protect me from introducing a possible bug by
preventing me from forgetting to assign the value. The problem is that if
that was true, why doesn''t it do the same thing for field level variables? I
mean, surely I could screw up there too right?

I have been scouring the web for a good answer and I have been unlucky in
finding a good response. Thank you for you time.



那么为什么编译器对局部变量做同样的事情呢?


" Rakesh Rajan" <太阳神********* @ discussions.microsoft.com>在消息中写道

新闻:D7 ********************************** @ microsof t.com ...
So why doesn''t the compiler do the same thing for the local variables?

"Rakesh Rajan" <Ra*********@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
嗨Rene,

当构建
实例时,字段级变量将被初始化为默认值。由于编译器会处理这个问题,所以你没有明确指定值。

HTH,
Rakesh Rajan

Rene写道:
Hi Rene,

Field level variables will be initialized to default values when the
instance is contructed. Since the compiler takes care of this, you don''t
have
to explicitly assign the values.

HTH,
Rakesh Rajan

"Rene" wrote:
我明白如果我在使用之前没有指定* local *变量,
编译器会生成一个使用未分配的局部变量错误。我没有得到的是为什么编译器不会隐式地为我的未分配变量赋予默认值


此时你我正准备回复我,告诉我这个
只是编译器保护我免于引入可能的错误的一种方式
通过
阻止我忘记分配价值。问题是,如果这是真的,那么为什么它不能对字段级别的变量做同样的事情呢?我的意思是,我当然可以在那里搞砸了吗?

我一直在网上搜索一个很好的答案,我很不走运,找到了一个好的回答。谢谢你的时间。
I understand that if I don''t assign a *local* variable before I use it,
the
compiler will generate a "Use of unassigned local variable" error. What I
don''t get is why doesn''t the compiler just implicitly assign a default
value
to my unassigned variable?

At this point you are getting ready to reply to me and tell me that this
is
just a way for the compiler to protect me from introducing a possible bug
by
preventing me from forgetting to assign the value. The problem is that if
that was true, why doesn''t it do the same thing for field level
variables? I
mean, surely I could screw up there too right?

I have been scouring the web for a good answer and I have been unlucky in
finding a good response. Thank you for you time.



嗨Rene,


看看它是这样的:

只有当你要使用它所包含的值的任何实际的

时,才声明一个局部变量。

如果编译器也设置了局部变量的默认值,

它们将被设置为null(对于类)等,这肯定会抛出

错误一旦你试图访问它的成员。


因此,为了确保你自己初始化本地var',我们有

这个设计。


HTH,

Rakesh Rajan


" Rene"写道:
Hi Rene,

Look at it like this:
You declare a local variable only when you are going to make any practical
use of the value it contains.
If the compiler had been setting default values for local variables as well,
they will either be set to null (for classes) etc., which will surely throw
an error as soon as you try to access it''s members.

Hence, to make sure that you yourself initialize the local var''s, we have
this design.

HTH,
Rakesh Rajan

"Rene" wrote:
那么为什么编译器不会对局部变量做同样的事情呢?

Rakesh Rajan <太阳神********* @ discussions.microsoft.com>在消息中写道
新闻:D7 ********************************** @ microsof t.com。 ..
So why doesn''t the compiler do the same thing for the local variables?

"Rakesh Rajan" <Ra*********@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
嗨Rene,

当构建
实例时,字段级变量将被初始化为默认值。由于编译器会处理这个问题,所以你没有明确指定值。

HTH,
Rakesh Rajan

Rene写道:
Hi Rene,

Field level variables will be initialized to default values when the
instance is contructed. Since the compiler takes care of this, you don''t
have
to explicitly assign the values.

HTH,
Rakesh Rajan

"Rene" wrote:
我明白如果我在使用之前没有指定* local *变量,
编译器会生成一个使用未分配的局部变量错误。我没有得到的是为什么编译器不会隐式地为我的未分配变量赋予默认值


此时你我正准备回复我,告诉我这个
只是编译器保护我免于引入可能的错误的一种方式
通过
阻止我忘记分配价值。问题是,如果这是真的,那么为什么它不能对字段级别的变量做同样的事情呢?我的意思是,我当然可以在那里搞砸了吗?

我一直在网上搜索一个很好的答案,我很不走运,找到了一个好的回答。谢谢你的时间。
I understand that if I don''t assign a *local* variable before I use it,
the
compiler will generate a "Use of unassigned local variable" error. What I
don''t get is why doesn''t the compiler just implicitly assign a default
value
to my unassigned variable?

At this point you are getting ready to reply to me and tell me that this
is
just a way for the compiler to protect me from introducing a possible bug
by
preventing me from forgetting to assign the value. The problem is that if
that was true, why doesn''t it do the same thing for field level
variables? I
mean, surely I could screw up there too right?

I have been scouring the web for a good answer and I have been unlucky in
finding a good response. Thank you for you time.




这篇关于未分配的变量混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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