What is the Difference between value and reference type?

Answer Posted / vishvam

What is the difference between value type and reference
type? Can a value type contain NULL values?

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 ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you know about Translate Accelerator?

592


What is oledb in c#?

505


Can main method be final?

471


What is arraylist?

540


Which attribute adorn a test class to be picked up by the NUnit GUI in the NUnit test framework?

553






What are c# i/o classes? What are the commonly used i/o classes?

516


Explain get and set accessor properties?

494


What is sqldataadapter in c#?

491


Can abstract class be instantiated c#?

515


What is use of abstract class in c#?

494


Why is aws serverless?

501


Can non-default constructors be used with single call sao?

525


What is meant by unicode characters?

569


Can I fly with a loop recorder?

479


What is difference between string and stringbuilder in c#?

465