[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
-----------------------------------------------------------------
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM