Can we Initialize a Class Object with Object Class in C#

Shubham
Posted by Shubham under C# category on | Points: 40 | Views : 1291
Someone asked me a question that can we initialize a class object with object class. No it is not possible because every class is derived from object class. So we can not assign object class object to any other class. While reverse is possible.


If you try to compile below program. It will give error saying:

Cannot implicitly convert type 'object' to 'MyApp._class.my_class'. An explicit conversion exists (are you missing a cast?)
=========================================================================================================================
using System;
class _class
{
public static void Main()
{

my_class cObj = new object();

Console.Read();
}

class my_class
{
public my_class()
{
}
}
}

Comments or Responses

Posted by: T.Saravanan on: 3/6/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Nice info. For looking good, kindly post your code inside the code tag.

Login to post response