5

Possible Duplicate:
What are the differences between value types and reference types in C#?

what are the basics differences between values types and rereference types

Community
  • 1
  • 1
Raghav
  • 51
  • 1
  • 1
  • 2
  • Possible dupe: http://stackoverflow.com/questions/3272861/reference-and-value-types-scenario , or http://stackoverflow.com/questions/1658985/what-is-a-value-class-and-what-is-a-reference-class-in-c. So, now you have two Jon Skeet answers - lucky you! – Grant Thomas Mar 31 '11 at 14:29

5 Answers5

19

Consider two variables:

SomeReferenceType x;
SomeValueType y;

The value of x is a reference - it will either be null or a reference to an object which is itself an instance of SomeReferenceType or a derived class. The value of x is not, in itself, the object.

The value of y is the data itself - if SomeValueType has three fields, the value of y will directly contain those fields.

That's a very brief summary - see Eric Lippert's blog post about value types and my article for more information. (You might also be interested in my article about parameter passing which is related, but not quite the same.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
8

Value types, as name tells, are values stored in memory; referencer types are (a kind of) pointer to an object (a class, an object, etc...)

From Microsoft:

A data type is a value type if it holds the data within its own memory allocation. A reference type contains a pointer to another memory location that holds the data.

Value Types
Value types include the following:

  • All numeric data types
  • Boolean, Char, and Date
  • All structures, even if their members are reference types
  • Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong

Reference Types
Reference types include the following:

  • String
  • All arrays, even if their elements are value types
  • Class types, such as Form
  • Delegates
Marco
  • 56,740
  • 14
  • 129
  • 152
  • David Chappell's article is mistaken/misleading then... See http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx (Only downvoted because of the stack/heap part. Remove that and I'll remove the downvote.) – Jon Skeet Mar 31 '11 at 14:32
  • @Jon Skeet: I've just removed that part, sorry for my mistaken link – Marco Mar 31 '11 at 14:34
  • 1
    Righto. I suggest you remove the "List item" bits as well :) – Jon Skeet Mar 31 '11 at 14:35
  • @Jon Skeet: you're always right... I still feel a little bit confused with this editor... sorry again... – Marco Mar 31 '11 at 14:41
2

Variables of reference types, referred to as objects, store references to the actual data, see here for details. They include classes, interfaces and delegates.

From MSDN:

Value Types are structs and enumerations. Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself. All value types are derived implicitly from the System.ValueType. Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces. Unlike reference types, a value type cannot contain the null value. However, the nullable types feature does allow for values types to be assigned to null

Read this: http://www.csharptocsharp.com/node/41

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
1

When you have a variable of a value type, that variable directly holds a value. If you assign it to another variable, the value is copied directly. When the variable is of a reference type, it does not hold the value directly, but rather a reference (a pointer) to the value. When you copy the variable, you don't copy the value that it points to, but the reference (pointer).

You can read more about in MSDN: http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx and http://msdn.microsoft.com/en-us/library/490f96s2.aspx

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • 1
    I prefer to talk about assignment working the same for both scenarios - in each case the *value of the variable* is copied, whether that value contains the data directly or whether it's a reference. – Jon Skeet Mar 31 '11 at 14:36
0

Here you are: C# Concepts: Value vs Reference Types

Rafal Spacjer
  • 4,838
  • 2
  • 26
  • 34