What is the difference of string vs string?

Asked

Viewed 7,848 times

57

I wonder what the real difference is between String (capital letters) and string (s tiny).

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

  • 3

    http://answall.com/a/3261/2360

  • 1

    Choose the one you find most legible. The community tends to agree on the use of string.

  • 1

    In other languages usually String is a complex object, and string is a native

  • 2

    Related http://answall.com/questions/3255/usar-typo-de-variavel-nao-primitiva-em-c-pode-afetar-no-performance

5 answers

61


It has already been said that string is just one alias for the guy String.

To be clear, there is no difference in performance or how the code is generated. There is exactly zero difference in the treatment of both. They can do the same things, and all members are available in any of them, after all so behaves an alias.

Useful differences in use and choice:

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

  • string cannot 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 one example, there is no need and it is not recommended to use in real code. There are cases that an alias may be useful, but this only makes it difficult to read for those who are not used to it, without bringing any benefit.

  • There are some locations that occur the opposite and create a alias can bring more readability to the code.

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

  • Actually the guy String should be used as System.String or where there is a using System, whereas string can be used in any code the compiler will already understand.

  • String can be used as valid identifier. string is a reserved word and cannot be an identifier.

  • There is a difference in the way the syntax Highlight presents both. For a form is guy 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 preferred to make clear, through the Pascal Case, that you are using a reference type.

    But it doesn’t make much sense because Int32 is type of value and also uses Pascalcase (or lowerCamelCase). And yet it, despite being a reference type, has value semantics. So even if you used the logic of Pascal Case for reference type, and Camel Case (or (Uppercamelcase) for type of value, by semantics, which is what matters, it should be Camel Case, ie, should start with lowercase same.

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

  • It is possible to replace the official type with another one with the same name if you do everything correctly. *alias *will point to this new type that has the same name and is placed the same way. If it is not exactly the same name (including the namespace) then it can only be used optionally which almost prevents the actual use.

Important to remember that it is a type like any other by reference with semantics by value and some facilities that the compiler gives. Same goes for using String.

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

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

  • Excellent answer. I just add the fact that it is recommended that the programmer always use the same representation and do not switch between one or the other without any pattern for legibility reasons.

34

In the there is no difference, because, 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

10

As explained by John Skeet at Stack Overflow in that reply, there’s a case where you need to use the alias instead of the System.<obj>, when explicitly declaring the type of a enum. For example:

public enum Foo : UInt32 {} // Inválido
public enum Bar : uint   {} // Válido

3

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

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

0

string is an alias (shortcut) for System.String. So, technically, there’s 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";

Similarly, 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 your examples.

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

Translation and adaptation of this beautiful OS explanation.

Note: I was preparing this response to that question, but she was duplicated this one, so I decided to share it here. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.