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
It's easiest not to think of object
and Object
as being interchangeable at all, in fact.
It's better to understand object
as an alias for global::System.Object
. So whenever it you see global::System.Object
as a type name, you can use object
instead, and vice versa.
In cases where the name Object
would be resolved to the type global::System.Object
(e.g. because you have a using
directive for the System
namespace, and no other using
directives which would import a different Object
type), you can just use Object
instead.
The two ways of referring to the type really do refer to the exact same type - there will be no difference in the generated code between:
global::System.Object foo = ...;
and
object foo = ...;
The C# 5 specification describes it like this, by the way, in section 4.2.2:
The
object
type
Theobject
class type is the ultimate base class of all other types. Every type in C# directly or indirectly derives from the object class type.The keyword
object
is simply an alias for the predefined classSystem.Object
.
(There's no need for a global::
qualification in this case as there's no concept of imported namespaces in the spec itself... System.Object
could never mean global::Foo.System.Object
in the spec, for example - whereas it could in C# code.)
See more on this question at Stackoverflow