C#: Difference Between "System.Object" and "Object"

c#: difference between System.Object and object

string is an alias for global::System.String. It's simply syntactic sugar. The two are exactly interchangable in almost all cases, and there'll be no difference in the compiled code.

Personally I use the aliases for variable names etc, but I use the CLR type names for names in APIs, for example:

public int ReadInt32() // Good, language-neutral

public int ReadInt() // Bad, assumes C# meaning of "int"

(Note that the return type isn't really a name - it's encoded as a type in the metadata, so there's no confusion there.)

The only places I know of where one can be used and the other can't (that I'm aware of) are:

  • nameof prohibits the use of aliases
  • When specifying an enum base underlying type, only the aliases can be used

Difference between Object and object

There is none. C# provides synonyms for the primitives defined by the CLR. System.String -> string, System.Int64 -> long, System.Object -> object, etc.

Difference between dynamic and System.Object

The difference is that MyTestVar2.ToUpper() compiles and works, without any explicit casting.

object is a normal type.

dynamic is a basically a placeholder type that causes the compiler to emit dynamic late-bound calls.

GetType() is a normal function defined by the object class that operates on the instance that you call it on.

GetType() is completely unaffected by the declared type of a variable that refers to the object you call it on. (except for nullables)

Difference between System.Object.GetType and System.Type.GetType

Yes there is ..

Read about System.Object.GetType and System.Type.GetType.

What is the diferrence between object and Object

There are CLR types, as System.Object System.Int32 etc. These are common for all .Net languages some of these types have C# aliases, that is, there are keywords in C# like object, string, int etc. These are specific to C# but are treated exactly as the CLR analogs

What's the difference between referencing an object by specifying an exact class and System.Object?

Supposing your class had a property called Name, if you used this,

Object obj = new MyClass();

and tried to use:

obj.Name = "George";

This would be a compiler error, because the Object class does not have a Name property. This, however, would be fine:

MyClass obj2 = new MyClass();
obj2.Name = "George";

because the obj2 is a MyClass. In short, in order to access a variable's class members (properties, methods, variables), that variable has to be of a type that has those members, or it needs to be cast to the necessary class:

((MyClass)obj).Name = "George";

However, constantly casting variables in order to get at their members is not good, as it requires extra CPU cycles and can lead to InvalidCastExceptions.

Declaring a collection of objects using object vs Object

There's no difference, the lowercase object is an alias for the Object class, as the lower case string is an alias for the String class, int for Int32 etc.

already answered in the best way:

String vs string in C#

Difference between System.Object.GetType and System.Type.GetType

Yes there is ..

Read about System.Object.GetType and System.Type.GetType.

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

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.)



Related Topics



Leave a reply



Submit