4

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

4
  • Just an FYI -- everything derives from Object automatically. Commented May 1, 2014 at 5:39
  • 3
    The answer to your question is in your question. are you missing a using directive or an assembly reference? Please read and understand compiler output before asking questions here. It's not that hard. Commented May 1, 2014 at 5:40
  • You're missing a using directive or a namespace prefix. Commented May 1, 2014 at 5:40
  • if Object and object are interchangeable then it mean object is an alias for System.Object... then why do i need to use "using System" as namespace Commented May 1, 2014 at 5:47

3 Answers 3

12

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
{
}
Sign up to request clarification or add additional context in comments.

9 Comments

if Object and object are interchangeable then it mean object is an alias for System.Object... then why do i need to use "using System" as namespace
@KumarVivekMitra: Because otherwise the compiler doesn't know what the name Object means - it could mean SomeOtherNamespace.Object. The compiler has special knowledge of object, but not Object.
@JonSkeet, sir you have just said what i expected when i asked this question, but i just want to clarify that does object is kinda primitive thing and Object an reference type . Ya i do know "object" or "Object" are only reference type... but if compiler has special way of dealing with object without its namespace, then how is it ?? And what kinda special knowledge does compiler possess for "object" ?
@KumarVivekMitra You're right. object is an alias for Object and not the other way around. When you just say Object compiler doesn't know you're actually refering to System.Object or some other object. For instance namespace MyNamespace { internal class Object {} class Strange : Object {} } in this example Strange class inherits from MyNamespace.Object not System.Object
@KumarVivekMitra if you read statement as object is alias for System.Object it may clear confusion, same as string is alias for System.String . (99% of people have using System as first line of code so System.Object and Object mean the same thing for most of the code).
|
7

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
The object 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 class System.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.)

1 Comment

Sir, thanks for taking you time to explain this well, thanks for explaining me the reason behind my doubt...........and not directly giving the way out as Sriram Sakthivel or Alexei Levenkov did, but yes i appreciate their efforts too....
0

Actually the answer is here:

https://msdn.microsoft.com/en-us/library/9kkx3h3c(VS.71).aspx

The object data type is the type to and from which objects are boxed. And that is to say NOT the System.Object type; for boxing and unboxing use object. one might be an alias for the other, but the documentation at the link and actual functionality lends me to believe they are not identically interchangeable.

In the comments some people are making assumptions based on the compiler error message "are you missing a using directive or an assembly reference?".

Notice the classes are both deriving from object and Object, so if one is an Alias for the other how is he missing something?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.