Friday 30 September 2011

What are Value types and Reference types?


                          Value types inherit from the System.ValueType class, which in turn, inherits from System.Object. However, you can not inherit directly from theSystem.ValueType class. If you try to inherit explicitly from System.Value, you'll get the C3838 compiler error. Value types have several special properties:
             Value types are stored on the stack. (We'll discover that shortly.)
             Value type objects have two representations: an unboxed form and a boxed form.
             Value types are accessed directly which means you don't need to use  the new operator.
             Value types are managed types, they are initialised to 0 when they are created.
              Value type instances are not under the control of the Garbage Collector.                     Value types are implicitly sealed, which means that no class can derive from them.
                                    In C#, structs are always value types and allocated on the stack.
Reference types inherit directly from System.Object, they offer many advantages over Value types:
             Reference types are stored on the managed heap, thus they are under the control of Garbage Collector.
             Two or more reference type variables can refer to a single object in the heap, allowing operations on one variable to affect the object referenced by the other variable.
The variable representing the instance contains a pointer to the instance of the class, it is dereferenced first if you want to access any of its members. In C#,Classes are always reference types and created on the managed heap

No comments:

Post a Comment