Why each class in .Net derives from System.Object? what are the benefits?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I pose the opposite question to you: why not? If not for some common ancestor, how would you have a reference to “some object of any type”? Sometimes that’s needed. Really, though, the
System.Objectclass does have some useful methods that are generally useful for any type:Equalshelps test for equalityGetHashCodehelps with performance in collectionsGetType– all objects have some typeFinalizeto support CLR finalizationBecause these things are common to all types, you can have code (even before generics) that operate intelligently on multiple types.
With that said, though, in C# 4.0, they’ve introduced
dynamicwhich is really a class hierarchy of its own. It bypasses static type-checking altogether and does not necessarily derive fromobject. MSDN has a good article about it, and Chris Burrows’ blog series is interesting as well.