标签云

微信群

扫码加入我们

WeChat QR Code

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?


O.R.Mapper, but the fact remains that string is a lexical construct of the C# grammar whereas System.String is 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 support string in a way that the implementation is not (quite) so obligated to consider for a particular class in the BCL.

2018年05月24日38分48秒

KirkWoll: According to the language specification, the language itself must consider string to be exactly the same as the BCL type System.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#.

2018年05月23日38分48秒

You can use string without a using directive for System. You can't do that with String.

2018年05月24日38分48秒

For someone coming from Algol and Fortran, this discussion shows there is something wrong with string. It is needed to abbreviate System.String, but, as an alias, it seems quite like, but not exactly the same thing. After several years of C#, though, I'd say, it is safe to simply use string and string.Format() and not to worry about System.String.

2018年05月24日38分48秒

If this kind of question were asked in 2017, it would've received 4,000 downvotes, 2,000 links to the MSDN String class page, and closed as not productive.

2018年05月24日38分48秒

If you decide to use StyleCop and follow that, that will say to use the types specific to the language. So for C# you'll have string (instead of String), int (instead of Int32), float (instead of Single) - stylecop.soyuz5.com/SA1121.html

2018年05月24日38分48秒

I always use the aliases because I've assumed one day it might come in handy because they are acting as an abstraction, so therefore can have their implementations changed without me having to know.

2018年05月23日38分48秒

Visual Studio 2015 says that String.Format should be changed to string.Format, so I guess Microsoft is going that way. I have also always used String for the static methods.

2018年05月24日38分48秒

As I've read through these I've notice several of the comments are simply incorrect. DRAirey1 In time, you'll find that the old way is still the best, if you doubt that then I dare you to try to write C# code without using Visual Studio. It's virtually impossible and a situation that does come up from time to time in web development work. Vlad You don't need to import anything to use String. Abhi Your comment is pointless and equally true for string.Format(). KlitosG No, that isn't true. They all work exactly the same.

2018年05月23日38分48秒

Could you add a remark that there is, in fact, a difference? For example: nameof(string) won't compile whereas nameof(String) will.

2018年05月23日38分48秒

The inheritance situation with enum is interesting. Can you point to documentation onto why alias must be used for enumerations? Or is this a known bug?

2018年05月24日38分48秒

It's in section 14.1 of the spec (I can't quote here easily as it's too long). It doesn't explicitly say that you've got to use the alias, but the aliases are sort of treated as their own types. It's all a bit weird.

2018年05月24日38分48秒

PiPeep what's more astounding than the large amount of upvotes is the staggering low amount of downvotes (consider the top 5 posts have a total of over 2000 upvotes, and yet only 1 downvote amongst them all). Especially when you factor in the notion that there's always "haters" in any community, I really find that simply incredible.

2018年05月24日38分48秒

One interesting difference between string and String is that string' is a keyword in c#, so you can not use it as a variable name.For Ex: string string="hi";//compiler error, but String String="hi";` is acceptable, as String is an identifire not a keyword.

2018年05月24日38分48秒

SanjeevRai: Yes. You can use string to create an identifier which ends up as string though. It's a sort of escaping mechanism.

2018年05月24日38分48秒

` If you code in C#, I'd prefer string as it's a C# type alias and well-known by C# programmers` - when would a C# person not know the .NET framework. +1 as I think generally this is the best answer, but the point I mention seems odd.

2018年05月24日38分48秒

I personally prefer using "Int32", since it immediately shows the range of the value. Imagine if they upgraded the type of "int" on later higher-bit systems. 'int' in c is apparently seen as "the integer type that the target processor is most efficient working with", and defined as "at least 16 bit". I'd prefer predictable consistency there, thank you very much.

2018年05月24日38分48秒

MyDaftQuestions I concur. If anything it would make sense to consistently use the .net types because they are language ignorant and the type is obvious, independent of any language (do I know all of F#'s or VB's idiosyncrasies?).

2018年05月24日38分48秒

The second point sounds actually like a reason not to use string, int etc.

2018年05月24日38分48秒

MauganRa And it's supposed to, the author of the book lists those reasons as to why he doesn't use aliases.

2018年05月24日38分48秒

"If someone is reading C# source code they should interpret long according to the language spec, not another languages spec." That misses the point entirely. It's not that anyone intends to misinterpret code, it's simply easy for one's brain to jump to the wrong conclusion when a type has a different meaning than what the programmer sees on a daily basis in another context. We all make mistakes; using explicitly named types makes those mistakes less likely.

2018年05月24日38分48秒

+These reasons sum up my feelings on the matter. When I first started coding in C# (coming from a Java/C++/C background) I thought the aliases were ugly. I still feel that way, unfortunately most of the world doesn't seem to agree with me, or they don't care, and so use the lowercase.

2018年05月24日38分48秒

jinzai the question is about C#, in which long is defined as a signed 64-bit integer, regardless of the platform or the compiler. So in some cases at least, yes, it does depend on the language.

2018年05月24日38分48秒

Keep in mind that calling a local string is really rather pointless, since the names of locals are only present in PDBs. Might as well call it _string or something. It makes more sense for things that have names accessible via reflection, where the name of an string member would be "string".

2018年05月24日38分48秒

Also keep in mind using a reserved word as a variable name is grossly inelegant.

2018年05月24日38分48秒

"stack overflow highlights them differently". No more reasons needed :)

2018年05月24日38分48秒

The OP does not want to use String or string as a variable name. They asked for an explanation of the difference between these Types. Your answer only serves to add more confusion IMO

2018年05月24日38分48秒

