2

I understand from this post that value types in C# are not objects. That is, they do not inherit from System.Object.

Assuming my logic holds up to this point, are nullable types, such as int?, objects. Do they inherit from Object?

If so, are they subject to all the same rules and constraints as other objects, or are there special rules governing their behavior?

Just as reference, this question comes from an inquiry into the workings of the null coalesce operator.

Community
  • 1
  • 1
Nick Vaccaro
  • 5,428
  • 6
  • 38
  • 60
  • Hey, you should read the linked question and answers harder. Value types are derived from `System.Object` too, just indirectly; else they wouldn't have methods like `object.GetHashCode` and `object.ToString`. – mqp Dec 04 '09 at 22:54
  • What exactly is your question with regards to `Nullable`? What is the issue with them? – thecoop Dec 04 '09 at 22:55

3 Answers3

12

Incorrect - value types are objects, but they don't behave the same as reference types - they are pass-by-value, instead of pass-by-reference. So, Nullable<T> (which is what T? is) is a struct, and so inherits from System.ValueType and System.Object. There is Magic in the C# compiler that makes nullable types behave the same way as reference types with regards to null and ??, but they always have copy-by-value semantics.

thecoop
  • 45,220
  • 19
  • 132
  • 189
  • Thank you. I most likely misread the information (and may have been misinformed). – Nick Vaccaro Dec 04 '09 at 22:58
  • 1
    Value types *are not* "just objects stored on the stack instead of the heap". Maybe they're *usually* stored on the stack, but not always, and it's certainly not this that distinguishes value types from ref types. – LukeH Dec 04 '09 at 23:43
  • Yes, value types can be boxed and can be in a class (and so on the heap inside a ref class instance), but that is the easiest way to think about and to communicate how they behave. – thecoop Dec 05 '09 at 00:17
  • 1
    The point is that the behaviour of value types is completely unrelated to how/where they're stored. After all, the semantics don't change depending on whether they're stored on the stack or the heap, do they? – LukeH Dec 05 '09 at 00:33
1

Are you asking about "inheriting from System.Object" or "reference types"?

Value types do inherit from System.Object, but they're not reference types.

Nullable types are also value types, so they are not reference types either, but they do inherit from System.Object.

Note that there is a difference in what you as the programmer can declare and what the system provides.

Value types do inherit from System.Object, but you cannot yourself declare value types to descend from anything.

Value types descend from System.ValueType, which in turn descends from System.Object.

So technically, all value types descend from System.Object.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
-1

Everything is an object, value types are just structs rather classes.

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
  • 1
    Actually, pointer types _aren't_ objects - see http://blogs.msdn.com/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx – thecoop Dec 04 '09 at 23:00