9.3. Value types

C# value types are close to Java primitives, but there are significant differences. Value types will be your enums (see Chapter 25) and structs (see Chapter 26). Simple types (or primitive types of Java) such as int, byte, and float are special predefined struct types which you can use directly.

While reference types store a reference to an object on the heap, value types directly contain their data. As such, similar to the case of Java's primitives, when you assign one value type to another, a new value is created. Each of the value type variables will store their own copy of the values, and changing the value of one will not affect the value stored in the other. The code below illustrates this:

 1: using System; 2: 3: public ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.