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?
|
But int automatically references As far as guidelines, I think it's generally recommended to use e.g.
Likewise, I think it's generally recommended to use e.g.
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,
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:
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 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 |
|||||||||||||||||||||
|
|
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. |
|||||||||||||||||
|
|
If for some reason you wanted a variable called string, you'd see only the first of these compiles:
If you really want a variable name called string you can use
Another critical difference: stack overflow highlights them differently. |
|||||||||||||||||
|
|
There is one difference - you can't use |
|||||||||||||
|
|
It's been covered above; however, you can't use |
||||
|
|
|
Valters, you cannot establish global aliases in the style of e.g.
See here: using Directive (C# Reference) |
|||||||||||||||||
|
|
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 |
|||||||||||||||||
|
|
Lowercase |
|||||||||||||||||||||
|
|
I prefer the capitalized Conditional and control keywords (like Consider:
|
|||||||||||||||||||||
|
|
C# is a language which is used together with the CLR.
When you use C# together with the CLR Theoretically, you could implement a C#-compiler that generated Java bytecode. A sensible implementation of this compiler would probably map |
|||||||||||||||||
|
|
This YouTube video demonstrates practically how they differ. But now for a long textual answer. When we talk about
"
In short "String" is an alias ( the same thing called with different names) of "string". So technically both the below code statements will give the same output.
or
In the same way there are aliases for other c# data type as shown below:- object: Now the million dollar question from programmer's point of view So when to use "String" and "string"? First thing to avoid confusion use one of them consistently. But from best practices perspective when you do variable declaration it's good to use "string" ( small "s") and when you are using it as a class name then "String" ( capital "S") is preferred. In the below code the left hand side is a variable declaration and it declared using "string". At the right hand side we are calling a method so "String" is more sensible.
|
|||||||||
|
|
The only practical difference is the syntax highlighting as you mention, and that you have to write |
|||||||||
|
|
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 |
||||
|
|
|
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 ( |
|||||||||
|
|
As the others are saying, they're the same. StyleCop rules, by default, will enforce you to use |
|||||
|
|
Against what seems to be common practice among other programmers, I prefer |
||||
|
|
|
Using System types makes it easier to port between C# and VB.Net, if you are into that sort of thing. |
|||||
|
|
String ( |
||||
|
|
|
|
||||
|
|
|
|
||||
|
|
|
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. |
||||
|
|
|
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. The C# keyword Similarly, |
|||||||||||||||||
|
|
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. |
||||
|
|
|
New answer after 6 years and 5 months (procrastination). While I shall provide two examples where First, when
The above will not compile because Worse: Saying Secondly, when
Neither statement in the |
|||||
|
|
There is no difference between the two - |
||||
|
|
|
Yes, that's no difference between them, just like the |
||||
|
|
|
One argument not mentioned elsewhere to prefer the pascal case
|
|||||||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
stringis a lexical construct of the C# grammar whereasSystem.Stringis just a type. Regardless of any explicit difference mentioned in any spec, there is still this implicit difference that could be accomodated with some ambiguity. The language itself must supportstringin a way that the implementation is not (quite) so obligated to consider for a particular class in the BCL. – Kirk Woll Dec 2 '14 at 3:05stringto be exactly the same as the BCL typeSystem.String, nothing else. That is not ambiguous at all. Of course, you can implement your own compiler, using the C# grammar, and use all of the tokens found like that for something arbitrary, unrelated to what is defined in the C# language specification. However, the resulting language would only be a C# lookalike, it could not be considered C#. – O. R. Mapper Dec 2 '14 at 8:22stringwithout a using directive for System. You can't do that withString. – Wilsu Nov 30 '15 at 8:52