C#.Net 不区分大小写的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#.Net case-insensitive string
提问by GateKiller
Why does C#.Net allow the declaration of the string object to be case-insensitive?
为什么 C#.Net 允许字符串对象的声明不区分大小写?
String sHello = "Hello";
string sHello = "Hello";
Both the lower-case and upper-case S of the word String are acceptable and this seems to be the only object that allows this.
字符串的小写和大写 S 都是可以接受的,这似乎是唯一允许这样做的对象。
Can anyone explain why?
谁能解释为什么?
采纳答案by Keith
stringis a language keyword while System.Stringis the type it aliases.
string是一个语言关键字,而System.String是它的别名类型。
Both compile to exactly the same thing, similarly:
两者都编译为完全相同的东西,类似地:
intisSystem.Int32longisSystem.Int64floatisSystem.SingledoubleisSystem.DoublecharisSystem.CharbyteisSystem.ByteshortisSystem.Int16ushortisSystem.UInt16uintisSystem.UInt32ulongisSystem.UInt64
int是System.Int32long是System.Int64float是System.Singledouble是System.Doublechar是System.Charbyte是System.Byteshort是System.Int16ushort是System.UInt16uint是System.UInt32ulong是System.UInt64
I think in most cases this is about code legibility - all the basic system value types have aliases, I think the lower case stringmight just be for consistency.
我认为在大多数情况下这是关于代码易读性的——所有基本的系统值类型都有别名,我认为小写string可能只是为了一致性。
回答by lubos hasko
"String" is the name of the class. "string" is keyword that maps this class.
“字符串”是类的名称。“string”是映射此类的关键字。
it's the same like
它是一样的
- Int32 => int
- Decimal => decimal
- Int64 => long
- Int32 => int
- 十进制 => 十进制
- Int64 => 长
... and so on...
... 等等...
回答by ZombieSheep
string is an alias for System.String. They are the same thing.
string 是 System.String 的别名。他们是一样的东西。
By convention, though, objects of type (System.String) are generally refered to as the alias - e.g.
但是,按照惯例,类型 (System.String) 的对象通常被称为别名——例如
string myString = "Hello";
whereas operations on the class use the uppercase version e.g.
而对类的操作使用大写版本,例如
String.IsNullOrEmpty(myStringVariable);
回答by aku
"string" is a C# keyword. it's just an alias for "System.String" - one of the .NET BCL classes.
“字符串”是 C# 关键字。它只是“System.String”的别名——.NET BCL 类之一。
回答by Patrik Svensson
"string" is just an C# alias for the class "String" in the System-namespace.
“string”只是 System-namespace 中“String”类的 C# 别名。
回答by Iain Holder
Further to the other answers, it's good practice to use keywords if they exist.
除了其他答案之外,最好使用关键字(如果存在)。
E.g. you should use stringrather than System.String.
例如,您应该使用string而不是System.String。
回答by Brian Leahy
I use String and not string, Int32 instead of int, so that my syntax highlighting picks up on a string as a Type and not a keyword. I want keywords to jump out at me.
我使用 String 而不是 string,Int32 而不是 int,这样我的语法突出显示将字符串作为类型而不是关键字。我想让关键字跳出来。

