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

Community
  • 1
  • 1
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • Just an FYI -- everything derives from `Object` automatically. – TyCobb May 01 '14 at 05: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. – Igby Largeman May 01 '14 at 05:40
  • You're missing a using directive or a namespace prefix. – Phillip Cloud May 01 '14 at 05: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 – Kumar Vivek Mitra May 01 '14 at 05:47

3 Answers3

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
{
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • 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` – Kumar Vivek Mitra May 01 '14 at 05:47
  • 1
    @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`. – Jon Skeet May 01 '14 at 05:48
  • @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" ? – Kumar Vivek Mitra May 01 '14 at 05:51
  • @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` – Sriram Sakthivel May 01 '14 at 05:52
  • @KumarVivekMitra: No, being a primitive type and having an alias are entirely separate issues. The compiler could have an alias for `System.Windows.Forms.Form` perfectly easily - it doesn't, but it could. – Jon Skeet May 01 '14 at 05:53
  • @JonSkeet, Sir let me rephrase what i have understood this far.... `Object and object are interchangeable`, `object is an alias for Object not its primitive type` and `compiler does have a knowledge about object but not Object (without its namespace)`. And yes primitive type is difference from alias.... As in `Java int is a primitive type, where as Integer is a Wrapper object` and in `C# int is an alias for System.Int32... ` – Kumar Vivek Mitra May 01 '14 at 05:59
  • @KumarVivekMitra: I wouldn't say that `Object` and `object` are interchangable in themselves - you've shown where they're not! I'll add an answer to try to make this clearer. – Jon Skeet May 01 '14 at 06:03
  • 1
    @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). – Alexei Levenkov May 01 '14 at 06:04
  • @SriramSakthivel, the only reason i didn't choose ur answer as the best answer cause u didn't explain me why... but instead gave me a way out.. i appreciate it... but that was not what i wanted... yet thanks for the efforts..... – Kumar Vivek Mitra May 01 '14 at 06:21
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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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.... – Kumar Vivek Mitra May 01 '14 at 06:18
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?

Stix
  • 485
  • 4
  • 13