0

In c#, the keywords Object and object can be used interchangeable.
Other fundamental types work the same way: String /string, Double / double, ...

Is there any convention for using one or the other?

Which of these is recommended:

object x = new object();

Object x = new Object();

object x = new Object();

What about:

object.ReferenceEquals(a,b);
Object.ReferenceEquals(a,b);

I ask mainly because lower-case new object() just looks wrong to me, yet that is the suggestion I get from Visual studio when I auto-complete Object x = new ...

HugoRune
  • 11,755
  • 6
  • 57
  • 129
  • I am aware, but what are the conventions on using one alias over the other? Presumably these aliases where added for a reason. – HugoRune Jul 07 '14 at 12:37
  • 1
    [What's the difference between String and string?](http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string) - The top 2 answers in this question should answer your question quite sufficiently. – JW Lim Jul 07 '14 at 12:41
  • If I had to venture a guess, the aliases were probably added simply to inform you that it is a "reserved" type to allow you to easily differentiate between .NET native types and your own hand-rolled types. As for convention? If the alias is there I use it, simply because I like it that way :). – David L Jul 07 '14 at 12:49
  • I have a hard time extracting an answer to my question from ["What's the difference between String and string?"](http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string), unless the answer is *"there is no such convention"*? The referred answers mostly deal with `System.String`, and the convention part seems limited to *"Personally, I prefer ..."*. I know that there is no *functional difference* between `Object`and `object`, I am just wondering when I would use one or the other. – HugoRune Jul 07 '14 at 12:57
  • @HugoRune The accepted answer in that question mentions the style used in MSDN examples, and the StyleCop enforcements. There is no official, proper convention, as far as I'm concerned. So I think that's the best you're gonna get, the rest is up to personal preference. As for why the aliases were added, perhaps they contribute to making C# more language-agnostic? `int` and `long` are far more common than `System.Int32` and `System.Int64`. – JW Lim Jul 07 '14 at 13:01
  • Ah, I missed the point about the MSDN examples. If I interpret those correctly, MS recommends `new Object()` for locks, but `new object[]` for arrays. – HugoRune Jul 07 '14 at 13:07

2 Answers2

1

As for my opinion, I like (and recommend) using lowercase object when dealing with instances, e.g.:

object myVar;
object myVar = new object();
object[] myVar = new object[4];

public void someMethod(object arg);
public object someMethod();

//And so on...

And uppercase Object when dealing with the type or class itself, i.e. static methods.

For example,

bool result = Object.Equals(obj1, obj2);
string formatted = String.Format(text, arg1, arg2, arg3);
Matias Cicero
  • 21,834
  • 10
  • 67
  • 132
  • 1
    Note that `Object.` in the case of a `static` method call can be left out because the `class` or `struct` you are inside inherits (directly or indirectly) from `System.Object`, so whether to include the class name at all is a matter of convention as well. – Jeppe Stig Nielsen Jul 22 '14 at 19:08
1

As with most coding style answers, there is no "right way", just personal opinion. Though I try to use rational arguments to come to my opinions.

The way I look at it is:

  • System.Object is not strictly C#, it is a CLR type.
  • object is a C# keyword.

So if I'm writing C# I prefer to use C# syntax, not the underlying CLR types.

The other primary difference is that object is syntax-coloured as a keyword and Object as a reference type. My preference is to use keywords for intrinsics so they look like built in things rather than user-defined types.

The third difference is that object always means the same thing, whereas you can define your own type called Object (in a different namespace than System), which could lead more easily to confusion.

So on balance I prefer object.

The main theme I would recommend for any coding style is to be consistent and have the simplest possible rule to follow, so I would advise against using different case under different circumstances. Pick one and stick with it.

Jason Williams
  • 53,816
  • 11
  • 99
  • 129