2

Possible Duplicate:
String vs string in C#

I have a simple scenario where I am binding a grid to a collection of objects that will be stored in a list. The question that I was wondering is what is the best practice for declaring a list of objects?

IList<object> myCollection;

or

IList<Object> myCollection;

I've read some code standards articles and a lot of people suggest using String vs string so I was wondering if the same rule applies here and why? What is different about the two methods (if anything) and what kind of performance gains are there for doing it one way vs the other.

This grid is part of a custom control where the list is exposed as a property that will be bound to the grid.

Community
  • 1
  • 1
jsmith
  • 976
  • 8
  • 19
  • 1
    Re "a lot of people suggest using String over string" - from what I see, that is by far the minority view; the majority view, IMO, is "using the string keyword, except in a public member name" - I.e. "string over String" – Marc Gravell Dec 02 '11 at 19:36
  • I should have rephrased that to a lot of former coworkers. Thanks! – jsmith Dec 02 '11 at 19:53

4 Answers4

5
  • There's no semantic difference.
  • There's no performance difference at all.
  • Both string and object are keywords of the C# language and resolve to System.String and System.Object classes respectively.

The theoretical advantage of using object over Object is that you are lowering the dependency on the Base Class Library. However, since BCL is a core part of .NET Framework the practical “advantage” is avoiding using System; in simple cases. Such “advantage” is IMHO disputable.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
3

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#

Community
  • 1
  • 1
Mark W
  • 3,879
  • 2
  • 37
  • 53
  • 6
    The one difference is that using `object` requires no namespace references -- you don't need `using System;`. This is because `object` is baked into the C# parser itself, and so is always recognized. – Kirk Woll Dec 02 '11 at 18:51
  • ahh, you're right, good call. – Mark W Dec 02 '11 at 18:52
3

There is no difference between the two at runtime (they compile to the same IL).

The only difference is if you press the shift key and have a using System; or not.

dtb
  • 213,145
  • 36
  • 401
  • 431
1

Internally they are treated the same, so there is no difference to performance or functionality, it's just a matter of preference. I actually like using string instead of String as strings don't behave like objects, so I like that they look differently (and certainly int instead of Int32).

The odd thing I can't defend for myself is that I like using object as well, even though objects behave as, well, Objects.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72