tags:

views:

523

answers:

7

I know it is commonly used as a lock object, but is that really sufficient reason? What is the meaning of

object o = new object();

An non-abstract class is something that represents actual objects. "asdasdf" is a string. What actual instance can there be of "object" class? It doesn't make sense, OOP-wise. My question is if there is some practical reason for its existence (besides being used as a lock object).

+2  A: 

Perhaps you can explain why it should be abstract / MustInherit ?

StingyJack
+10  A: 

The easiest way to answer that question is to ask another one: is there any reason for the Object class to be abstract? Since there is no reason for it to be abstract, it's not.

Vojislav Stojkovic
Oh I love this answer. ;)
Ray Booysen
A reason for it to be abstract is that it has no concrete purpose. Things that don't have a concrete purpose should be made abstract to help us as humans better grok APIs.
Scott Langham
That it has a purpose for lock objects is just a bit of a handy convenience... and I guess that's why the designers made object instantiable, even though its got no use by itself.
Scott Langham
If every interface was instantiable with methods doing nothing/benign things, we'd confuse ourselves using them when we shouldn't. That's why they're abstract. I suppose though, they would be useful for fakes in unit tests. It seems to me like object is an exception to this.
Scott Langham
The problem with your argument, Scott, is that interfaces specify a contract, without implementing it; the Object class, on the other hand, not only specifies a contract, but also provides a default implementation.
Vojislav Stojkovic
Of course, the default implementation for the virtual methods (Equals, GetHashCode and ToString) is trivial and mostly useless. On the other hand, declaring those methods as abstract would've forced all developers to implement them whenever they defined a new class.
Vojislav Stojkovic
In the end, the decision to make object a concrete class was a pragmatic one. Perhaps I should have said "no compelling reason for it to be abstract" ;)
Vojislav Stojkovic
I understand Object class provides a default implementation, but that's not a reason for it to be concrete. The Object class could be marked as abstract even though all the methods are implemented.
Scott Langham
However, I do completely agree it was made concrete as a pragmatic decision, and I also agree it should be concrete.
Scott Langham
I suppose in my earlier agrument I used the words interface and abstract a little interchangably (from my C++ background). I should have said 'If every abstract class'
Scott Langham
+8  A: 

Sometimes you just need an Object, and nothing else, such as when using a random reference type for things like implementing multithreading locks. Not making object abstract allows use in those situations. The Monitor.Enter and Monitor.Exit classes (which is the basis behind the "lock" keyword in C#) can acquire a lock on any given object, regardless of implementation.

David Morton
+1  A: 

Yes, that is sufficient. Either you allow object to be created or you have to create classes for each structure that's just looking for a static pointer that can be referenced. My guess is that there are other places this is used.

Orion Adrian
+2  A: 

This is actually used quite a bit in System.Windows.Forms where plain old System.Object instances are created to act as keys into per Control instance dictionaries for things like event handlers and property values. Think of it as a poor man's DependencyProperty but without all the machinery built up around it (and thus no need to define an abstraction like the DependencyProperty class).

The CLR might even have some kind of support for optimizing memory usage of plain old System.Object instances since there are only a couple scenarios where they can be used beneficially.

+4  A: 

Surely for a class to be abstract means that it has some functionality which requires implementation...

System.Object does everything it needs to thus it is not Abstract.

I think you misunderstand why things are made abstract, it's becuase they contain some useful functionality and don't dictate how you implement some parts of thier functionality.

Essentially they are half made classes, like a model kit, which you get to customise to work the way you want to, but you don't have to roll the whole thing from scratch.

They do not exist to stop you from creating instances of them.

Omar Kooheji
+2  A: 

it shouldn't have a public constructor and, I think, it should be abstract. The object class is the base of all .net objects and have no purpose by itself (ok, locking... but I prefer to have a class Lock : object and the problem is solved). The excuse "because it doesn't need to be abstract" is poor. We should have reasons to make classes concrete instead of abstract. We don't have any reason to make it concrete, so... it shouldn't, because the main purpose of this class is "to be inherited by all the other classes".

Ivo
"We should have reasons to make classes concrete instead of abstract." Why that? Why shouldn't objects simply model domain and responsibilities with one object per responsibility? Nothing needed to be abstract then.