.NET值类型的初始化 [英] .net value type initialization

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

问题描述

这是在存储器中的值,例如,整数值(int)的后声明,但是未初始化?在CLR六C#里氏写道,值类型与0初始化,但不允许使用。 那么什么将是存储声明变量这样的后

What is the value in memory of, for example, integer value (int) after declaration but not initialization? In "CLR vi C#" Richter writes, that value types is initialized with 0, but not allowed to be used. So what will be in memory after declaration of variable like this

int testVar;

和如何初始化检查落实的机制?

And how is mechanism of initializing check implemented?

推荐答案

类型与内存为全零初始化。我不知道这是否是根据规范所有的值类型,所以你不能指望这个,除非你检查。对于不同类型的值在内存中的零可能意味着不同的事情取决于什么类型的再presents。

Types are initialized with memory that is all zeros. I don't know if this is according to the specification for all value types so you can't count on this unless you check. For different value types zeros in memory can mean different things depending on what the type represents.

值类型是自动初始化的,可用于当它们是一类的场和不局部变量。据我所知,在CLR本身没有初始化检查。初始化检查由编译器执行,并报告时,未初始化的变量是用来编译时错误。

Value types are auto initialized and can be used when they are a field of a class and not local variables. As far as I know there is no initialization check in the CLR itself. The initialization check is performed by the compiler and it reports compile time error when uninitialized variable is used.

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

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