In C#, what is the difference between String
and string
? (note the case)
Example:
string s = "Hello, World";
String S = "Hello, World";
Also, what are the guidelines for the use of each?
In C#, what is the difference between Example:
Also, what are the guidelines for the use of each? |
||||
As far as guidelines, I think it's generally recommended to use
Likewise, I think it's generally recommended to use
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, 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 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 There is one circumstance in which you have to use the aliases: when explicitly specifying an enum's underlying type. For instance:
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. |
|||||||||||||||||||||
|
I can say the same about ( |
||||
|
The best answer I have ever heard about using the provided type aliases in C# comes from Jeffrey Richter in his book CLR Via C#. Here are his 3 reasons:
So there you have it. I think these are all really good points. I however, don't find myself using Jeffrey's advice in my own code. Maybe I am too stuck in my C# world but I end up trying to make my code look like the framework code. |
||||
|
string is a reserved word, but String is just a class name. This means that 'string' cannot be used as a variable name by itself. For instance if for some reason you were doing this :
If you really want a variable name called 'string' you can use @ as a prefix :
Another critical difference : Stackoverflow highlights them differently. |
|||||||||
|
It's been covered above; however, you can't use |
||||
|
There IS one difference - you can't use String without "using System;" beforehand. |
|||||
|
Lowercase |
|||||||||||||
|
Valters, you cannot establish global aliases in the style of e.g.
See here: using Directive (C# Reference) |
|||||||||
|
'System.String' is THE .net string class - in C# 'string' is an alias for System.String - so in use they are the same. As for guidelines I wouldn't get too bogged down and just use whichever you feel like - there are more important things in life and the code is going to be the same anyway. If you find yourselves building systems where it is necessary to specify the size of the integers you are using and so tend to use Int16, Int32, UInt16, UInt32 etc. then it might look more natural to use String - and when moving around between different .net languages it might make things more understandable - otherwise I would use string and int. |
||||
|
The only practical difference is the syntax highlighting as you mention, and that you have to write |
|||||
|
C# is a language which is used together with the CLR. string is a type in C#. System.String is a type in the CLR. When you use C# together with the CLR string will be mapped to System.String. Theoretically, you could implement a C#-compiler that generated Java bytecode. A sensible implementation of this compiler would probably map string to java.lang.String in order to interoperate with the Java runtime library. |
|||||
|
I prefer the capitalized .NET types (rather than the aliases) for formatting reasons. The .NET types are colored the same as other object types (the value types are proper objects, after all). Conditional and control keywords (like 'if', 'switch', and 'return') are lowercase and colored dark blue (by default). And I would rather not have the disagreement in use and format. |
|||||||||||||
|
Both are same. But from coding guidelines perspective it's better to use string instead of String. This is what generally developers use. e.g. instead of using Int32 we use int as int is alias to Int32 FYI “The keyword string is simply an alias for the predefined class System.String.�? - C# Language Specification 4.2.3 http://msdn2.microsoft.com/En-US/library/aa691153.aspx |
||||
|
As the others are saying, they're the same. StyleCop rules, by default, will enforce you to use |
|||||
|
Lower case string is an alias for System.String. They are the same in C#. There's a debate over whether you should use the System types (System.Int32, System.String, etc.) types or the C# aliases (int, string, etc). I personally believe you should use the C# aliases, but that's just my personal preference. |
||||
|
I just wrote a short article on this topic, including a response from Juval Löwy (of IDesign, creator of the C# coding standard). It says it all! Check it out on this link. |
||||
|
Using System types makes it easier to port between C# and VB.Net, if you are into that sort of thing. |
|||||||||
|
‘string’ is an alias (or shorthand) of System.String. That means, by typing ‘string’ we meant System.String. You can read more in think link: 'string' is an alias/shorthand of System.String. |
||||
|
Against what seems to be common practice among other programmers, I prefer |
||||
|
|
||||
|
String (System.String) is a class in the base class library. string (lower case) is a reserved work in C# that is an alias for System.String. Int32 vs int is a similar situation as is Boolean vs. bool. These C# language specific keywords enable you to declare primitives in a style similar to C. |
||||
|
There is no difference. The C# keyword Similarly, |
||||
|
I'd just like to add this to lfousts answer, from Ritchers book:
I didn't get his opinion before I read the complete paragraph. |
||||
|
There is no difference between the two - |
||||
|
It's a matter of convention, really. "string" just looks more like C/C++ style. The general convention is to use whatever shortcuts your chosen language has provided (int/Int for Int32). This goes for "object" and "decimal" as well. Theoretically this could help to port code into some future 64-bit standard in which "int" might mean Int64, but that's not the point, and I would expect any upgrade wizard to change any "int" references to "Int32" anyway just to be safe. |
||||
|
Coming late to the party: I use the CLR types 100% of the time (well, except if forced to use the C# type, but I don't remember when the last time that was). I originally started doing this years ago, as per the CLR books by Ritchie. It made sense to me that all CLR languages ultimately have to be able to support the set of CLR types, so using the CLR types yourself provided clearer, and possibly more "reusable" code. Now that I've been doing it for years, it's a habit and I like the coloration that VS shows for the CLR types. The only real downer is that auto-complete uses the C# type, so I end up re-typing automatically generated types to specify the CLR type instead. Also, now, when I see "int" or "string", it just looks really wrong to me, like I'm looking at 1970's C code. |
||||
|
One argument not mentioned elsewhere to prefer the pascal case
|
||||
|
String refers to a string object which comes with various functions for manipulating the contained string. string refers to a primitive type In C# they both compile to String but in other languages they do not so you should use String if you want to deal with String objects and string if you want to deal with literals. |
||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.
using string = System.String;
– Dominic Zukiewicz Mar 8 at 21:17using string = System.String;
, asstring
is a key word in the C# language specification and is recognized by the compiler itself, yeah? Which also means thatstring
is ECMA compatible (with the C# spec) while System.String really isn't. Theusing blah = Type
stuff is basically syntactical sugar to allow mere mortals to ask the compiler to do substitutions during pre-parsing so you don't have to use 47 characters of dot-separated type specification for every variable in cases where you have name collisions. Using it in other situations is probably undesirable. – Craig May 23 at 23:21