在C#中string 和String到底有没有区别? 妖狐艹你老母 2022-07-11 05:38 178阅读 0赞 [在C\#中string 和String到底有没有区别?][C_string _String] 今天上班的时候看到了几行C\#代码,其中一行用的是string,另一个用的是String;大家都知道string是C\#中System.String的别名,一般情况下二者具有相同的含义和功能,那问题时二者是完全相同没有任何区别的吗?如果答案是“是”的话,微软为什么实现了这两个概念而不是仅仅用sting或者String呢,毕竟用一个的话跟简单,给开发人员带来的困惑也更少。 这个问题纯粹是概念上和理解上的问题,对您的编程技巧没有实质的帮助,如果没有兴趣的话你看到这里就可以为止了;如果您是技术大牛,完全明白这二者的含义,您可以忽略本文也可以帮我指正错误。 好了,既然您继续向下看,那我也就继续说了哦J. 为了使本文描述更准确(其实是担心自己理解不全面,误导了继续看的dev们),我特地[google了一下][google],下面的分析中很多是参考了搜索结果的内容,甚至是照搬了部分代码,当然我也会在相应的位置上注明引用的URL地址。 不废话了,直接上代码开始分析(请注意下面代码的颜色,这是我用插入代码的方式自动生成的)。 1 string s = “Hello, World.”; 2 String S = “Hello, World.”; 在含义上相当于是using string = System.String,你可以使用typeof(string) == typeof(System.String)来判断;从技术上来讲二者没有什么区别,正如int和Int32一样。很多编程指导原则中推荐这样的一种使用方法:当你使用对象时用string,当作类时用String,例如string s = String.Format(“Hello, \{0\}.”, “World”),这是微软在示例[String.Format][]方法时的用法;而StyleCop现在强制使用C\#规范的别名,也就是说使用StyleCop的话它会强制你使用string而不是String。 类似的C\#别名还有: <table> <tbody> <tr> <td> <p><strong>C#别名</strong></p> </td> <td> <p><strong>BCL全称</strong></p> </td> </tr> <tr> <td> <p>object</p> </td> <td> <p>System.Object</p> </td> </tr> <tr> <td> <p>string</p> </td> <td> <p>System.String</p> </td> </tr> <tr> <td> <p>bool</p> </td> <td> <p>System.Boolean</p> </td> </tr> <tr> <td> <p>byte</p> </td> <td> <p>System.Byte</p> </td> </tr> <tr> <td> <p>sbyte</p> </td> <td> <p>System.SByte</p> </td> </tr> <tr> <td> <p>short</p> </td> <td> <p>System.Int16</p> </td> </tr> <tr> <td> <p>ushort</p> </td> <td> <p>System.UInt16</p> </td> </tr> <tr> <td> <p>int</p> </td> <td> <p>System.Int32</p> </td> </tr> <tr> <td> <p>uint</p> </td> <td> <p>System.UInt32</p> </td> </tr> <tr> <td> <p>long</p> </td> <td> <p>System.Int64</p> </td> </tr> <tr> <td> <p>ulong</p> </td> <td> <p>System.UInt64</p> </td> </tr> <tr> <td> <p>float</p> </td> <td> <p>System.Single</p> </td> </tr> <tr> <td> <p>double</p> </td> <td> <p>System.Double</p> </td> </tr> <tr> <td> <p>decimal</p> </td> <td> <p>System.Decimal</p> </td> </tr> <tr> <td> <p>char</p> </td> <td> <p>System.Char</p> </td> </tr> </tbody> </table> 和string、object不同,其他的别名都是值类型的,其中decimal是值类型但不是CLR的基础类型(primitive type)。CLR中唯一没有别名的基础类型是System.IntPtr。 有一种情况是必须使用别名而不能使用CLR全名: 1 public enum Foo:Int32{} //编译错误 2 3 public enum Bar:int{} //编译正确 还有一种情况:string是C\#保留的关键字,而String是类名(不是保留关键字),也就是说如果你把String用作变量名是不会有编译错误的,而用string作变量名就会有编译错误。 1 StringBuilder String = new StringBuilder(); //编译正确 2 3 StringBuilder string = new StringBuilder();//编译错误 当然了,如果你非要用string作变量名的话请在string前加上@,即StringBuilder @string = new StringBuilder()这句话是编译能通过的。 还有一种情况:在反射中你只能使用String而不能使用string。 还有一种情况:在未使用using System情况下,你不能使用String而只能使用string。 最后一种情况:string是C\#的保留字,而String是CLR的类型,如果其他厂商实现一种运行时来支持C\#的话(恰好有没有支持String),结果就可想而知了。当然了这种情况基本上是不存在的,比彗星撞地球都几率小。 以上是对string和String区分的一个小总结,如果有遗漏或不正确的地方,欢迎指正。 标签: [ C\# CLR string String PrimitiveType][C_ CLR string String PrimitiveType] 好文要顶 关注我 收藏该文 ![icon_weibo_24.png][] ![wechat.png][] [![sample_face.gif][]][sample_face.gif 1] [Lidong Jia][sample_face.gif 1] [关注 - 0][- 0] [粉丝 - 0][- 0 1] \+加关注 0 0 [« ][Link 1]上一篇: [一辈子都难遇见的一个Exception啊,具体错误信息已经记录下来,忙过这段时间后就着手分析了][Link 1] [C_string _String]: http://www.cnblogs.com/it-laojia/p/3601682.html [google]: http://www.google.com.hk/#newwindow=1&q=difference+between+String+and+string+in+c%23&safe=strict [String.Format]: http://msdn.microsoft.com/en-us/library/System.String.Format.aspx#Format1_Example [C_ CLR string String PrimitiveType]: http://www.cnblogs.com/it-laojia/tag/C%23%20CLR%20string%20String%20PrimitiveType/ [icon_weibo_24.png]: http://common.cnblogs.com/images/icon_weibo_24.png [wechat.png]: /images/20220711/e7201fb54bee49658ba371869801e594.png [sample_face.gif]: /images/20220711/d4fc82265ab9458b95040e9aaa0104d6.png [sample_face.gif 1]: http://home.cnblogs.com/u/it-laojia/ [- 0]: http://home.cnblogs.com/u/it-laojia/followees [- 0 1]: http://home.cnblogs.com/u/it-laojia/followers [Link 1]: http://www.cnblogs.com/it-laojia/p/3571426.html
还没有评论,来说两句吧...