What is the difference between string vs string?

54

I'd like to know what the actual difference is between String (upper case s) and string (lowercase s).

Apparently the two have the same goals, but which one is "better" to use?

    
asked by anonymous 01.02.2014 / 11:31

5 answers

56

It has already been said that string is only an alias for type String .

To make it clear, there is no difference in performance or how the code is generated. There is exactly zero difference in handling both. Both can do the same things, and all members are available in any of them, anyway an alias behaves.

Useful differences in use and choice:

  • string is just a simpler way to use type String in C #, that is string is how to "type" a string in C # and String is a type dp CLR. In C # depending on the context it is better to use either way. The type String present in the namespace System can be used in any language that uses the CLR .

  • string can not be used with reflection. String should be used instead.

  • String can be used to create other aliases :

    using str = System.String;
    //...
    str s = "Foi usado outro alias para string.";
    // a variável 's' é do tipo System.String que é o mesmo que ser string
    

    But this is just an example, there is no need and it is not recommended to use in real code. There are cases where an alias can be useful, but this only makes it difficult for anyone not accustomed to it to do so without any benefit.

  • There are some places where the opposite occurs and creating an alias can bring more readability to the code.

  • In other cases it may just be weird to use one or the other and make it difficult to read.

  • Actually type String should be used as System.String or where using System exists, while string can be used in any code that the compiler will already understand.

    / li>
  • String can be used as a valid identifier. string is a reserved word and can not be an identifier.

  • There is a difference in how syntax highlight displays both. One way is type and the other is keyword .

  • Despite the general recommendation to use string whenever possible while programming in C #, there are controversies in its use. Some claim that String is preferential to make it clear through Pascal Case that you are using a reference type < sup> (en) .

    But it does not make much sense because Int32 is value type and also uses PascalCase (or lowerCamelCase). And yet it, despite being a reference type, has value semantics. So even if you used the Pascal Case logic for reference type, and Camel Case (or (UpperCamelCase) for value type, by semantics, which is what matters, it should be Camel Case, ie it should start with tiny even.

    In the CLR case style is used to help determine other identifier characteristics.

Important to remember that it is a type like any other by reference with semantics by value and some facilities that the compiler gives. The same is true if you use String .

It's also worth remembering that there is no primitive type in C # ( other reference ). This exists only as a concept of the CLR, but not as something fundamental.

Note that all of this is true for C #, other languages like Java, or even VB.NET may have more differences.

02.02.2014 / 16:34
32

In the there is no difference since string is just a shortcut to System.String .

See the full list of aliases :

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
    
01.02.2014 / 11:38
9

As explained by John Skeet in Stack Overflow in English in this answer , there is a case where you need to use alias instead of System.<obj> , when you are explicitly declaring the type of a enum . For example:

public enum Foo : UInt32 {} // Inválido
public enum Bar : uint   {} // Válido
    
02.02.2014 / 00:24
3

String is of type System.String and you will find it in any implementation of any language for .Net, it is a type defined by the .Net Framework.

string is a C # language keyword that points to type System.String . The same is true for Int32 and int , etc.

    
26.01.2015 / 13:09
0

string is an alias (shortcut) for System.String . So, technically, there is no difference. It's like int vs.. System.Int32 .

It is generally recommended to use string , whenever you are referring to an object.

For example:

string place = "world";

Likewise, it is generally recommended to use String , if you need to refer specifically to the class.

For example:

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

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

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

  

Translation and adaptation of this beautiful SO explanation .

Note: I was preparing this response for this question , but it was duplicated from this, so I decided to share it here. :)

    
26.01.2015 / 13:05