实例化和值类型与引用类型的初始化 [英] Instantiation and Initialization of Value Types vs. Reference Types

查看:177
本文介绍了实例化和值类型与引用类型的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


INT数=新INT();




问题:




  1. 有关引用类型的的新的的运营商通过在分配内存创建类型的实例然后堆通过调用类的构造函数初始化。如上面所看到的,可以为一个值类型也这样做。对我来说,线之上意味着构造函数int()函数来初始化的数量的一个值。我已阅读,int是指向结构的 System.Int32 的关键字。因此,在Visual Studio中,我浏览到该结构的Int32。你瞧,没有构造存在。此预定义类型究竟是如何初始化为0,没有构造?​​


  2. 相关上面,有一个字段存储的值的Int32结构<? / p>


  3. 这两个自定义结构和类我可以创建一个的新的的关键字的新实例,没有结构或实际包含构造类。在这种情况下,没有初始化所有的结构/类包含的字段做了什么?那是碰巧内存堆栈/堆分配值/引用类型的唯一的事情?


  4. 最后,对于值类型,没有的是需要实例化和初始化关键字。究竟如何在一个较低的水平工作的?说我们做的 INT数= 5; 的。是它在某种程度上翻译成的 int类型的新= INT(); 1 = 5; 的?如果是这样,怎么样?




太感谢了!


解决方案

不存在。


[ System.Int32 的]构造< /块引用>

值类型有默认构造函数:




每个值类型都有初始化该类型的默认值一个隐含的默认构造函数




(报价结束)




有关上述,有没有在的Int32一个字段结构是存储价值?




是的,它:

  [Serializable接口] 
[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
[System.Runtime.InteropServices.ComVisible(真)]
公共结构的Int32:IComparable的,IFormattable,IConvertible
,IComparable的<的Int32>中IEquatable<&的Int32 GT;
,IArithmetic<&的Int32 GT; {
内部INT m_value; //<< ==这

公共const int的的MaxValue = 0x7FFFFFFF的;
公共const int的MINVALUE =选中((INT)为0x80000000);

}




对于双方自定义结构和类我可以创建新的实例使用new关键字,没有结构或实际包含一个构造函数的类。




这是不是完全正确的:不同于结构 S,并非所有的类都有一个隐含的默认构造函数;如果你定义一个构造函数的参数,编译器将删除你的类的默认构造函数。




在这种情况下,是没有初始化的完成所有的结构/类包含的字段?




所有未明确初始化设置为默认值。字段




说我们 INT数= 5; 。是它在某种程度上转化为 int类型的新= INT(); 1 = 5;




INT 是内置型。编译器知道如何产生IL它。该类型的 INT 被烤成的CIL编译器生成的分配:

  .locals的init([0] INT32一)
ldc.i4.s 5
stloc.0

第一行对应于 int类型的的声明,有无。最后两行做作业: 5 的值加载到寄存器,并将其存储在局部变量 A (索引零)。


int number = new int();

Questions:

  1. For reference types, the new operator creates an instance of the type by allocating memory on the heap then initializes it by calling the type's constructor. As seen above, you could do the same for a value type. To me, the line above means that the constructor int() is called to initialize number with a value. I have read that int is a keyword pointing to the struct System.Int32. Therefore, in Visual Studio, I navigate to the struct Int32. Lo and behold, no constructor exists. How exactly is this predefined type initialized to 0 without a constructor?

  2. Related to the above, is there a field in the Int32 struct that stores the value?

  3. Both for custom structs and classes I can create new instances with the new keyword, without the struct or class actually containing a constructor. In that case, is no initialization done at all of the fields the struct/class contains? Is the only thing that happens that memory is allocated on stack/heap for the value/reference type?

  4. Finally, for value types, no new keyword is needed for instantiation and initialization. How exactly does that work on a lower level? Say we do int number = 5;. Is it somehow translated to int a = new int(); a = 5;? If so, how?

Thanks a million!

解决方案

no constructor [of System.Int32] exists.

All value types have a default constructor:

Each value type has an implicit default constructor that initializes the default value of that type.

(end of quote)

Related to the above, is there a field in the Int32 struct that stores the value?

Yes, it does:

[Serializable]
[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)] 
[System.Runtime.InteropServices.ComVisible(true)]
    public struct Int32 : IComparable, IFormattable, IConvertible
        , IComparable<Int32>, IEquatable<Int32>
        , IArithmetic<Int32> {
        internal int m_value; // <<== Here it is

        public const int MaxValue = 0x7fffffff;
        public const int MinValue = unchecked((int)0x80000000);
        ...
}

Both for custom structs and classes I can create new instances with the new keyword, without the struct or class actually containing a constructor.

That is not entirely correct: unlike structs, not all classes have an implicit default constructor; if you define a constructor that takes parameters, the compiler removes the default constructor from your class.

In that case, is no initialization done at all of the fields the struct/class contains?

All of the fields that are not initialized explicitly are set to their default values.

Say we do int number = 5;. Is it somehow translated to int a = new int(); a = 5;?

int is a built-in type. The compiler knows how to generate IL for it. The type of int is "baked into" the CIL the compiler generates for the assignment:

.locals init ([0] int32 a)
ldc.i4.s   5
stloc.0

The first line corresponds to the declaration of int a, with or without new. The last two lines do the assignment: load the value of 5 into a register, and store it in the local variable a (at index zero).

这篇关于实例化和值类型与引用类型的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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