C# string 特殊參考型別

來源:互聯網
上載者:User

C#有string關鍵字,在翻譯成.NET類時,它就是System.String.有了它,像字串串連和字串複製這樣的操作就簡單了.

1.  string 是參考型別還是實值型別

參考型別操作

當使用重載操作符”=”給string對象賦值時,string的對象是參考型別,它保留在堆上,而不是堆棧上.因此,當把一個字串賦給另一個字串時,會得到對記憶體中同一個字串的兩個引用.例如,修改其中一個字串,就會建立一個全新的string對象(注意,這個過程發生在”=”中),而另一個字串沒有改變.考慮下面的代碼:

public class MyClass

{

      public static void Main()

      {

        string str1 = "I am a number";

        string str2 = str1;

        Console.WriteLine("str1 = "+str1);

        Console.WriteLine("str2 = "+str2);

       

        str1 = "I am another number"; 

        Console.WriteLine("after str1 changed... str1 = "+str1);

        Console.WriteLine("after str1 changed... str2 = "+str2);

           Console.ReadLine();

      }   

}

 

Output :

str1 = I am a number

str2 = I am a number

after str1 changed...str1 = I am another number

after str1 changed...str2 = I am a number

 

具有實值型別特徵的操作

string有兩種情況下的操作是具有實值型別特徵的:

<!--[if !supportLists]-->1)        <!--[endif]-->在函數中傳遞string(比如函數參數是string型)時,傳遞的是地址,但卻不能修改成員變數,原因是它重新又建立了一個全新的對象,和它想修改的那個成員變數非同一地址,所以看上去像是實值型別;

<!--[if !supportLists]-->2)        <!--[endif]-->str1 == str2 ,僅僅是比較了值,而非地址(是MS重寫了==運算子所致).

總結:

string 到底是參考型別還是實值型別   答:參考型別 .   只不過它在某此操作上會表現出實值型別的特徵.

string類型的另外一個特殊性在於它是“不會變”的,每次操作string,都相當於建立了一個string對象.
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

2.  ”@”在string中的用法

       都知道如果要使用逸出字元的話,需要在字元前加上”\”,而C#提供了一種新的機制,使用”@”.在”@”後的字串都看作是原意,不會解釋為逸出字元串.並且以”@”開頭的字串支援斷行符號換行的顯示方式(見下例).不過會增加到字串長度,不推薦過多使用.

public class MyClass

{

      public static void Main()

      {

        string str1 = @"HelloWorld!";

        string str2 = @"line1: Hello

        line2: World!";

        Console.WriteLine("str1 length = "+str1.Length);

        Console.WriteLine("str2 length = "+str2.Length);

        Console.ReadLine();

      }   

}

Output :

str1 length = 11

str2 length = 34

 

 

3.  String 和 string 的區別:

       String是CLR(執行階段程式庫)的類型名字,而string是C#中的關鍵字.其實C#在編譯時間,會增加代碼(下面列出的),將string轉換成System.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;

 

 

4.  ”@”的其它用法

       在 C#  規範中, ”@”可以作為標識符(類名、變數名、方法名等)的第一個字元,以允許C# 中保留關鍵字作為自己定義的標識符.

public class MyClass

{

      public static void Main()

      {

        @class c = new @class();

        c.@static();

        Console.ReadLine();

      }   

    public class @class

    {

        private int @bool;

        public void @static()

        {

            Console.WriteLine("I've been staticed...and @bool is "+this.@bool);

        }

        public @class()

        {

            this.@bool = 999;

        }

    }

}

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

5.  等於null 和 等於””的區別

       string = null; //不分配記憶體

       string = “”;     //分配位元組為0的記憶體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.