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