15

In the .NET Framework, at least in the C# language, we have two "versions" of the string type:

  • "string"
  • "String"

It appears that they are interchangeable, but are they really? If they are not interchangeable, is it generally better to use one instead of the other, and under what circumstances?

5
  • 1
    Similar to this question: stackoverflow.com/q/6000517/6504
    – Agent_9191
    Apr 11, 2012 at 21:24
  • 2
    Stylistically, I tend to use the lower-case version when I'm referring to the type, and the upper-case version when I'm calling a static method of System.String. Apr 11, 2012 at 22:38
  • 4
    Identical to stackoverflow.com/questions/7074/….
    – zzzzBov
    Apr 12, 2012 at 4:30
  • 2
    string does not exist in .net, it only exists in C#. Apr 12, 2012 at 17:41
  • 1
    Although this might be a bit more suitable for Stack Overflow, I don't think it's completely off topic for Programmers, and since Stack Overflow already has at least one duplicate there wouldn't be much point in migrating this. So please stop flagging / close voting to migrate, if you feel this should be closed, that's cool, but it should be closed not migrated.
    – yannis
    Apr 13, 2012 at 8:58

6 Answers 6

30

Well according to the MSDN

string is an alias for String in the .NET Framework.

Where "String" is in fact System.String.

I would say that they are interchangeable and there is no difference when and where you should use one or the other.

It would be better to be consistent with which one you did use though.

7
  • for some reason I had it in my head that String was a class wrapper around a value typed string and this leads to the idea of boxing... am i totally wrong here? Apr 11, 2012 at 22:30
  • 1
    @Gabriel No, String (and hence string by alias) are reference types. Apr 11, 2012 at 22:57
  • @Gabriel: doesn't Java do that with stuff like byte vs Byte? Apr 12, 2012 at 4:23
  • Is the same also true for Double vs double, and other data types? Apr 12, 2012 at 9:18
  • 1
    @AaronAnodide I believe that's a Java convention, which is probably where you heard of it.
    – KChaloux
    May 1, 2014 at 12:28
21

As pointed out, they are the same thing and string is just an alias to String.

For what it's worth, I use string to declare types - variables, properties, return values and parameters. This is consistent with the use of other system types - int, bool, var etc (although Int32 and Boolean are also correct).

I use String when using the static methods on the String class, like String.Split() or String.IsNullOrEmpty(). I feel that this makes more sense because the methods belong to a class, and it is consistent with how I use other static methods.

2
  • We use this convention at work as well, and I like it. My brain automatically makes the connection.
    – SBI
    May 1, 2014 at 11:18
  • I have always done the same or the same reason. Glad I'm not the only one. Annoyed how Visual Studio now suggests (nags) changing String to string is "simplifying".
    – xr280xr
    Apr 9, 2020 at 22:06
5

I'd suggest using lowercase string. Uppercase String will use the first class named String it finds in the imported namespaces ... which is generally System.String. It's possible for your own String class to be used instead.

4
  • 3
    If you have two different imported types with the same name, and you use that name without namespace qualification, you will get an error.
    – svick
    Apr 11, 2012 at 22:02
  • It's possible to trick String into working, it's never possible to trick string. Apr 11, 2012 at 22:04
  • Still, I don't think that's a good enough reason to use string.
    – svick
    Apr 11, 2012 at 22:05
  • 5
    How is "one might break" vs. "one will never break" with all else being identical not a good enough reason? Apr 11, 2012 at 22:24
4

string is a C# keyword which is simply an alias for the System.String

..but as it's a keyword, your syntax colouring editor/IDE will show it in keyword colour, same as int, double, if, else, etc. Stylistically, you may prefer to use the keyword rather than the System.String type (or just String after you've done using System;).

This is a niche point though, as although bool, char, int, double and string are all keyword aliases for system types, string is an odd-one-out as it is a reference type and the others are value types.

2
  • 4
    object is a keyword alias for a reference type, too
    – nikie
    Apr 12, 2012 at 9:24
  • Oh yes, I forgot all about that one - thanks for reminder. Apr 12, 2012 at 12:22
2

string is just an alias for String, they're ultimately the same.

You can use whichever you prefer, although I generally use string when I'm planning on using it much like a primitive data type (similar to int, float, or bool) and String when I'm planning on using it as an object/class. For example:

string foo = "   ";
if (String.isNullOrWhitespace(foo)) 
{
    // ...
}

In the case above, I'm planning on using foo simply to store some text (effectively as a primitive), so I use string; however, when I call String.isNullOrWhitespace(string) I use String. I do this, because generally the first letter of class names are capitalized (String) while the first letter of primitives (such as int) are not.

-2

String is used with namespace System.String, string is used C# compiler specification, moreover System.String is reference type value. You are preferred to deal with System objects, string for literals purposes only. System and system both working is same.

string TC = new System.String('/', 5);

String is used for comparing, equal and other purposes as well.

1
  • 4
    this doesn't seem to offer anything substantial over prior 6 answers
    – gnat
    May 1, 2014 at 11:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.