What is the Difference between value and reference type?

Answer Posted / himani

In simple words, all value based types are allocated on the
stack, while all reference based types are allocated on the
heap. What does this mean? A value type contains the actual
value. A reference type contains a reference to the value.
When a value type is assigned to another value type, it is
copied. When a reference type is assigned to another
reference type, a reference is assigned to the value.

By saying stack, we mean things are kept one on top of the
other. We keep track of each value at the top. By saying
heap, we mean things are kept in a mashed order. We keep
track of each value by its address, that is referenced by a
pointer to it.

All value types are implicitly derived from
System.ValueType. This class actually overrides the
implementation in System.Object, the base class for all
objects which is a reference type itself.

Data types
like integers, floating point numbers, character data,
Boolean values
, Enumerations and Structures are examples of Value Types.
Classes, Strings, Arrays are examples of Reference Types.

A value type may not contain NULL values. Reference types
may contain NULL values.

It is not possible to derive new types from Value Types.
This is possible in Reference types. However, Value Types
like Structures can implement interfaces.

Is This Answer Correct ?    2 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

List the two important objects of ado.net and also list the namespaces that are commonly used in ado.net to aid in connection to a database.

521


What is the purpose of ienumerable in c#?

498


Why dictionary is used in c#?

544


What are the different ways of method can be overloaded?

458


What is COM Interoperability?

553






Give some examples for built in datatypes in c#?

552


Why singleton is sealed?

486


Where do we set the min and max pool size for connection pooling?

524


What are the advantages of using partial classes?

533


What are constructors in c#?

543


What is the difference between struct and class in c#?

478


Does c# support try-catch-finally blocks?

480


Can you specify nested classes as partial classes?

512


What are the fundamental principles of oo programming?

499


Explain how do I get deterministic finalization in c#?

472