How do memory-managed .net manage data types inside objects?

public class MyClass
{
    public int Age;
    public int ID;
}

public void MyMethod() 
{
    MyClass m = new MyClass();
    int newID;
}

As far as I understand, the following is true:

  • The m link lives on the stack and goes out of scope when MyMethod () exits.
  • The value type newID lives on the stack and goes out of scope when MyMethod () exits.
  • The object created by the new statement is on the heap and becomes regenerable by the GC when MyMethod () exits, assuming that no other object reference exists.

Here is my question:

  • Do value types be used inside objects on the stack or heap?
  • Are box / unboxing value types in an object a problem?
  • Are there any detailed but understandable resources on this topic?

, , , , , .

Edit:

:

+5
6

. ; , ?

, CLR, . , CLR , .

, , .

public class EmbeddedValues
{
  public int NumberField;
}

.

public class EmbeddedTest
{
  public void TestEmbeddedValues()
  {
    EmbeddedValues valueContainer = new EmbeddedValues();

    valueContainer.NumberField = 20;
    int publicField = valueContainer.NumberField;
  }
}

MSIL, .NET Framework SDK, IL EmbeddedTest.TestEmbeddedValues ​​()

.method public hidebysig instance void  TestEmbeddedValues() cil managed
{
  // Code size       23 (0x17)
  .maxstack  2
  .locals init ([0] class soapextensions.EmbeddedValues valueContainer,
           [1] int32 publicField)
  IL_0000:  nop
  IL_0001:  newobj     instance void soapextensions.EmbeddedValues::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldc.i4.s   20
  IL_000a:  stfld      int32 soapextensions.EmbeddedValues::NumberField
  IL_000f:  ldloc.0
  IL_0010:  ldfld      int32 soapextensions.EmbeddedValues::NumberField
  IL_0015:  stloc.1
  IL_0016:  ret
} // end of method EmbeddedTest::TestEmbeddedValues

, CLR stfld "20" EmbeddValues ​​ "NumberField" . , ldfld, . /.

+9
  • , .
  • ints .
+2

, , - CLR # . , .NET. , , , . . . , . . , , . , , , IL . unbox IL. , . , . () , . - , .

+2
  • Ans # 1: . "Essential.Net Vol 1"

(RT) , . , (VT) . var VT, CLR . VT, CLR /, .

  • Ans # 2: . / . obInstance.VT_typedfield .

    RT- , . 2 RT var . , VT . 2 VT var (struct)

  • Ans # 3: Don Box Essential.net/Jeffrey Richter CLR #. ... .Net-

+2

?

. , .

/unboxing ?

.

- , ?

+1 .

+1

. ,

struct Foo {public int x,y; int z;}

Foo bar; , bar.x, bar.y bar.z , bar. bar int. , bar, , bar , bar_x, bar_y bar_cantaccessthis_z [ bar, , , - -].

Recognizing structured type storages as field aggregation is the first step in understanding structures. Trying to look at them as some kind of object may seem "simpler", but does not match the way they actually work.

0
source

All Articles