8

This question is more about adding a ? to a value type than about int?

In C# an int is a value type. Is int? a value type or a reference type?

In my opinion it should be a reference type as it can be null.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2769119
  • 83
  • 1
  • 3
  • http://msdn.microsoft.com/en-us/library/1t3y8s4s%28v=vs.90%29.aspx and http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx – Sriram Sakthivel Sep 11 '13 at 14:30
  • 2
    The `null` support for `Nullable` is behind the scenes trickery on the part of the language team and compiler. – Adam Houldsworth Sep 11 '13 at 14:32
  • int? === Nullable. Nullable is a value type, thus, int? is a value type. I believe the Nullable type was added for use with database integration, where strongly typed data fields (in this case int) can be 0 or greater, or null...all of which are valid in the context of a database. – Matthew Layton Sep 11 '13 at 14:50
  • nice answer series0ne – user2769119 Sep 11 '13 at 15:12
  • 1
    Your logic is: `null` is always the "refers to nothing" value of a reference type, `null` is a legal value for a nullable value type, therefore nullable value types are reference types. This syllogism would be correct if the two premises were true, but the first premise is false. A nullable value type is nothing more than a bool that indicates nullity glued to a non-nullable value. If the bool is false then the value is considered to be null. The null value of a nullable value type is not the same bits as a null reference; they are completely different. – Eric Lippert Sep 11 '13 at 17:07
  • As far as I know, '?' is the 'nullable' operator. It is normally used in value types in C#. – Choudhury Saadmaan Mahmid Mar 23 '14 at 05:47

4 Answers4

22

int? is equivalent to Nullable<int> which means it is a struct.

So that means it is a value type.

In my opinion it should be a reference type as it can be null.

Your assumption is wrong. From documentation;

Nullable structure represents a value type that can be assigned null. The Nullable structure supports using only a value type as a nullable type because reference types are nullable by design.

So it has 2 values;

  1. The value of data
  2. Boolean value which determines the values has been set or not. (Nullable<T>.HasValue)
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    [SerializableAttribute] public struct Nullable where T : struct thx! – user2769119 Sep 11 '13 at 15:06
  • I used reflection to examine the properties of a type .Where(p => p.PropertyType.IsValueType) and properties included nullables. So, that backs up what you're saying. – Phil Jul 07 '16 at 09:17
2

Nullable types are instances of the System.Nullable struct Hence int? is a Value Type.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

int? is not a reference type. It is a struct (value type).

It has its own value like a normal int plus an additional "null" value. With Nullable types, you can check if it has a value using HasValue.

MSDN

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
0

EDIT I should've started with this example in the 1st place:

Think about enums. Every enum we write automatically extends Enum, which is a class. Hence, value types we write extend a class written by the .NET team which is a reference type.

You can split the world of types in two ways:

  • whether they contain the null value or not
  • whether they are value types or not

All combinations are possible in theory. If I'm not mistaking, in C# at least, the combination of reference types which cannot be null yields an empty set. There is no example for that combination.

Basically all other 3 combinations are possible:

  • types which can be null and are value types
  • types which cannot be null and are value types
  • types which can be null and are reference types

The nature of being either a value type or a reference type has nothing to do with null (at least in the way languages should be designed and used, not in the way of the actual engineering details underneath).

Value types are types which cause assignments between variables, parameters and fields to do full cloning of the instance. Reference types are types which don't do that.

That has basically nothing to do with null or with the type of memory which is occupied by the instances (be it stack, heap or something else).

Eduard Dumitru
  • 3,242
  • 17
  • 31