by default most people do add this in any ways at the top of the file. VS does this by default in most cases of not all!

2018年05月24日38分48秒

By default I add only using statements I require, and explicitly remove all that I don't. Power Productivity Tools > "[x] Remove and Format Usings on save"

2018年05月24日38分48秒

JMD I've modified the .cs template file so it doesn't even have any using statements at the top! I also changed the class template to internal sealed.

2018年05月24日38分48秒

+1 for stating that there are more important things in life, I feel here is yet another StackOverflow millions of upvote question about a trivial matter is taking place: en.wikipedia.org/wiki/Parkinson's_law_of_triviality

2018年05月24日38分48秒

Just pick one and be consistent. If you work somewhere with a house style, use that.

2018年05月24日38分48秒

unfortunately style is personal preference and may be too expensive to enforce in a large code base across several teams without dedicated code owner. there are always more important matters to take care of rather than string vs String. which brings us back to "more important things in life"

2018年05月24日38分48秒

Do you also write code like: Int32 i = 1; Rather than int i = 1; ? Seems inconsistent to not use the string alias when it's availble.

2018年05月24日38分48秒

nashwan: actually, yes, I do use Int32 i=1; intstead of int i = 1; I find the former to be more readable as to my intent: namely that I want a 32 bit signed integer.

2018年05月24日38分48秒

Well I guess it all depends whether the developer thinks they are writing C# code (string) or .NET code (String). Personally I foremost think I'm writing C# (and it's C# that is using .NET).

2018年05月23日38分48秒

Alex: my point was simply that I prefer to be very specific in my coding in order to remove ambiguity.

2018年05月24日38分48秒

On the absolute other end of the spectrum, I nearly always just use var

2018年05月24日38分48秒

Jeffrey Richter recommends using the CLR type in all cases (CLR via C#) to avoid exactly the kind of confusion that is taking place here.

2018年05月24日38分48秒

Clearly, whether you use S or s it will have caused this questions, so down-vote Richter. ;)

2018年05月24日38分48秒

Richter meant that string shouldn't have been an option - Microsoft shouldn't have it in the language. You can't down-vote Richter - he's a legend! :)

2018年05月24日38分48秒

Fair point Jon, but I just happen to agree with Richter on this point about String. And yes, I totally agree - CLR via C# is wonderful!

2018年05月24日38分48秒

"string" is not the same as "String". Is means "System.String". So if you use "String" you have to put "using System" to include the namespace

2018年05月24日38分48秒

string is not a type in C#; it is a reserved word that maps to a type in the CLR.

2018年05月23日38分48秒

CesarGon: According to ECMA-334, section 8.2.1: "C# provides a set of predefined types [...] The predefined reference types are object and string."

2018年05月24日38分48秒

According to ECMA-334, section 9.4.3, "string" is a keyword. :-) I agree with you that "string" is a type if you focus on the semantics, but I'd say it's a keyword (i.e. a reserved word) if you focus on the syntax. The standard backs both points of view (perhaps too ambiguously!). To me, the OP is about syntax, so I tend to focus on syntax when I look at answers, but I see your point too. Furthermore, your answer, as it stands, may be interpreted as to mean that two different types exist: string and String, when that is not the case. One is a maping to the other.

2018年05月24日38分48秒

Let's be clear about this. 'string' is a reserved alias. It is not a true datatype. It is something that points to something else. You can remove all these aliases (or just never use them) and have a perfectly good programming language.

2018年05月24日38分48秒

"In short "String" is an alias ( the same thing called with different names) of "string"". This is not correct: the alias is "string".

2018年05月24日38分48秒

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. This convention seems to be no more valid: if you use Visual Studio 2015 and try to write String it suggest you to "simplify your code", carrying it to string...

2018年05月24日38分48秒

You don't need to prefix System to use String.

2018年05月24日38分48秒

You do have to include a using System when using String, otherwise you get the following error: The type or namespace name 'String' could not be found (are you missing a using directive or an assembly reference?)

2018年05月24日38分48秒

That's the problem, they are not 'C#' aliases, they are 'C' aliases. There is no native 'string' or 'int' in the C# language, just syntactic sugar.

2018年05月24日38分48秒

not sure where "C" came from here, since C# 5 language specification reads "The keyword string is simply an alias for the predefined class System.String." on page 85, paragraph 4.2.4. All high level languages are syntactic sugar over CPU instruction sets and bytecode.

2018年05月24日38分48秒

I wasn't aware that StyleCop would flag String use - except for static methods. I think that is great as that is how I always use it: string for type declarations and String when I access the static members.

2018年05月24日38分48秒

Converting between C# and VB.NET is easy enough as it is. developerfusion.com/tools/convert/vb-to-csharp

2018年05月24日38分48秒

In a 64 bit build, int maps to System.Int64 (8 bytes) , in 32 bit build it maps to System.Int32 (4 bytes)

2018年05月24日38分48秒

Alex: Nope, see msdn.microsoft.com/en-us/library/ya5y69ds.aspx

2018年05月24日38分48秒

IntPtr and UIntPtr are the only types that change size according to platform (disregarding actual pointer types like int* and types composed of [U]IntPtrs or actual pointers).

2018年05月24日38分48秒

stackoverflow.com/questions/651956/sizeofint-on-x64

2018年05月24日38分48秒

I suppose to be diabolical one could: using String = System.Int32; using Int32 = System.String; and then count the bugs.

2018年05月24日38分48秒

The only tiny difference is that if you use the String class, you need to import the System namespace on top of your file, whereas you don’t have to do this when using the string keyword.

2018年05月24日38分48秒