C#中,字符串的声明,你使用String还是string?
String? string? 只有一个大小写的问题,你习惯用哪个?知道具体的区别吗?
我是习惯了用string,区别也是最近才找到了权威的根据,'CLR via C#'。
其实,String是CLR的类型名称(也算是keyword),而string是C#中的keyword。在C#的编译时,默认会增加几行代码,看了你就会明白string和String的区别了!
using string = System.String; using sbyte = System.SByte; using byte = System.Byte; using short = System.Int16; using ushort = System.UInt16; using int = System.Int32; using uint = System.UInt32; ... ...
对了! using string = System.String; C#编译器,会自动的把string转化为Sysem.string!
在CLR via C#中,Jeffrey Richter建议coding时,使用CLR默认的类型,也就是说,不要string,要String;不要int要Int32!至于为什么,还是大家自己看看这本书吧,very useful!
原贴地址:http://www.cnblogs.com/skywind/archive/2007/04/30/733414.html
string属于C#的类型
String属于.NET 的类型
在.NET 所有的类型都是对象。下面是.NET C# VB.NET IL的类型对应表:
NET C# VB.NET IL 值或引用
System.Boolean bool Boolean bool Value
System.Byte byte Byte unsigned int8 Value
System.Char char Char char Value
System.DateTime - Date - Value
System.Decimal decimal Decimal - Value
System.Double double Double float64 Value
System.Int16 short Short int16 Value
System.Int32 int Integer int32 Value
System.Int64 long Long int64 Value
System.Object object Object object Reference
System.SByte sbyte - int8 Value
System.Single float Single float32 Value
System.String string String string Reference
System.UInt16 ushort - unsigned int16 Value
System.UInt32 uint - unsigned int32 Value
System.UInt64 ulong - unsigned int64 Value
从上表可以看到,不仅仅string和String,还有许多类似的类型。有的没有对应的类型,这时使用的就是.NET的,比如说在C#中没有date类型,所以在C#中我们使用.NET 的Date. 这也是.NET的跨语言特性之一。