class cA
{
int B;
}
请问...
.net中
类是引用类行..实例化时是在堆中分配,但int是值类型(值类型在栈中分配)...
那麽实例化cA类时..他的int B是在堆中还是在栈中....
请说明下原因...谢谢...
{
int B;
}
请问...
.net中
类是引用类行..实例化时是在堆中分配,但int是值类型(值类型在栈中分配)...
那麽实例化cA类时..他的int B是在堆中还是在栈中....
请说明下原因...谢谢...
但.net 必竟和其它的编译器不一样啊...还有谁有更好的解答吗?
在线等...
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.