Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, use of value types vs. reference types

My questions are:

  • When should we use value types and when reference types?
  • What are the advantages and disadvantages of one over other?
  • What if one uses reference types everywhere? Is there any harm in it?

Please also discuss advantages and disadvantages of each one. I want to understand that as well.

like image 424
Nawaz Avatar asked Jan 19 '11 18:01

Nawaz


2 Answers

You should use value types for small, immutable types which represent values.
Never make mutable structs.

For everything else, use reference types.

like image 163
SLaks Avatar answered Sep 19 '22 08:09

SLaks


Use value types for immutables that do not have an identity of their own (a 1 is a 1), use reference types for other things.

like image 41
Oded Avatar answered Sep 20 '22 08:09

Oded