16

Reading a book about C# I noticed that sometimes is mentioned value type and sometimes primitive type for some data type (e.g. int, double). I thought they were the same thing, but they are really the same or not?

What is the difference between a value type and a primitive type? Are they the same thing?

EDIT

The question is not only related to C# programming Language, I was wondering how them are different even in any other language.

tzrm
  • 513
  • 1
  • 8
  • 14
  • 7
    A struct, for ex., is a value type but not a primitive. – L.B Oct 01 '12 at 20:07
  • See also: http://stackoverflow.com/questions/8790809/whats-the-difference-between-primitive-and-reference-types – user1249569 Oct 01 '12 at 20:18
  • The formal classification of the types are: Value types and Reference types. Primitive or native types, is a way to refers those basic types such as int, char, bool, etc... they normally are Value types. – Jaider Oct 01 '12 at 20:18
  • 1
    @DJKRAZE see the edit, the question isn't strictly related to C# and .NET ... –  Oct 01 '12 at 20:20
  • 3
    The problem here is the answer to the question is subtly different in different languages so any correct answer has to be either very long or so general as to be of little use. – James Gaunt Oct 01 '12 at 20:23
  • 2
    `The question is not only related to C# programming Language` So you assume every language treats them the same. – L.B Oct 01 '12 at 20:29

3 Answers3

8

A primitive type (e.g. int) can be mapped directly to a Base Class Library (BCL) type (e.g. System.Int32)

A value type inherits from System.ValueType and is passed by value (among other properties).

They are not interchangeable as object (System.Object) is a primitive type but not a value type, and structs are value types but not primitive.

See more differences here

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 2
    The answer covers only C# and .NET, in others language is the same ? Look at the tag of the question ... – aleroot Oct 01 '12 at 20:17
  • 1
    Well, there are lots of languages so I suspect they are not all the same. I answered the c# definition since that's what was mentioned in the question. – D Stanley Oct 01 '12 at 20:18
6

A value type is usually whatever type reside on the Stack .

A primitive type is a type defined at the programming language level, often it is even a value type, directly supported by the compiler of the language.

However this is a summary general answer because each programming language have different set of differences between the two types ...

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 5
    -1 - except Eric Lippert calls this point about the stack a 'myth' and he should probably know : http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx – James Gaunt Oct 01 '12 at 20:12
  • Yes, better to speak in terms of semantics, not storage (though how they are stored is relevant too in some instances) – Ed S. Oct 01 '12 at 20:14
  • 2
    @JamesGaunt yes, that could be true for C#, but for Java or ANSI C ? Are you sure that my answer is wrong ? – aleroot Oct 01 '12 at 20:16
  • @aleroot - fair point, I see the question is tagged with multiple languages, but the first line refers to C#. I will rescind my -1. – James Gaunt Oct 01 '12 at 20:21
  • 1
    -1 for "A value type is usually whatever type reside on the Stack". – Vikas Verma Oct 15 '14 at 16:59
  • I still don't understand :( – BenKoshy Feb 17 '16 at 05:37
  • Whether value types are allocated on the stack or somewhere else is an implementation detail, regardless of the language. Eric Lippert's article can be applied to Java just as well. Also this answer does not really explain the difference, and stating that value types are not directly supported by the compiler can be very misleading. – Raimund Krämer Nov 22 '18 at 16:03
-1

No they are not. A value type is copied and while a reference type is being referenced. See the imageValue vs Reference

Edit My mistake. Here's an tutorial for the differences: http://www.codeproject.com/Articles/11212/Primitive-Reference-and-Value-Types

Keethanjan
  • 365
  • 1
  • 7