2

I need to clarify a thing that how an object type variables accept class type instance an given in the below code snippet,

class MyClass
{

}

static void Main()
{
   object obj = new MyClass();
}

Since the MyClass is not a type of object but still the instance of MyClass is accepted in the obj(object) variable.

Sankarann
  • 2,625
  • 4
  • 22
  • 59
  • 4
    Every type inherits from object, so MyClass is a type of object. – user2674389 Oct 15 '13 at 06:24
  • Type in the sense, here it refers to MyClass right? its not derived from object.. – Sankarann Oct 15 '13 at 06:27
  • @Sankarann - Why do you think `MyClass` is not derived from `Object`? – Corak Oct 15 '13 at 06:28
  • @Sankarann MyClass is implicitly derived from object, cause its the base class for all value and references types – Jehof Oct 15 '13 at 06:28
  • @Corak: In general case, if we inherit a class means we would use as class MyClass : object -- like this right? but here it is missing. – Sankarann Oct 15 '13 at 06:31
  • @Sankarann Like I said, `every` type inherits from object. You don't explicit define it the way you do in your comment, it happens implicit. – user2674389 Oct 15 '13 at 06:32
  • @Jehof: could it be able to reflect the class to see the implicit inheritance? – Sankarann Oct 15 '13 at 06:32
  • @Sankarann http://ideone.com/ca0epq - Output base type of empty class `MyClass` returns object. – user2674389 Oct 15 '13 at 06:34
  • 2
    @Sankarann - Yes, it is missing. That's both a blessing and a curse. Because every Type is derived from `Object`, you don't need to explicitly put `Type : Object` in there. The C# compiler does that automatically for you. (The curse being: if you don't know that, it might cause confusion). I recommend you reading [CLR via C#](http://www.microsoft.com/learning/en-us/book.aspx?id=15528) which will give you great insights to what happens "behind the scenes". – Corak Oct 15 '13 at 06:35

6 Answers6

3

Actually, your class is an object.
In C# all classes derives from object.

Referring to a class as it's base type is one way of Polymorphism.

It might be better understood using an analogy:
Your class is an object, like a Dog is an animal.

Also, If you try the following:

object obj = new MyClass();
bool isMyType = obj == typeof(MyClass);  //<--this will be true.

Take a look at this SO thread for more information how Polymorphism can be useful.

Community
  • 1
  • 1
Avi Turner
  • 10,234
  • 7
  • 48
  • 75
2

The concept that you do not understand is polymorphism which basically say that you can define an is relation between your classes. For a simple logic every dog is an animal so you can have class Dog that inherits from Animal. This implies that you can assign to variable of type Animal an instance of a Dog but not the other way around - not every animal is a dog. Another thing is that every thing derives form object this is language concept that you simply can take for granted.

Rafal
  • 12,391
  • 32
  • 54
2

Evrything in c# is derived from Object... even your class.enter image description here

jiten
  • 5,128
  • 4
  • 44
  • 73
1

.Net follows OOPs (Object Oriented Programming Language) and here every class can act as a object. Every class inherits Object class and hence every class can act as an object. In your example, .Net creates a default constructor to create instance of the class. You can definitely write your own constructor there.

Hope it helps.

Sandy
  • 11,332
  • 27
  • 76
  • 122
1

Everything in C# is derived from Object.

Even Value Types like struct(int,float,..) are all derived from Object type.


When you define your own class,it implicitly derives from the Object type.

It is mentioned in the docs

All classes, structures, enumerations, and delegates inherit from Object class

Anirudha
  • 32,393
  • 7
  • 68
  • 89
1

MSDN:

Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.

Inheritance Hierarchy:

All classes, structures, enumerations, and delegates.

This means when you use int.Parse() to cast some value to int, there is a class behind int type which makes it able to have methods and do such stuffs. Object has been rooted pretty much everywhere in .Net.

MahanGM
  • 2,352
  • 5
  • 32
  • 45