日期:2014-05-18  浏览次数:21025 次

C#中struct与class
请问结构类型new 与不new 在内存中有什么区别吗?值类型是在栈里面分配空间,结构是值类型如果new了后还是在栈里面分配空间吗?
下面是两份代码,第一份结构类型没有new直接给它里面的引用类型传引用。第二份是结构类型new了后在给它里面的引用类型传引用。从IL代码中我们可以看到是有区别的,因为对IL还不太熟悉,不知道它们内存怎么分配的,而且也不知道是在什么时候分配的,所以望高人指点。

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace StructAndClass
{
  class Program
  {
  static void Main(string[] args)
  {

  ClassText classText;
  classText = new ClassText();
  classText.num = 5;
  classText.name = "Hello";

  StructText structText;
  structText.classText = classText;
   
  }
  }

  public class ClassText
  {
  public int num;
  public string name;
   
  }

  public struct StructText
  {
  public ClassText classText;
  public int n;
  }
}

Main函数的IL代码

.method private hidebysig static void Main(string[] args) cil managed
{
  .entrypoint
  // 代码大小 34 (0x22)
  .maxstack 2
  .locals init (class StructAndClass.ClassText V_0,
  valuetype StructAndClass.StructText V_1)
  IL_0000: nop
  IL_0001: newobj instance void StructAndClass.ClassText::.ctor()
  IL_0006: stloc.0
  IL_0007: ldloc.0
  IL_0008: ldc.i4.5
  IL_0009: stfld int32 StructAndClass.ClassText::num
  IL_000e: ldloc.0
  IL_000f: ldstr "Hello"
  IL_0014: stfld string StructAndClass.ClassText::name
  IL_0019: ldloca.s V_1
  IL_001b: ldloc.0
  IL_001c: stfld class StructAndClass.ClassText StructAndClass.StructText::classText
  IL_0021: ret
} // end of method Program::Main


第二份代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace StructAndClass
{
  class Program
  {
  static void Main(string[] args)
  {

  ClassText classText;
  classText = new ClassText();
  classText.num = 5;
  classText.name = "Hello";

  StructText structText = new StructText();
  structText.classText = classText;
  int x = structText.n;
   
  }
  }

  public class ClassText
  {
  public int num;
  public string name;
   
  }

  public struct StructText
  {
  public ClassText classText;
  public int n;
  }
}

Main函数的IL代码
.method private hidebysig static void Main(string[] args) cil managed
{
  .entrypoint
  // 代码大小 42 (0x2a)
  .maxstack 2
  .locals init (class StructAndClass.ClassText V_0,
  valuetype StructAndClass.StructText V_1)
  IL_0000: nop
  IL_0001: newobj instance void StructAndClass.ClassText::.ctor()
  IL_0006: stloc.0
  IL_0007: ldloc.0
  IL_0008: ldc.i4.5
  IL_0009: stfld int32 StructAndClass.ClassText::num
  IL_000e: ldloc.0
  IL_000f: ldstr "Hello"
  IL_0014: stfld