Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why code with Object fails to complile when the same code with object works?

In the below 2 links i found that Object and object are interchangeable :

Difference between Object and object

c#: difference between "System.Object" and "object"

But i am just not able to understand why i can't make this below code to work, if Object and object are interchangeable:

Code that didn't work with "Object" :

class A : Object
{
    public int x = 5;

}

class B : A
{
    static void Main()
    {
        System.Console.WriteLine(new B().x);
    }
}

Output:

The type or namespace name 'Object' couldnot be found (are you missing a using directive or an assembly reference?)

Code that worked with "object" :

class A : object
{
    public int x = 5;

}

class B : A
{
    static void Main()
    {
        System.Console.WriteLine(new B().x);
    }

}

Output:

5

like image 843
Kumar Vivek Mitra Avatar asked Dec 01 '22 19:12

Kumar Vivek Mitra


1 Answers

To bring Object in scope you need to import System namespace as opposed to object which is a keyword(alias to Object).

add this to your cs file

using System;

or even simpler use fully qualified name.

class A : System.Object
{
}
like image 127
Sriram Sakthivel Avatar answered Dec 04 '22 14:12

Sriram Sakthivel