class cA
{
   int B;
}
请问...
.net中
类是引用类行..实例化时是在堆中分配,但int是值类型(值类型在栈中分配)...
那麽实例化cA类时..他的int B是在堆中还是在栈中....
请说明下原因...谢谢...

解决方案 »

  1.   

    应该是在栈中。如果在WIN32里面,你跟踪程序的运行就一目了然了,但在。net中,跟不到哦。
      

  2.   

    上面的说法只是个推测....虽然可以用c++不在.net中跟踪..
    但.net 必竟和其它的编译器不一样啊...还有谁有更好的解答吗?
    在线等...
      

  3.   

    应该在堆中分配,这时Int类型的B应该会被Boxing(装箱)
      

  4.   

    在stack上Although value types and reference types can be similar in terms of declaration syntax and usage, their semantics are distinct.Reference types are stored on the run-time heap; they may only be accessed through a reference to that storage. Because reference types are always accessed through references, their lifetime is managed by the .NET Framework. Outstanding references to a particular instance are tracked and the instance is destroyed only when no more references remain. A variable of reference type contains a reference to a value of that type, a value of a more derived type, or a null reference. A null reference is a reference that refers to nothing; it is not possible to do anything with a null reference except assign it. Assignment to a variable of a reference type creates a copy of the reference rather than a copy of the referenced value. For a variable of a reference type, the default value is a null reference.Value types are stored directly on the stack, either within an array or within another type; their storage can only be accessed directly. Because value types are stored directly within variables, their lifetime is determined by the lifetime of the variable that contains them. When the location containing a value type instance is destroyed, the value type instance is also destroyed. Value types are always accessed directly; it is not possible to create a reference to a value type. Prohibiting such a reference makes it impossible to refer to a value class instance that has been destroyed. Because value types are always NotInheritable, a variable of a value type always contains a value of that type. Because of this, the value of a value type cannot be a null reference, nor can it reference an object of a more derived type. Assignment to a variable of a value type creates a copy of the value being assigned. For a variable of a value type, the default value is the result of initializing each variable member of the type to its default value.The following example shows the difference between reference types and value types:Imports SystemClass Class1
        Public Value As Integer = 0
    End ClassModule Test
        Sub Main()
            Dim val1 As Integer = 0
            Dim val2 As Integer = val1
            val2 = 123
            Dim ref1 As Class1 = New Class1()
            Dim ref2 As Class1 = ref1
            ref2.Value = 123
            Console.WriteLine("Values: " & val1 & ", " & val2)
            Console.WriteLine("Refs: " & ref1.Value & ", " & ref2.Value)
        End Sub
    End Module
    The output of the program is:Values: 0, 123
    Refs: 123, 123
    The assignment to the local variable val2 does not impact the local variable val1 because both local variables are of a value type (the type Integer) and each local variable of a value type has its own storage. In contrast, the assignment ref2.Value = 123; affects the object that both ref1 and ref2 reference.Value types and reference types are unified under the type Object, which is the root type of all types. Because Object is neither a reference nor a value type, it can contain values of either kind and has semantics consistent with the kind of type contained within it at run time.The following example shows how Object can be used with both value types and reference types:Imports SystemClass Class1
        Public Value As Integer = 0
    End ClassStructure Struct1
        Public Value As Integer
    End StructureModule Test
        Sub Main()
            Dim val1 As Object = New Struct1()
            Dim val2 As Object = val1        val2.Value = 123        Dim ref1 As Object = New Class1()
            Dim ref2 As Object = ref1        ref2.Value = 123        Console.WriteLine("Values: " & val1.Value & ", " & val2.Value)
            Console.WriteLine("Refs: " & ref1.Value & ", " & ref2.Value)
        End Sub
    End Module
    The output of the program is:Values: 0, 123
    Refs: 123, 123
    The assignment to the field of the local variable val2 does not impact the field of the local variable val1 because both local variables are of a value type (the type Integer) and each local variable of a value type has its own storage. In contrast, the assignment ref2.Value = 123 affects the object that both ref1 and ref2 references.
      

  5.   

    msdn 2005 地址:ms-help://MS.VSCC.2003/MS.MSDNQTR.2005APR.1033/vbls7/html/vblrfVBSpec6_1.htm中文 msdn 2003 地址(翻译的乱七八糟,而且不如2005全):ms-help://MS.MSDNQTR.2003FEB.2052/vbls7/html/vblrfVBSpec6_1.htm
      

  6.   

    msdn online:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec6_1.asp内容和我的msdn2005 一样
      

  7.   

    Sunmast的听起来有点道理哦...有没有谁能证明一下...能不能用c++观察下..虽然不是在.net下但也可以代表些什么..
      

  8.   

    up一下。分少请大家不要见怪...必竟以知识为主。。相信这个问题。。也有部份人和我一样不太清楚...想学习的同志请up 下..谢谢~~~