What is difference between Value types and Reference types in VB.NET or C# (Value types VS Reference types)?
292216-Nov-2010
Home / DeveloperSection / Interviews / What is difference between Value types and Reference types in VB.NET or C# (Value types VS Reference types)?
Amit Singh
16-Nov-2010C# provides two types—class and struct, class is a referencetype while the other is a value type. Here is difference between Value typesand Reference types.
Value types directly contain their data which are either allocated on the stackor allocated in-line in a structure. Reference types store a reference to thevalue's memory address, and are allocated on the heap. With reference types, anobject is created in memory, and then handled through a separate reference—ratherlike a pointer.
Reference types can be self-describing types, pointer types, or interfacetypes. Primitive types such as int, float, bool and char are value types.
Variables that are value types each have their own copy of the data, andtherefore operations on one variable do not affect other variables. Variablesthat are reference types can refer to the same object; therefore, operations onone variable can affect the same object referred to by another variable.
Value types have a default implied constructor that initializes the defaultvalue. Reference types default to a null reference in memory.
Value types derive from System.ValueType. Reference types derive fromSystem.Object.
Value types cannot derive a new type from an existing value type, except forstructs. Reference types can derive a new type from an existing reference typeas well as being able to implement interfaces.