What is the difference between String and string in C#?


Example (note the case):

string s = "Hello world!";
String S = "Hello world!";

What are the guidelines for the use of each? And what are the differences?



Answers



string is an alias in C# for System.String. So technically, there is no difference. It's like int vs. System.Int32.

As far as guidelines, I think it's generally recommended to use string any time you're referring to an object.

e.g.

string place = "world";

Likewise, I think it's generally recommended to use String if you need to refer specifically to the class.

e.g.

string greet = String.Format("Hello {0}!", place);

This is the style that Microsoft tends to use in their examples.


It appears that the guidance in this area may have changed, as StyleCop now enforces the use of the C# specific aliases.




Just for the sake of completeness, here's a brain dump of related information...

As others have noted, string is an alias for System.String. They compile to the same code, so at execution time there is no difference whatsoever. This is just one of the aliases in C#. The complete list is:

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

Apart from string, object, the aliases are all to value types. decimal is a value type, but not a primitive type in the CLR. The only primitive type which doesn't have an alias is System.IntPtr.

In the spec, the value type aliases are known as "simple types". Literals can be used for constant values of every simple type; no other value types have literal forms available. (Compare this with VB, which allows DateTime literals, and has an alias for it too.)

There is one circumstance in which you have to use the aliases: when explicitly specifying an enum's underlying type. For instance:

public enum Foo : UInt32 {} // Invalid
public enum Bar : uint   {} // Valid

That's just a matter of the way the spec defines enum declarations - the part after the colon has to be the integral-type production, which is one token of sbyte, byte, short, ushort, int, uint, long, ulong, char... as opposed to a type production as used by variable declarations for example. It doesn't indicate any other difference.

Finally, when it comes to which to use: personally I use the aliases everywhere for the implementation, but the CLR type for any APIs. It really doesn't matter too much which you use in terms of implementation - consistency among your team is nice, but no-one else is going to care. On the other hand, it's genuinely important that if you refer to a type in an API, you do so in a language neutral way. A method called ReadInt32 is unambiguous, whereas a method called ReadInt requires interpretation. The caller could be using a language which defines an int alias for Int16, for example. The .NET framework designers have followed this pattern, good examples being in the BitConverter, BinaryReader and Convert classes.




String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference. Choose what you like and use that. If you code in C#, I'd prefer string as it's a C# type alias and well-known by C# programmers.

I can say the same about (int, System.Int32) etc..




Difference between byte vs Byte data types in C#

The byte keyword is an alias for the System.Byte data type.

They represent the same data type, so the resulting code is identical. There are only some differences in usage:

  • You can use byte even if the System namespace is not included. To use Byte you have to have a using System; at the top of the page, or specify the full namespace System.Byte.

  • There are a few situations where C# only allows you to use the keyword, not the framework type, for example:

.

enum Fruits : byte // this works
{
  Apple, Orange
}

enum Fruits : Byte // this doesn't work
{
  Apple, Orange
}



byte and System.Byte in C# are identical. byte is simply syntactic sugar, and is recommended by StyleCop (for style guidelines).




No difference. byte is alias to System.Byte, the same way int is alias to System.Int32, long to System.Int64, string to System.String, ...




Why do lowercase and uppercase versions of string exist and which should I use?

In C#, lower-case type names are aliases for the System.xxx type names, e.g. string equals System.String and int equals System.Int32.

It's best practice to use these language aliases for the type names instead of their framework equivalent, for the sake of consistency. So you're doing it wrong. ;-)

As for a reason why they both exist, the .NET types exist because they are defined in a language-independent standard for the .NET libraries called CTS (common type system). Why C# defines these aliases is beyond me (VB does something quite similar). I guess the two reasons are

  1. Habit. Get all these C and Java programmers to use C# by providing the same type names for some fundamental types.
  2. Laziness: You don't have to import the System namespace to use them.

EDIT Since many people seem to prefer the other notation let me point out that this is by no means unreasonable. A good case can actually be made for the usage of the CTS type names rather than C#'s keywords and some superficially good arguments are offered in the other answers. From a purity/style point of view I would probably concur.

However, consider if this is worth breaking a well-established convention that helps to unify code across projects.




It is conceptually similar to something like this:

using int=System.Int32



string is mapped to the String class AFAIK, so they're the same.

The same is true for, for example int and Int32.




String vs string

There is no difference. string (lower case) is just an alias for System.String.




No difference. System.String is strictly identical to string. Common C# coding guidelines indicates that you should use the keyword string.




They are aliases and are interchangeable. However, stylistically, for declarations, I use the lowercased string, and for the static methods, I use String.

string foo = "bar";

if( foo != String.Empty )
{
   Console.WriteLine(String.Format("foo.Length = {0}", foo.Length));
}