📜  C#|如何检索系统对指定字符串的引用

📅  最后修改于: 2021-05-29 16:31:02             🧑  作者: Mango

String.Intern(String)方法用于检索系统对指定String的引用。此方法使用实习生池搜索字符串等于指定字符串的值。
如果存在这样的字符串,则返回其在内部缓冲池中的引用,或者如果该字符串不存在,则将指定字符串的引用添加至内部缓冲池,然后返回该引用。
在这里,实习生池是一个表,其中包含对在程序中以编程方式声明或创建的每个唯一字面量字符串的单个引用。

句法:

public static string Intern (string strA);

在这里, strA是在内部池中搜索的字符串。

返回值:该方法的返回类型为System.String 。如果该方法是InterA,则它将返回系统对strA的引用。否则,使用strA值对字符串的新引用。

异常:如果str为null,则此方法将提供ArgumentNullException。

下面给出了一些示例,以更好地理解实现:

范例1:

// C# program to illustrate Intern() method
using System;   
             
public class GFG    
{    
      
    // main method
    public static void Main(string[] args)    
    {    
         
        // string
       string strA = "This is C# tutorial";  
          
       // retrive the system reference
       // of strA string by
       // using Intern() method
       string strB = string.Intern(strA);  
          
       // Display the strings
       Console.WriteLine(strA);  
       Console.WriteLine(strB);  
    }    
}    
输出:
This is C# tutorial
This is C# tutorial

范例2:

// C# program to illustrate the 
// use of Intern() Method
using System;
  
class GFG {
      
    public static void Main()
    {
  
        // strings
        string strA = "GeeksforGeeks";
        string strB = "GFG";
        string strC = "Noida";
        string strD = String.Intern(strA);
        string strE = String.Intern(strC);
  
        // Display string
        Console.WriteLine("string A == '{0}'", strA);
        Console.WriteLine("string B == '{0}'", strB);
        Console.WriteLine("string C == '{0}'", strC);
        Console.WriteLine("string D == '{0}'", strD);
        Console.WriteLine("string E == '{0}'", strE);
        Console.WriteLine();
  
        // Check the reference of strings
        Console.WriteLine("Is string A have the same reference as string B: {0}",
                                                    (Object)strA == (Object)strB);
                                                      
        Console.WriteLine("Is string B have the same reference as string C: {0}", 
                                                    (Object)strB == (Object)strC);
                                                      
        Console.WriteLine("Is string D have the same reference as string E: {0}",
                                                    (Object)strD == (Object)strE);
                                                      
        Console.WriteLine("Is string A have the same reference as string D: {0}",
                                                    (Object)strA == (Object)strD);
                                                      
        Console.WriteLine("Is string E have the same reference as string C: {0}",
                                                    (Object)strE == (Object)strC);
    }
}
输出:
string A == 'GeeksforGeeks'
string B == 'GFG'
string C == 'Noida'
string D == 'GeeksforGeeks'
string E == 'Noida'

Is string A have the same reference as string B: False
Is string B have the same reference as string C: False
Is string D have the same reference as string E: False
Is string A have the same reference as string D: True
Is string E have the same reference as string C: True

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system。字符串.intern?view = netframework-4.7.2#definition