In C#, When to use String verses string

So, technically, String and string mean exactly the same thing in C#.  string, is an alias for String (aka a shorthand) for System.String.  So, when should use which?  It seems that the convention is to use string when you are referring to an object and String when you are referring specifically to the string class.

This is basically what is said at this URL:  http://en.csharp-online.net/CSharp_String_Theory%E2%80%94string_versus_String

Juval Lowy has some coding standards at http://www.idesign.net that says basically the same thing.  From the document on IDesign’s web site the following examples show what is best practices (which I completely agree with).

idesign1                            idesign2

That’s it for now. 

About Peter Kellner

Follow me:


Comments

  1. Troy Alford says:

    @Ryan:

    I think that’s the entire point of the article, actually. That is what the syntax highlighting would seem to indicate – but in this case, just like with bool/Boolean, int/Int32 and so on, this is actually not true.

    These are actual aliases for one another – and they are exactly equivalent in every way other than the color they get highlighted on the screen.

    I think the bone of contention is whether or not that is “good” – and what constitutes “best practice” with regards to readability and such. At the end of the day, though – if you use them interchangeably, there is absolutely no performance difference whatsoever – because both string and String compile to System.String – and are exactly identical.

    This is far more an esoteric debate revolving around preference. 😛

  2. string is a primative type which is exactly how it is in other languages… Just a string. String however is an object. String is used to create an Object that contains this string. String will take up more membery but is called faster. If something is created then destroyed, for instance a private string _var; then that is correct. However if it will be referenced over and over then you should use String.

  3. To answer this, we need to understand why the alias ‘string’ exists in the first place? Why not just use ‘System.String’ everywhere?

    My belief is that the alias exists to make code more readable, and has as much to do with syntax highlighting in VS than anything. Although a reference type, System.String is used like a simple value type in the overwhelming majority of cases. The exception to this is when accessing static members of the class like String.Empty.

    Therefore, I would use ‘string’ when instantiating a new instance, and declaring variables etc. But when I need to put string.Something, it’s time to switch to the capital S – String, as we don’t often see valuetype.Something, so in my view string.Empty looks odd, so IMHO, best use would be…

    string foo = String.Empty;

    P.S. @Josh … spoken like a true VB programmer 😉

  4. i know this post is old but ~ @Josh, lol at the suggestion that microsoft should throw out their flagship product because you dont like case sensitivity… vb is for nubs…

  5. strainedeye says:

    obsolesce i mean 🙂

  6. strainedeye says:

    @Mr_Not_VB

    that’s why he said vb should be deprecated or put to obsolence

  7. @Josh Stodola

    Microsoft created VB for guys like you who still need training wheels.

  8. haha, nice answer, Rusty

  9. String.Empty or string.Empty? and why?
    Use string.empty when you have a space after the = and String.Empty when you are starting on a new line.

    Why? Because people just need rules, apparently.

  10. one doubt.
    which is better to use?
    String.Empty or string.Empty? and why?
    Or are they both same since string is an alias of String?

  11. Peter Kellner says:

    I’m more thinking that things like S or s doesn’t make a difference should be deprecated (like what’s in this post). I’ve grown up using case sensative languages so I just always expect case will make a difference. VB just confuses me.

  12. Josh Stodola says:

    In VB.NET, it will always capitalize the S for you, therefore you don’t even have to think about it. Case-sensitive languages should be deprecated, IMO.

Follow

Get every new post delivered to your Inbox

Join other followers: