本文整理汇总了C#中System.String.Intern方法的典型用法代码示例。如果您正苦于以下问题:C# String.Intern方法的具体用法?C# String.Intern怎么用?C# String.Intern使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.Intern方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
// Sample for String.Intern(String)
using System;
using System.Text;
class Sample
{
public static void Main()
{
string s1 = "MyTest";
string s2 = new StringBuilder().Append("My").Append("Test").ToString();
string s3 = String.Intern(s2);
Console.WriteLine($"s1 == {s1}");
Console.WriteLine($"s2 == {s2}");
Console.WriteLine($"s3 == {s3}");
Console.WriteLine($"Is s2 the same reference as s1?: {(Object)s2 == (Object)s1}");
Console.WriteLine($"Is s3 the same reference as s1?: {(Object)s3 == (Object)s1}");
}
}
输出:
s1 == MyTest s2 == MyTest s3 == MyTest Is s2 the same reference as s1?: False Is s3 the same reference as s1?: True
示例2: StringBuilder
string s1 = "MyTest";
string s2 = new StringBuilder().Append("My").Append("Test").ToString();
string s3 = String.Intern(s2);
Console.WriteLine((Object)s2==(Object)s1); // Different references.
Console.WriteLine((Object)s3==(Object)s1); // The same reference.
示例3: StringBuilder
string str1 = String.Empty;
string str2 = String.Empty;
StringBuilder sb = new StringBuilder().Append(String.Empty);
str2 = String.Intern(sb.ToString());
if((object)str1==(object)str2)
Console.WriteLine("The strings are equal.");
else
Console.WriteLine("The strings are not equal.");