Sponsored Ad

Tuesday, April 5, 2011

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

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;

namespace MyApp
{
    class _class
    {
        public static void Main(string[] args)
        {

            my_class cObj = new object();           

            Console.Read();
        }

        class my_class
        {
            public my_class()
            {
            }
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates