How are Value Types different from Reference Types in .NET?

Value types and reference types fall under the same roof termed unified type system of C#. But still, they are different.




The table given below tabulates the difference between the two:

Value Types
Reference Types
Value types are allocated in stack Reference types are allocated in heap
Value type directly contain the data Reference type doesn't hold the data directly, instead they hold the memory address in which the data is stored
When you specify int num1 = num2, then the data in value type variable num2 is copied into num1. However, num1 and num2 have individual copies of data When you specify sampleClass obj2 = obj1, then both obj2 and obj1 are pointing to the same reference i.e. to the same memory location
System.ValueType is the base class of all value types. However System.ValueType is derived from System.Object All reference types are directly inherited from System.Object
To convert a value type to an Object and vice versa, you have to perform boxing and unboxing Reference types can directly be assigned to an Object and vice versa. It doesn't require boxing or unboxing to happen
Value types cannot accept null values unless otherwise you explicitly use null coalescence operator Reference types can accept null values
Value types cannot involve in inheritance. You cannot consider value type as a base class and derive from it further. However, there is an exception with structures. Structures cannot be derived further but they can implement one or more interfaces Reference types can be inherited further. For example assume that you have a class called sampleClass. You can inherit from it and create a new derived class sample2Class which can override properties and methods of base class
Memory used by the value type available in the stack will be freed when the value type variable has gone out of scope Memory management is automatically taken care of by garbage collector

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.