Skip to content

Commit 5ffd99f

Browse files
authored
remove useless example (#44990)
1 parent 95e3b6e commit 5ffd99f

File tree

5 files changed

+14
-74
lines changed

5 files changed

+14
-74
lines changed

docs/fundamentals/runtime-libraries/snippets/System/String/Intern/csharp/Intern2.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

docs/fundamentals/runtime-libraries/snippets/System/String/Intern/fsharp/Intern2.fs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="Intern1.fs" />
9+
</ItemGroup>
10+
</Project>

docs/fundamentals/runtime-libraries/snippets/System/String/Intern/vb/Intern2.vb

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/fundamentals/runtime-libraries/system-string-intern.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,18 @@ dev_langs:
1111

1212
[!INCLUDE [context](includes/context.md)]
1313

14-
The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.
14+
The common language runtime conserves string storage by maintaining a table, called the *intern pool*, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system. For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.
1515

16-
For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.
16+
The <xref:System.String.Intern%2A> method uses the intern pool to search for a string equal to the value of its parameter, `str`. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to `str` is added to the intern pool, then that reference is returned. (In contrast, the <xref:System.String.IsInterned(System.String)> method returns a null reference if the requested string doesn't exist in the intern pool.)
1717

18-
The <xref:System.String.Intern%2A> method uses the intern pool to search for a string equal to the value of `str`. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to `str` is added to the intern pool, then that reference is returned.
19-
20-
In the following example, the string s1, which has a value of "MyTest", is already interned because it is a literal in the program. The <xref:System.Text.StringBuilder?displayProperty=nameWithType> class generates a new string object that has the same value as s1. A reference to that string is assigned to s2. The <xref:System.String.Intern%2A> method searches for a string that has the same value as s2. Because such a string exists, the method returns the same reference that is assigned to s1. That reference is then assigned to s3. References s1 and s2 compare unequal because they refer to different objects; references s1 and s3 compare equal because they refer to the same string.
18+
In the following example, the string `s1`, which has a value of "MyTest", is already interned because it is a literal in the program. The <xref:System.Text.StringBuilder?displayProperty=nameWithType> class generates a new string object that has the same value as `s1`. A reference to that string is assigned to `s2`. The <xref:System.String.Intern%2A> method searches for a string that has the same value as `s2`. Because such a string exists, the method returns the same reference that's assigned to `s1`. That reference is then assigned to `s3`. References `s1` and `s2` compare unequal because they refer to different objects; references `s1` and `s3` compare equal because they refer to the same string.
2119

2220
:::code language="csharp" source="./snippets/System/String/Intern/csharp/Intern1.cs" interactive="try-dotnet-method" id="Snippet1":::
2321
:::code language="fsharp" source="./snippets/System/String/Intern/fsharp/Intern1.fs" id="Snippet1":::
2422
:::code language="vb" source="./snippets/System/String/Intern/vb/Intern1.vb" id="Snippet1":::
2523

26-
Compare this method to the <xref:System.String.IsInterned%2A> method.
27-
28-
In the following example, the variable `str1` is assigned a reference to <xref:System.String.Empty?displayProperty=nameWithType>, and the variable `str2` is assigned the reference to <xref:System.String.Empty?displayProperty=nameWithType> that is returned by calling the <xref:System.String.Intern%2A> method after converting a <xref:System.Text.StringBuilder> object whose value is <xref:System.String.Empty?displayProperty=nameWithType> to a string. Then the references contained in `str1` and `str2` are compared for equality. `str1` and `str2` are equal.
29-
30-
:::code language="csharp" source="./snippets/System/String/Intern/csharp/Intern2.cs" interactive="try-dotnet-method" id="Snippet2":::
31-
:::code language="fsharp" source="./snippets/System/String/Intern/fsharp/Intern2.fs" id="Snippet2":::
32-
:::code language="vb" source="./snippets/System/String/Intern/vb/Intern2.vb" id="Snippet2":::
33-
3424
## Performance considerations
3525

36-
If you are trying to reduce the total amount of memory your application allocates, keep in mind that interning a string has two unwanted side effects. First, the memory allocated for interned <xref:System.String> objects is not likely to be released until the common language runtime (CLR) terminates. The reason is that the CLR's reference to the interned <xref:System.String> object can persist after your application, or even your application domain, terminates. Second, to intern a string, you must first create the string. The memory used by the <xref:System.String> object must still be allocated, even though the memory will eventually be garbage collected.
26+
If you're trying to reduce the total amount of memory your application allocates, keep in mind that interning a string has two unwanted side effects. First, the memory allocated for interned <xref:System.String> objects is not likely to be released until the common language runtime (CLR) terminates. The reason is that the CLR's reference to the interned <xref:System.String> object can persist after your application, or even your application domain, terminates. Second, to intern a string, you must first create the string. The memory used by the <xref:System.String> object must still be allocated, even though the memory will eventually be garbage collected.
3727

3828
The <xref:System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning?displayProperty=nameWithType> enumeration member marks an assembly as not requiring string-literal interning. You can apply <xref:System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning> to an assembly using the <xref:System.Runtime.CompilerServices.CompilationRelaxationsAttribute> attribute. Also, when you use [Ngen.exe (Native Image Generator)](../../framework/tools/ngen-exe-native-image-generator.md) to compile an assembly in advance of run time, strings are not interned across modules.

0 commit comments

Comments
 (0)