在哪里,为什么使用int类型的新= INT? [英] Where and why use int a=new int?

查看:136
本文介绍了在哪里,为什么使用int类型的新= INT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇,是什么样的区别是:

  int类型的= 100;

  INT A = INT新();

我知道新的用于分配的内存heap..but我真的没有在这里得到的上下文。


解决方案

 静态无效的主要()
{
    INT A = INT新();
    INT B =默认(INT);
    INT C = 100;
    Console.Read();
}

被编译成

 。方法私人hidebysig静态无效的主要()CIL管理
{
  。入口点
  // code尺寸15(0xF的)
  .maxstack 1
  .locals的init([0] INT32 A,
           [1] INT32 B,
           [2] INT32 C)
  IL_0000:NOP
  IL_0001:ldc.i4.0
  IL_0002:stloc.0
  IL_0003:ldc.i4.0
  IL_0004:stloc.1
  IL_0005:ldc.i4.s 100
  IL_0007:stloc.2
  IL_0008:调用INT32 [mscorlib程序] System.Console ::阅读()
  IL_000d:流行
  IL_000e:RET
} //方法程序结束::主要

正如你可以看到第一个只是初始化它,第二个是的一样的和第三个初始化并设置为 100 。而对于IL code产生的他们都得到了一行初始化

所以

  INT A = INT新();

是一样的。

  INT A =默认(INT);

Just curious, what is the difference between:

int A = 100;

and

int A = new int();  

I know new is used to allocate memory on the heap..but I really do not get the context here.

解决方案

static void Main()
{
    int A = new int();
    int B = default(int);
    int C = 100;
    Console.Read();
}

Is compiled to

.method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] int32 A,
           [1] int32 B,
           [2] int32 C)
  IL_0000:  nop
  IL_0001:  ldc.i4.0
  IL_0002:  stloc.0
  IL_0003:  ldc.i4.0
  IL_0004:  stloc.1
  IL_0005:  ldc.i4.s   100
  IL_0007:  stloc.2
  IL_0008:  call       int32 [mscorlib]System.Console::Read()
  IL_000d:  pop
  IL_000e:  ret
} // end of method Program::Main

As you can see first one just initialize it and second one is just the same and third one initialize and set to 100. As for the IL code generated, they both get initialized in a single line.

so

int A = new int();

Is the same as

int A = default(int);

这篇关于在哪里,为什么使用int类型的新= INT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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