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.

2
  • 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. Commented Dec 4, 2009 at 22:54
  • What exactly is your question with regards to Nullable<T>? What is the issue with them? Commented Dec 4, 2009 at 22:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I most likely misread the information (and may have been misinformed).
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.
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.
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?
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.

4 Comments

Nullable types are structs (according to thecoop) and cannot inherit from anything.
Technically they do. Check stackoverflow.com/questions/1793357/…
@RCIX: I did not say that. To quote - '...and so inherits from System.ValueType and System.Object'
-1

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

1 Comment

Actually, pointer types aren't objects - see blogs.msdn.com/ericlippert/archive/2009/08/06/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.