當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# String IsInterned()用法及代碼示例

C# IsInterned() 方法用於獲取指定字符串的引用。

Intern() 和 IsInterned() 之間的區別在於,如果 Intern() 方法不實習,則 Intern() 方法實習字符串,但 IsInterned() 不實習。在這種情況下,IsInterned() 方法返回 null。

簽名

public static string IsInterned(String str)

參數

str:它是一個字符串類型參數。

返回

它返回一個引用。

C# 字符串 IsInterned() 方法示例

using System; 
        
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
          string s1 = "Hello C#";
           string s2 = string.Intern(s1);  
           string s3 = string.IsInterned(s1);
           Console.WriteLine(s1);
           Console.WriteLine(s2);
           Console.WriteLine(s3);
         }  
     }

輸出:

Hello C#
Hello C#
Hello C#

C# 字符串 Intern() 與 IsInterned() 示例

using System; 
        
    public class StringExample  
    {  
      public static void Main(string[] args)  
      {  
        string a = new string(new[] {'a'});
        string b = new string(new[] {'b'});

        string.Intern(a); // Interns it
        Console.WriteLine(string.IsInterned(a) != null);//True

        string.IsInterned(b); // Doesn't intern it
        Console.WriteLine(string.IsInterned(b) != null);//False
       }  
     }

輸出:

True
False




相關用法


注:本文由純淨天空篩選整理自 C# String IsInterned()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。