Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a reference type and value type in c#?

Some guy asked me this question couple of months ago and I couldn't explain it in detail. What is the difference between a reference type and a value type in C#?

I know that value types are int, bool, float, etc and reference types are delegate, interface, etc. Or is this wrong, too?

Can you explain it to me in a professional way?

like image 467
tugberk Avatar asked Feb 20 '11 13:02

tugberk


People also ask

What is meant by value type and reference type in C#?

Value types and reference types are the two main categories of C# types. A variable of a value type contains an instance of the type. This differs from a variable of a reference type, which contains a reference to an instance of the type.

Why class is reference type and structure is value type?

Structs are value types, while classes are reference types, and the runtime deals with the two in different ways. When a value-type instance is created, a single space in memory is allocated to store the value. Primitive types such as int, float, bool and char are also value types, and work in the same way.


2 Answers

Your examples are a little odd because while int, bool and float are specific types, interfaces and delegates are kinds of type - just like struct and enum are kinds of value types.

I've written an explanation of reference types and value types in this article. I'd be happy to expand on any bits which you find confusing.

The "TL;DR" version is to think of what the value of a variable/expression of a particular type is. For a value type, the value is the information itself. For a reference type, the value is a reference which may be null or may be a way of navigating to an object containing the information.

For example, think of a variable as like a piece of paper. It could have the value "5" or "false" written on it, but it couldn't have my house... it would have to have directions to my house. Those directions are the equivalent of a reference. In particular, two people could have different pieces of paper containing the same directions to my house - and if one person followed those directions and painted my house red, then the second person would see that change too. If they both just had separate pictures of my house on the paper, then one person colouring their paper wouldn't change the other person's paper at all.

like image 177
Jon Skeet Avatar answered Sep 23 '22 02:09

Jon Skeet


Value type:

Holds some value not memory addresses

Example:

Struct

Storage:

TL;DR: A variable's value is stored wherever it is decleared. Local variables live on the stack for example, but when declared inside a class as a member it lives on the heap tightly coupled with the class it is declared in.
Longer: Thus value types are stored wherever they are declared. E.g.: an int's value inside a function as a local variable would be stored on the stack, whilst an in int's value declared as member in a class would be stored on the heap with the class it is declared in. A value type on a class has a lifetype that is exactly the same as the class it is declared in, requiring almost no work by the garbage collector. It's more complicated though, i'd refer to @JonSkeet's book "C# In Depth" or his article "Memory in .NET" for a more concise explenation.

Advantages:

A value type does not need extra garbage collection. It gets garbage collected together with the instance it lives in. Local variables in methods get cleaned up upon method leave.

Drawbacks:

  1. When large set of values are passed to a method the receiving variable actually copies so there are two redundant values in memory.

  2. As classes are missed out.it losses all the oop benifits

Reference type:

Holds a memory address of a value not value

Example:

Class

Storage:

Stored on heap

Advantages:

  1. When you pass a reference variable to a method and it changes it indeed changes the original value whereas in value types a copy of the given variable is taken and that's value is changed.

  2. When the size of variable is bigger reference type is good

  3. As classes come as a reference type variables, they give reusability, thus benefitting Object-oriented programming

Drawbacks:

More work referencing when allocating and dereferences when reading the value.extra overload for garbage collector

like image 32
Durai Amuthan.H Avatar answered Sep 22 '22 02:09

Durai Amuthan.H