9

Possible Duplicate:
In C# what is the difference between String and string

what's the difference between the String and string. In C#, which is preferred?

Community
  • 1
  • 1
user496949
  • 75,601
  • 138
  • 297
  • 413

4 Answers4

5

Actually string is an alias for System.String but erash is basically right...

Here is a list of other alias' shamelessly lifted from Jon Skeet in this post:

* object: System.Object
* string: System.String
* bool: System.Boolean
* byte: System.Byte
* sbyte: System.SByte
* short: System.Int16
* ushort: System.UInt16
* int: System.Int32
* uint: System.UInt32
* long: System.Int64
* ulong: System.UInt64
* float: System.Single
* double: System.Double
* decimal: System.Decimal
* char: System.Char
Community
  • 1
  • 1
Abe Miessler
  • 75,910
  • 89
  • 276
  • 451
2

They are the same this, string is an alias for String.

I tend to use String when calling static methods (i.e., String.Format(...) or String.IsNullOrEmpty(...). I don't know why, I just do.

Ed S.
  • 115,705
  • 20
  • 165
  • 244
1

string is just an alias for String -- they are the same

edit: type fixed

cordialgerm
  • 8,015
  • 5
  • 26
  • 47
1

string is a C#-specific keyword that means the same thing as the System.String type. Prefer the language keywords where possible, so use e.g. string, int, float, instead of System.String, System.Int32, System.Single.

cdhowie
  • 133,716
  • 21
  • 261
  • 264
  • Aside from the fact that it looks a *lot* cleaner, this is the guideline in most open source projects and is the style MS uses in their documentation. – cdhowie Nov 16 '10 at 05:54
  • float == Single? Didn't know that... I guess it's half a double though so that makes sense... still a funny name though. – mpen Nov 16 '10 at 05:55
  • For the rest I would agree about the 'it looks cleaner' statement, but the question is about the String class, and I don't think string looks any cleaner than String. In general though, yeah. – Ed S. Nov 16 '10 at 05:56
  • If he was not asking about the class, what else would he be asking about? When you reference the class from code when e.g. declaring a variable, do you not use `string`? – cdhowie Nov 16 '10 at 05:58