C# IsInterned() 方法用于获取指定字符串的引用。
Intern() 和 IsInterned() 之间的区别在于 Intern() 方法在字符串未被实习的情况下实习,但 IsInterned() 不这样做。在这种情况下,IsInterned() 方法返回 null。
声明
public static string IsInterned(String str)
参数
str:它是一个字符串类型的参数。
返回
它返回一个引用。
C# String 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# String 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