Please help us settle the controversy of ‘Nearly’ everything is an object (an answer to Stack Overflow question As a novice, is there anything I should beware of before learning C#?). I thought that was the case as everything in Visual Studio at least appears as a struct. Please post a reference, so that it doesn’t become ‘modern jackass’ (This American Life).
Note that this question refers to C#, not necessarily .NET, and how it handles the data under the hood (obviously it’s all 1’s and 0’s).
Here are the comments to ‘everything is an object’:
- Eh, no, it’s not. – Binary Worrier
- I’d like an example… – scotty2012
- isn’t everything derived from the base type Object? – rizzle
- Most things are objects… – Omar Kooheji
- Value types, ints, doubles, object references (not the objects them selves) etc aren’t objects. They can be ‘boxed’ to look like objects (e.g. i.ToString()) but really they’re primitive types. Change the entry to ‘NEARLY everthing is an object’ and I’ll remove the downvote – Binary Worrier
- I appreciate the clarification. I think the lowest level that you can interact with, say an int, in C# is as a struct, which isn’t an object? – http://msdn.microsoft.com/en-us/library/ms173109.aspx – rizzle
- Doesn’t Int32 inherit from ValueType which inherits from Object? If so, despite the behavior, an int is an object. – Chris Farmer
- No, the boxed type for int inherits from ValueType, which inherits from Object. They’re not objects in the traditional sense because a) an int isn’t a reference to an int, IT IS the int. b) ints aren’t garbage collected. If you declare an Int32, then that int is 4 bytes on the stack, end of story – Binary Worrier
Definition of object: ‘Object’ as a inheritor of class System.Object vs. ‘object’ as an instance of a type vs. ‘object’ as a reference type.’
The problem here is that this is really two questions – one question is about inheritance, in which case the answer is ‘nearly everything’, and the other is about reference type vs value type/memory/boxing, which case the answer is ‘no’.
Inheritance:
In C#, the following is true:
System.Object.System.Object.System.Object. They are all convertible toSystem.Object, but interfaces only derive from other interface types, andSystem.Objectis not an interface type.System.Object, nor are any of them directly convertible toSystem.Object.System.Object. Type parameter types are not derived from anything; type arguments are constrained to be derived from the effective base class, but they themselves are not ‘derived’ from anything.From the MSDN entry for System.Object:
So not every type in C# is derived from
System.Object. And even for those types that are, you still need to note the difference between reference types and value types, as they are treated very differently.Boxing:
While value types do inherit from
System.Object, they are treated differently in memory from reference types, and the semantics of how they are passed through methods in your code are different as well. Indeed, a value type is not treated as an Object (a reference type), until you explicitly instruct your application to do so by boxing it as a reference type. See more information about boxing in C# here.