The Common Type System, Variables and Conversions

4.1 Value Types vs Reference Types

Value types Inline (stack or inside an object) Copies the value

Integer, Double, Decimal, Boolean, Date, Char, Structure, Enum Reference types Heap (the variable holds a reference) Copies the reference

String, Object, arrays, Class, delegates, interfaces All types derive from System.Object. Value types derive from System.ValueType.

Boxing: converting a value type to Object (a heap allocation). Unboxing: casting it back.

4.2 Built-in Types

Byte / SByte Short / UShort Int16 / UInt16 Integer / UInteger Int32 / UInt32 Default integer type Long / ULong Int64 / UInt64 Single / Double Single / Double IEEE 754 floating point Base-10, 28–29 digits: money UTF-16 code unit Immutable UTF-16 text

Guid, TimeSpan, DateTimeOffset, DateOnly, TimeOnly, BigInteger, Half, Int128 BCL types used directly

4.3 Declarations

Dim count As Integer = 0
Dim total = 0D                          ' Decimal via suffix
Dim name As String = Nothing
Dim a, b As Integer                     ' both Integer
Static invocations As Integer           ' persists across calls
Const TaxRate As Decimal = 0.18D
Private ReadOnly _created As Date = Date.Now
Dim hex = &HFF, bin = &B1111_0000, big = 1_000_000L
Dim c = "A"c, dt = #2025-10-24#

4.4 Conversions

Integer → Long, Single → Double CInt, CLng, CDbl, CDec, CStr, CBool, CDate, CType(x, T)

Compiler intrinsics. Banker's rounding for CInt.

DirectCast(x, T) Exact type/inheritance or unboxing. Throws InvalidCastException.

TryCast(x, T) Reference types only. Returns Nothing.

Integer.Parse, Integer.TryParse, Decimal.TryParse(s, NumberStyles, CultureInfo, result)

Convert.ToInt32, Convert.ToBase64String Widening/Narrowing Operator CType User-defined (Module 12)

Dim input = "42.5"
Dim value As Decimal
If Decimal.TryParse(input, Globalization.NumberStyles.Number, Globalization.CultureInfo.InvariantCulture, value) Then
    Console.WriteLine(value * 2)
End If
Dim o As Object = 5            ' boxing
Dim n = DirectCast(o, Integer) ' unboxing

4.5 Default Values and Nothing

Nothing assigned to a value type gives its default (0, False, #1/1/0001#). Assigned to a reference type, it gives a null reference.

Dim x As Integer? declares a nullable value type (Module 22).