[C#] String与string的区别

 

C#是区分大小写的,但是我却发现C#中同时存在String与string,于是我很困惑,于是我上网搜索了一下,于是我了解了一些小知识。

MSDN中对string的说明:string is an alias for String in the .NET Framework。string是String的别名而已,string是c#中的类,String是Framework的类,C# string 映射为 Framework的 String。如果用string,编译器会把它编译成String,所以如果直接用String就可以让编译器少做一点点工作。

如果使用C#,建议使用string,比较符合规范 。 string始终代表 System.String(1.x) 或 ::System.String(2.0) ,String只有在前面有using System;的时候并且当前命名空间中没有名为String的类型(class、struct、delegate、enum)的时候才代表System.String。

string是关键字,String不是,也就是说string不能作为类、结构、枚举、字段、变量、方法、属性的名称,而String可以。


0.  问题:

1. C#到底是什么时候传引用?什么时候传值?

2. String传值还是传引用

3. string和String有什么区别?

4. String为什么是Immutable,怎么实现的?
以下查询结果以及我的理解: 

1. C#到底是什么时候传引用?什么时候传值?
传值的情况 :Struct、Enumeration、Numeric(Integral/Floating/decimal)、bool
传引用的情况:class、Delegate、Interface
当使用操作符"="以及函数传参数的时候:       传值的结果是把原对象复制了一份,接收者指向原对象。       传引用的结果是直接让接收者指向原对象。
 
有人说,我硬要把值当引用传怎么办?
a、用ref关键字
b、用数组,数组是class
c、凉拌:)
 
2. String传值还是传引用 C#的String声明是class String,当然是传引用。
不过,之所以有这个疑惑,多数是因为这个情况:
string a = "aaa";
string b = a;
b = "bbb";
或者是这么几行代码:
public void Swap(string s1, string s2)
{
    string temp=s1;
    s1=s2;
    s2=temp;
}

[C#] String与string的区别

这时候结果一打印,结果发现a的值还没有变,Swap也没有成功,这时候就会有幻觉:是不是没有传引用啊?
呵呵,string不会这么粗暴的打乱“声明为class就是传引用”这种规则的。
分析一下:
string a = "aaa"; //==> a----->new String("aaa")
string b = a;        //==> b----->a, 传引用
b = "bbb";          //==> b----->new String("bbb"), 传引用,b指向了一个新的字符串,a并没有变。
 
Swap函数也是这样,比如说传了a, b进去(a="aaa", b="bbb"),
    //s1----->a, s2----->b
    string temp=s1;//temp----->s1----->a
    s1=s2;               //s1----->s2----->b;
    s2=temp;          //s2----->temp----->a
结果是,s1和s2确实是Swap了,但是这种结果并不会影响到a和b
3. string和String有什么区别?
MSDN中对string的说明:string is an alias for String in the .NET Framework
呵呵string是String的别名而已,都是一家。
4. String为什么是Immutable,怎么实现的? immutable:对象一旦生成不可改变
关于怎么实现的,在明白了问题2之后很好办,只要不提供任何修改自己成员变量的方法就可以了。顺便声明为sealed,防止不清楚的后来者违反规定:)
String每个看似修改了成员变量的方法,事实上都返回了一个新的String。
比如String.Replace函数,事实上并没有改变原来的串,这也是为什么只有让str = str.Replace( foo, bar )才真正完成替换的原因。
 
 
下面是.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
-----------------------------------------------------------------
 
posted @ 2018-01-25 16:17  笑笑小白  阅读(48944)  评论(2编辑  收藏  举报