新的Int32()用于c#?

Roy*_*mir 5 .net c# int .net-4.0

C#编译器允许我写:

int k=new Int32();
Run Code Online (Sandbox Code Playgroud)

但是,我无法为其分配任何值.

2个问题

1)没有new创建堆分配,如果是这样,堆栈< - >值的东西怎么样?

2)我无能为力int k=new Int32();.在什么情况下我会使用它?

Mar*_*ell 8

All structs (and int/Int32 is a struct) allow new() usage, which just means: initialise the struct to default values, I.e. zero for all fields in the struct. Most structs are immutable - and primitives certainly are. Basically, what you have written is the same as:

int k=0;
Run Code Online (Sandbox Code Playgroud)

你可以做很多事情......但你不能改变零的任何属性.零为零是零.

new 并不总是意味着"在堆上分配".