本文整理汇总了C#中System.String.IsInterned方法的典型用法代码示例。如果您正苦于以下问题:C# String.IsInterned方法的具体用法?C# String.IsInterned怎么用?C# String.IsInterned使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.IsInterned方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompilationRelaxations
// Sample for String.IsInterned(String)
using System;
using System.Text;
using System.Runtime.CompilerServices;
// In the .NET Framework 2.0 the following attribute declaration allows you to
// avoid the use of the interning when you use NGEN.exe to compile an assembly
// to the native image cache.
[assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)]
class Sample
{
public static void Main()
{
// String str1 is known at compile time, and is automatically interned.
String str1 = "abcd";
// Constructed string, str2, is not explicitly or automatically interned.
String str2 = new StringBuilder().Append("wx").Append("yz").ToString();
Console.WriteLine();
Test(1, str1);
Test(2, str2);
}
public static void Test(int sequence, String str)
{
Console.Write("{0}) The string, '", sequence);
String strInterned = String.IsInterned(str);
if (strInterned == null)
Console.WriteLine("{0}', is not interned.", str);
else
Console.WriteLine("{0}', is interned.", strInterned);
}
}
输出:
1) The string, 'abcd', is interned. 2) The string, 'wxyz', is not interned. If you use NGEN.exe to compile the assembly to the native image cache, this example produces the following results: 1) The string, 'abcd', is not interned. 2) The string, 'wxyz', is not interned.
示例2: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string str1 = "a";
string str2 = str1 + "b";
string str3 = str2 + "c";
string[] strings = { "value", "part1" + "_" + "part2", str3,
String.Empty, null };
foreach (var value in strings) {
if (value == null) continue;
bool interned = String.IsInterned(value) != null;
if (interned)
Console.WriteLine("'{0}' is in the string intern pool.",
value);
else
Console.WriteLine("'{0}' is not in the string intern pool.",
value);
}
}
}
输出:
'value' is in the string intern pool. 'part1_part2' is in the string intern pool. 'abc' is not in the string intern pool. '' is in the string intern pool.