开发者

Determining if a type is a reference type or value type

I've come from a Assembler and C/C++ background, so I understand the concept behind reference types versus value types in vb.net. Also, I've read Jon Skeet's article regarding references and value types and I understand all of that.

My question is: How can you tell if a given type is a reference type or a value type?

Is it simply that all integral types (ints, floats, etc.) are value types and all classes are reference types? (If so, where do strings fall?)


Question 2 (related): Is there a way to declare a class as a value class ver开发者_开发知识库sus a reference class? For example (using extreme brevity):

Public Class MyClass1
    Public Value As Integer
End Class

Using this class:

    Dim test1 As New MyClass1
    test1.Value = 1

    Dim test2 As MyClass1
    test2 = test1
    test2.Value = 2

At the end of this code, the Value in Test1 is 2. Clearly, MyClass1 is a reference type. But, what is it that causes it to be such and not a value type?


In general - enums and structs are value types, classes interfaces and delegates are reference types.

As for declaring a class as a value type - this is not possible, but C# structs are very close to classes and are value types.

As for VB.NET, I believe the equivalent is the Structure statement:

The Structure statement defines a composite value type that you can customize.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