tags:

views:

41

answers:

1

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 versus 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?

+3  A: 

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.

Oded
Thanks so much! I've been struggling to understand this for a while. The MSDN links help a lot.
Richard
Good answer. Structures in VB.net can also contain code, btw. They just can't save state
drventure