Value Types Versus Reference Types


A value type is a relatively simple data type such as an int or float that represents the data it contains directly. If you declare an int variable named numItems and assign it the value 27, the program allocates a chunk of memory and stores the value 27 in it.

In contrast, a reference type variable contains a reference to another piece of memory that actually contains the variable’s data. For example, suppose you define an OrderItem class that has PartNumber, PriceEach, and Quantity properties. Now suppose your program creates an OrderItem object named item1 that has PartNumber = 3618, PriceEach = 19.95, and Quantity = 3. The program allocates a chunk of memory to hold those property values. It also creates another piece of memory that is a reference to the first piece of memory. The variable named item1 is actually this reference and not the memory containing the properties.

 

Source: C# 5.0 Programmer’s Reference


Leave a Reply

Your email address will not be published. Required fields are marked *