默认值为类成员 [英] default-values to class members

查看:74
本文介绍了默认值为类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们没有为我们的类指定

的默认构造函数,那么C#编译器为我们提供了自己的

为零(或者为数据成员分配默认值?

我为我的类编写了一个无参数的构造函数,其中有一个

空函数体。然后我实例化了一个对象并尝试打印它的值,令人惊讶的是成员已经初始化了。

但是如果我没有包含任何代码来实现这个怎么可能呢? />
所以。编译器显然没有为
初始化生成构造函数,因为我有自己的构造函数(没有任何参数)。

当我编写构造函数时,会发生同样的事情参数,

在这种情况下,同样,C#不提供默认构造函数,但

但值已初始化。


我尝试使用ILDASM来查看发生了什么,..和System.Object的

构造函数在构造函数的开头被调用了

body。

这是编译器生成的唯一代码。

是System.Object的构造函数然后执行
$ b的工作$ b初始化

成员。这没有多大意义,因为基类'

构造函数如何才能访问派生类的成员!


感谢任何帮助,


~乔尔

Is it true that if we don''t specify a default constructor for
our class, then the C# compiler provides us with its own
that zeroes (or assigns default values) to the data members?

I wrote a no-parameter constructor for my class with an
empty function body. I then instantiated an object and tried
printing its values, amazingly the members were already initialized.
But how is this possible if I have not included any code for doing
so. The compiler obviously hasn''t generated a constructor for
initialization because I have my own constructor (without any params).
The same thing happens when I write a constructor with parameters,
in this case, again, C# does not provide a default constructor but
yet the values are get initialized.

I tried using ILDASM to see what was going on, .. and System.Object''s
constructor was being called at the beginning of my constructor''s
body.
This was the only code that the compiler had generated.
Is it System.Object''s constructor then that does the work of
initializing
the members. This doesn''t make much sense because how can base class''s
constructor get access to derived class''s members!

Thanks for any help,

~ Joel

推荐答案

Joel写道:
Joel wrote:

如果我们没有为我们的类指定

的默认构造函数,那么C#编译器为我们提供了自己的

为数据成员归零(或指定默认值)?

我为我的类编写了一个无参数的构造函数,其中有一个

空函数身体。然后我实例化了一个对象并尝试打印它的值,令人惊讶的是成员已经初始化了。

但是如果我没有包含任何代码来实现这个怎么可能呢? />
所以。编译器显然没有为
初始化生成构造函数,因为我有自己的构造函数(没有任何参数)。

当我编写构造函数时,会发生同样的事情参数,

在这种情况下,同样,C#不提供默认构造函数,但

但是这些值是初始化的。
Is it true that if we don''t specify a default constructor for
our class, then the C# compiler provides us with its own
that zeroes (or assigns default values) to the data members?

I wrote a no-parameter constructor for my class with an
empty function body. I then instantiated an object and tried
printing its values, amazingly the members were already initialized.
But how is this possible if I have not included any code for doing
so. The compiler obviously hasn''t generated a constructor for
initialization because I have my own constructor (without any params).
The same thing happens when I write a constructor with parameters,
in this case, again, C# does not provide a default constructor but
yet the values are get initialized.



会员在内存中的位置。


内存中的位置总是有值。


只有几种可能性:

*语言/框架没有初始化所以他们

保留上次使用的价值

*语言/框架确实初始化


..NET init因此它们变为0,0.0,null等..


Arne

Members are in the and locations in memory.

Locations in memory always has a value.

There are only a couple possibilities:
* the language/framework does not init so they
keep their values from last usage
* the language/framework does init

..NET init so they become 0, 0.0, null etc..

Arne


内存中的位置始终具有值。
Locations in memory always has a value.

>

只有几种可能性:

*语言/框架没有初始化他们

保留上次使用的价值

*语言/框架确实初始化


.NET初始化它们变为0, 0.0,null等..
>
There are only a couple possibilities:
* the language/framework does not init so they
keep their values from last usage
* the language/framework does init

.NET init so they become 0, 0.0, null etc..



这是非常明显的,但我正在寻找默认的确切行为

构造函数由csharp编译器生成。


谢谢


Joel

That''s very obvious, but I''m looking for exact behavior of the default
constructor generated by the csharp compiler.

Thanks

Joel


编译器会为您执行初始化。以下是C#规范第5.2节中的一些

信息,你可以找到

at:
http://msdn2.microsoft.com/en-us/vcsharp/aa336809.aspx


5.2默认值

以下类别的变量会自动初始化为

其默认值:

a?¢静态变量。

a?¢类实例的实例变量。

a?¢数组元素。

变量的默认值取决于类型变量

并确定如下:

a?¢对于值类型的变量,默认值与
$ b $相同b值由value-typea的默认构造函数计算(?§a?? 0)。

a?¢对于reference-type的变量,默认值为null。

初始化为默认值通常是通过
$来完成的b $ b内存管理器或垃圾收集器在分配使用之前将内存初始化为all-bits-zero

。因此,使用all-bits-zero表示空引用很方便




========== ======

Clay Burch

The compiler does the initializations for you. Here is some
information from section 5.2 of C# specifications which you can find
at:
http://msdn2.microsoft.com/en-us/vcsharp/aa336809.aspx

5.2 Default values
The following categories of variables are automatically initialized to
their default values:
a?¢ Static variables.
a?¢ Instance variables of class instances.
a?¢ Array elements.
The default value of a variable depends on the type of the variable
and is determined as follows:
a?¢ For a variable of a value-type, the default value is the same as the
value computed by the value-typea??s default constructor (?§a??0).
a?¢ For a variable of a reference-type, the default value is null.
Initialization to default values is typically done by having the
memory manager or garbage collector initialize memory to all-bits-zero
before it is allocated for use. For this reason, it is convenient to
use all-bits-zero to represent the null reference.

================
Clay Burch


这篇关于默认值为类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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