没有对象,C#如何直接将值分配分配给类中的字段? [英] without object, how c# handling the assignment of values directly to the fields in a class?

查看:106
本文介绍了没有对象,C#如何直接将值分配分配给类中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class a
{
int x=44;//how it is handling??

}



在c ++中是不可能的.
我认为这是使用CLR的默认构造函数完成的吗?

我只想知道,此类中的变量初始化是如何工作的,就像同一示例一样?
没有构造函数...



it is not possible in c++.
I think it is doing with the default constructor by CLR .Is it correct??

I just want to know, how the initialization of variable inside this class is working, like the same example??
there is no constructor ...

推荐答案

在调用任何构造函数之前,将变量的值(如果有的话)分配给它们.看一下这个: http://stackoverflow.com/questions/298183/c-sharp-member-variable-initialization-best-practice [ ^ ]
Variables are assigned their values (if any) just before any constructor is called. Take a look at this: http://stackoverflow.com/questions/298183/c-sharp-member-variable-initialization-best-practice[^]


堆(objects)中的项目的初始值为null.对于Value 类型(放入堆栈),该值趋向于0;否则,该值趋向于0.如果是布尔值,则返回false.为初始化设置的值显然会改变此设置,包括在构造函数中设置的值.
Items in the Heap (objects) will have an initial value of null. For Value types (put on the stack), the value tends to be 0; false if it is a boolean. The values set for initialization will obviously change this, including values set in the constructor.


这与默认构造函数一样.
当我们初始化字段时,CLR将使用此分配的值到默认构造函数中;当它转换为MSIL代码时,将将此值分配到默认构造函数中.

使用C ++ 程序无法进行此初始化,因为没有CLR,无法将此值复制到默认构造函数中..

如果要查看,背景中发生了什么.您可以通过ILDASM工具查看Class(数据和函数).
It is doing with the default constructor.
When we are initializing the fields, the CLR will use this assigned value into the default constructor.And when it will convert into MSIL code it will assign this value into the default constructor.

This initialization is not possible with C++ program because there is no CLR, to copy this value into default constructor..

IF you want to see,what is happening in background. You can see the Class( data and functions) through the ILDASM tool..


这篇关于没有对象,C#如何直接将值分配分配给类中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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