Lucene.NET 给索引库添加文档时 String.Intern 方法 详解(来自MSND)
调试lucene.NET时候遇到的,希望对大家有用。
-------------
更新: 2008 年 7 月
检索系统对指定 String 的引用。
命名空间: System
程序集: mscorlib(在 mscorlib.dll 中)
语法
| C# |
|---|
public static string Intern( string str ) |
| Visual C++ |
|---|
public: static String^ Intern( String^ str ) |
参数
- str
- 类型:System..::.String
异常
| 异常 | 条件 |
|---|---|
| ArgumentNullException |
str 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。 |
备注
公共语言运行时通过维护一个表来存放字符串,该表称为“暂存池”,它包含程序中以编程方式声明或创建的每个唯一的字符串的一个引用。因此,具有特定值的字符串的实例在系统中只有一个。
例如,如果将同一字符串分配给几个变量,运行时就会从暂存池中检索对该字符串的相同引用,并将它分配给各个变量。
Intern 方法使用暂存池来搜索与 str 值相等的字符串。如果存在这样的字符串,则返回暂存池中它的引用。如果不存在,则向暂存池添加对 str 的引用,然后返回该引用。
在下面的示例中,值为“MyTest”的字符串 s1 已被拘留,因为它是程序中的文本。
| C# | |
|---|---|
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. |
|
将此方法与 IsInterned 方法进行比较。
版本注意事项
在 .NET Framework 3.5 版 Service Pack 1 中,对于空字符串留用,Intern 方法将恢复其在 .NET Framework 1.0 版 和 .NET Framework 1.1 版 中的行为。在下面的示例中,在将值为 Empty 的
| C# | |
|---|---|
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.");
|
|
在 .NET Framework 1.0、.NET Framework 1.1 和 .NET Framework 3.5 SP1 中,str1 与 str2 不相等。在 .NET Framework 2.0 Service Pack 1 和 .NET Framework 3.0 版 中,str1 与 str2 相等。
性能注意事项
如果要减少应用程序分配的内存总量,请记住留用字符串有两个不希望出现的副作用。首先,为留用的 String 对象分配的内存在公共语言运行时 (CLR) 终止之前不大可能释放。这是因为 CLR 对留用的 String 对象的引用可能保持到应用程序终止之后,甚至可能保持到应用程序域终止之后。其次,要留用字符串,必须先创建字符串。即使 String 对象使用的内存最终将通过垃圾回收,仍然必须分配该内存。
.NET Framework 2.0 版引入了
示例
下面的代码示例使用三个值相等的字符串确定新建的字符串和留用的字符串是否相等。
| C# | |
|---|---|
// 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 == '{0}'", s1); Console.WriteLine("s2 == '{0}'", s2); Console.WriteLine("s3 == '{0}'", s3); Console.WriteLine("Is s2 the same reference as s1?: {0}", (Object)s2==(Object)s1); Console.WriteLine("Is s3 the same reference as s1?: {0}", (Object)s3==(Object)s1); } } /* This example produces the following results: s1 == 'MyTest' s2 == 'MyTest' s3 == 'MyTest' Is s2 the same reference as s1?: False Is s3 the same reference as s1?: True */ |
|
| Visual C++ | |
|---|---|
// Sample for String::Intern(String) using namespace System; using namespace System::Text; int main() { String^ s1 = "MyTest"; String^ s2 = (gcnew StringBuilder)->Append( "My" )->Append( "Test" )->ToString(); String^ s3 = String::Intern( s2 ); Console::WriteLine( "s1 == '{0}'", s1 ); Console::WriteLine( "s2 == '{0}'", s2 ); Console::WriteLine( "s3 == '{0}'", s3 ); Console::WriteLine( "Is s2 the same reference as s1?: {0}", s2 == s1 ); Console::WriteLine( "Is s3 the same reference as s1?: {0}", s3 == s1 ); } /* This example produces the following results: s1 == 'MyTest' s2 == 'MyTest' s3 == 'MyTest' Is s2 the same reference as s1?: False Is s3 the same reference as s1?: True */ |
|
平台
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见
版本信息
.NET Framework
受以下版本支持:3.5、3.0、2.0、1.1、1.0.NET Compact Framework
受以下版本支持:3.5、2.0、1.0XNA Framework
受以下版本支持:2.0、1.0另请参见
修订记录
|
日期 |
修订 |
原因 |
|---|---|---|
|
2008 年 7 月
|
在“版本注意事项”部分中增加了对 .NET Framework 3.5 SP1 中的方法行为的讨论。 |
内容 Bug 修复 |

浙公网安备 33010602011771号