开发者

why two keywords in c#: String and string [duplicate]

开发者 https://www.devze.com 2023-03-26 11:40 出处:网络
This question already exists: Closed 11 years ago. Possible Duplicate: String vs string in C# I see there are 2 different keywords (classes ?) one which starts with capital S and other wit
This question already exists: Closed 11 years ago.

Possible Duplicate:

String vs string in C#

I see there are 2 different keywords (classes ?) one which starts with capital S and other with sm开发者_高级运维all in c#.

String and string

What is the reason ? is one not sufficient ?


string is a keyword and alias of System.String class, System.String is a class. Also there's more aliases than string:

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

The best answer comes from Jeffrey Richter in his book CLR Via C#. Here are his 3 reasons:

  • I've seen a number of developers confused, not knowing whether to use string or String in their code. Because in C# the string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used.

  • In C#, long maps to System.Int64, but in a different programming language, long could map to an Int16 or Int32. In fact, C++/CLI does in fact treat long as an Int32. Someone reading source code in one language could easily misinterpret the code's intention if he or she were used to programming in a different programming language. In fact, most languages won't even treat long as a keyword and won't compile code that uses it.

  • The FCL has many methods that have type names as part of their method names. For example, the BinaryReader type offers methods such as ReadBoolean, ReadInt32, ReadSingle, and so on, and the System.Convert type offers methods such as ToBoolean, ToInt32, ToSingle, and so on.


string is a C# language alias for the actual class System.String.

You can safely use either. I've seen most people prefer string. If I'm working on an API I'll typically use System.String instead although both are safe.

There's plenty of other aliases, too. For example int is an alias for System.Int32, bool for System.Boolean. The string example just happens to be a casing difference so this question comes up a lot.


Actually, there is only one keyword for string (all C# keywords are lowercase). They mean the same thing. They are only interchangeable, though, if you have a using System; import.


String is the actual class you are using. It is just another class, nothing more. However, since strings are so vital in programming, Microsoft decided to bake them into the language by creating a keyword string that was just an alias for System.String. There is no such thing as an instance of string because it is not a class.

This gave strings elevated status and made them a lot easier to find (imagine trying to write a hello world style programming and have to actually look around and find the String class...) :D

0

精彩评论

暂无评论...
验证码 换一张
取 消