如何獲取系統對指定 String 的引用?


要訪問系統對特定 String 的引用,程式碼如下:

示例

 線上示例

using System;
public class Demo{
   public static void Main(string[] args){
      string str1 = "David";
      string str2 = string.Intern(str1);
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("System reference of String1 = "+str2);
   }
}

輸出

這將產生以下輸出:

String1 = David 
System reference of String1 = David

示例

現在我們再看另一個示例:

 線上示例

using System;
public class Demo{
   public static void Main(string[] args){
      string str1 = "50";
      string str2 = "100";
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("String2 = "+str2);
      str2 = string.Intern(str1);
      Console.WriteLine("System reference of String1 = "+str2);
   }
}

輸出

這將產生以下輸出:

String1 = 50
String2 = 100
System reference of String1 = 50

更新於:11-12-2019

44 次觀看

職業進階

透過完成課程獲得認證

開始
廣告
© . All rights reserved.