In C#, When to use String verses string
Saturday 29 December 2007 @ 11:12 am

 

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. 

- Posted in .Net 2.0, Best Practices  




4 Responses to “In C#, When to use String verses string”

  1. Josh Stodola Windows XP Internet Explorer 7.0 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.

  2. Peter Kellner Windows Vista Internet Explorer 7.0 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.

  3. Naveen J Windows XP Mozilla Firefox 2.0.0.11 Says:

    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?

  4. Rusty Windows XP Internet Explorer 7.0 Says:

    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.

Leave a Reply