Skip to content

Commit f8b7fde

Browse files
authored
Fix String.Intern and String.IsInterned docs: samples and NativeAOT clarification (#12448)
1 parent 1616d87 commit f8b7fde

11 files changed

Lines changed: 161 additions & 158 deletions

File tree

snippets/csharp/System/String/Intern/string_intern.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,28 @@ class Sample
77
{
88
public static void Main()
99
{
10-
string s1 = "MyTest";
10+
string s1 = new StringBuilder().Append("My").Append("Test").ToString();
1111
string s2 = new StringBuilder().Append("My").Append("Test").ToString();
12-
string s3 = String.Intern(s2);
1312
Console.WriteLine($"s1 == {s1}");
1413
Console.WriteLine($"s2 == {s2}");
15-
Console.WriteLine($"s3 == {s3}");
16-
Console.WriteLine($"Is s2 the same reference as s1?: {(Object)s2 == (Object)s1}");
17-
Console.WriteLine($"Is s3 the same reference as s1?: {(Object)s3 == (Object)s1}");
14+
Console.WriteLine($"Are s1 and s2 equal in value? {s1 == s2}");
15+
Console.WriteLine($"Are s1 and s2 the same reference? {Object.ReferenceEquals(s1, s2)}");
16+
17+
string i1 = String.Intern(s1);
18+
string i2 = String.Intern(s2);
19+
Console.WriteLine($"After interning:");
20+
Console.WriteLine($" Are i1 and i2 equal in value? {i1 == i2}");
21+
Console.WriteLine($" Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}");
1822
}
1923
}
2024
/*
2125
This example produces the following results:
2226
s1 == MyTest
2327
s2 == MyTest
24-
s3 == MyTest
25-
Is s2 the same reference as s1?: False
26-
Is s3 the same reference as s1?: True
28+
Are s1 and s2 equal in value? True
29+
Are s1 and s2 the same reference? False
30+
After interning:
31+
Are i1 and i2 equal in value? True
32+
Are i1 and i2 the same reference? True
2733
*/
2834
//</snippet1>

snippets/csharp/System/String/IsInterned/isin.cs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,35 @@
22
// Sample for String.IsInterned(String)
33
using System;
44
using System.Text;
5-
using System.Runtime.CompilerServices;
65

7-
// In the .NET Framework 2.0 the following attribute declaration allows you to
8-
// avoid the use of the interning when you use NGEN.exe to compile an assembly
9-
// to the native image cache.
10-
[assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)]
116
class Sample
127
{
138
public static void Main()
149
{
15-
// String str1 is known at compile time, and is automatically interned.
16-
String str1 = "abcd";
10+
// Constructed strings are not automatically interned.
11+
string s1 = new StringBuilder().Append("My").Append("Test").ToString();
12+
string s2 = new StringBuilder().Append("My").Append("Test").ToString();
1713

18-
// Constructed string, str2, is not explicitly or automatically interned.
19-
String str2 = new StringBuilder().Append("wx").Append("yz").ToString();
20-
Console.WriteLine();
21-
Test(1, str1);
22-
Test(2, str2);
23-
}
14+
// Neither string is in the intern pool yet.
15+
Console.WriteLine($"Is s1 interned? {String.IsInterned(s1) != null}");
16+
Console.WriteLine($"Is s2 interned? {String.IsInterned(s2) != null}");
2417

25-
public static void Test(int sequence, String str)
26-
{
27-
Console.Write("{0}) The string, '", sequence);
28-
String strInterned = String.IsInterned(str);
29-
if (strInterned == null)
30-
Console.WriteLine("{0}', is not interned.", str);
31-
else
32-
Console.WriteLine("{0}', is interned.", strInterned);
33-
}
34-
}
18+
// Intern s1 explicitly.
19+
string i1 = String.Intern(s1);
3520

36-
//This example produces the following results:
21+
// Now s2 can be found in the intern pool.
22+
string i2 = String.IsInterned(s2);
3723

38-
//1) The string, 'abcd', is interned.
39-
//2) The string, 'wxyz', is not interned.
40-
41-
//If you use NGEN.exe to compile the assembly to the native image cache, this
42-
//example produces the following results:
24+
Console.WriteLine($"Is s2 interned after interning s1? {i2 != null}");
25+
Console.WriteLine($"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}");
26+
}
27+
}
4328

44-
//1) The string, 'abcd', is not interned.
45-
//2) The string, 'wxyz', is not interned.
29+
// This example produces the following results:
30+
//
31+
// Is s1 interned? False
32+
// Is s2 interned? False
33+
// Is s2 interned after interning s1? True
34+
// Are i1 and i2 the same reference? True
4635

4736
//</snippet1>

snippets/fsharp/System/String/Intern/fs.fsproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<Compile Include="Intern1.fs" />
9-
<Compile Include="Intern2.fs" />
108
<Compile Include="string_intern.fs" />
119
</ItemGroup>
12-
</Project>
10+
</Project>

snippets/fsharp/System/String/Intern/string_intern.fs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@ module string_intern.fs
44
open System
55
open System.Text
66

7-
let s1 = "MyTest"
7+
let s1 = StringBuilder().Append("My").Append("Test").ToString()
88
let s2 = StringBuilder().Append("My").Append("Test").ToString()
9-
let s3 = String.Intern s2
109
printfn $"s1 = {s1}"
1110
printfn $"s2 = {s2}"
12-
printfn $"s3 = {s3}"
13-
printfn $"Is s2 the same reference as s1?: {s2 :> obj = s1 :> obj}"
14-
printfn $"Is s3 the same reference as s1?: {s3 :> obj = s1 :> obj}"
11+
printfn $"Are s1 and s2 equal in value? {s1 = s2}"
12+
printfn $"Are s1 and s2 the same reference? {Object.ReferenceEquals(s1, s2)}"
13+
14+
let i1 = String.Intern s1
15+
let i2 = String.Intern s2
16+
printfn "After interning:"
17+
printfn $" Are i1 and i2 equal in value? {i1 = i2}"
18+
printfn $" Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}"
1519
(*
1620
This example produces the following results:
1721
s1 = MyTest
1822
s2 = MyTest
19-
s3 = MyTest
20-
Is s2 the same reference as s1?: False
21-
Is s3 the same reference as s1?: True
23+
Are s1 and s2 equal in value? True
24+
Are s1 and s2 the same reference? False
25+
After interning:
26+
Are i1 and i2 equal in value? True
27+
Are i1 and i2 the same reference? True
2228
*)
2329
//</snippet1>

snippets/fsharp/System/String/IsInterned/isin.fs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,29 @@ module isin.fs
33
// Sample for String.IsInterned(String)
44
open System
55
open System.Text
6-
open System.Runtime.CompilerServices
76

8-
// In the .NET Framework 2.0 the following attribute declaration allows you to
9-
// avoid the use of the interning when you use NGEN.exe to compile an assembly
10-
// to the native image cache.
11-
[<assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)>]
12-
do ()
7+
// Constructed strings are not automatically interned.
8+
let s1 = StringBuilder().Append("My").Append("Test").ToString()
9+
let s2 = StringBuilder().Append("My").Append("Test").ToString()
1310

14-
let test sequence str =
15-
printf $"%d{sequence}) The string, '"
16-
let strInterned = String.IsInterned str
17-
if isNull strInterned then
18-
printfn $"{str}', is not interned."
19-
else
20-
printfn $"{strInterned}', is interned."
11+
// Neither string is in the intern pool yet.
12+
printfn $"Is s1 interned? {String.IsInterned(s1) <> null}"
13+
printfn $"Is s2 interned? {String.IsInterned(s2) <> null}"
2114

22-
// String str1 is known at compile time, and is automatically interned.
23-
let str1 = "abcd"
15+
// Intern s1 explicitly.
16+
let i1 = String.Intern(s1)
2417

25-
// Constructed string, str2, is not explicitly or automatically interned.
26-
let str2 = StringBuilder().Append("wx").Append("yz").ToString()
27-
printfn ""
28-
test 1 str1
29-
test 2 str2
18+
// Now s2 can be found in the intern pool.
19+
let i2 = String.IsInterned(s2)
3020

21+
printfn $"Is s2 interned after interning s1? {i2 <> null}"
22+
printfn $"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}"
3123

32-
//This example produces the following results:
33-
34-
//1) The string, 'abcd', is interned.
35-
//2) The string, 'wxyz', is not interned.
36-
37-
//If you use NGEN.exe to compile the assembly to the native image cache, this
38-
//example produces the following results:
39-
40-
//1) The string, 'abcd', is not interned.
41-
//2) The string, 'wxyz', is not interned.
24+
// This example produces the following results:
25+
//
26+
// Is s1 interned? False
27+
// Is s2 interned? False
28+
// Is s2 interned after interning s1? True
29+
// Are i1 and i2 the same reference? True
4230

4331
//</snippet1>

snippets/visualbasic/System/String/Intern/string_intern.vb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ Imports System.Text
44
Class Sample
55

66
Public Shared Sub Main()
7-
Dim s1 As String = "MyTest"
7+
Dim s1 As String = New StringBuilder().Append("My").Append("Test").ToString()
88
Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()
9-
Dim s3 As String = String.Intern(s2)
109
Console.WriteLine($"s1 = {s1}")
1110
Console.WriteLine($"s2 = {s2}")
12-
Console.WriteLine($"s3 = {s3}")
13-
Console.WriteLine($"Is s2 the same reference as s1?: {s2 Is s1}")
14-
Console.WriteLine($"Is s3 the same reference as s1?: {s3 Is s1}")
11+
Console.WriteLine($"Are s1 and s2 equal in value? {s1 = s2}")
12+
Console.WriteLine($"Are s1 and s2 the same reference? {s1 Is s2}")
13+
14+
Dim i1 As String = String.Intern(s1)
15+
Dim i2 As String = String.Intern(s2)
16+
Console.WriteLine("After interning:")
17+
Console.WriteLine($" Are i1 and i2 equal in value? {i1 = i2}")
18+
Console.WriteLine($" Are i1 and i2 the same reference? {i1 Is i2}")
1519
End Sub
1620
End Class
1721
'
1822
's1 = MyTest
1923
's2 = MyTest
20-
's3 = MyTest
21-
'Is s2 the same reference as s1?: False
22-
'Is s3 the same reference as s1?: True
24+
'Are s1 and s2 equal in value? True
25+
'Are s1 and s2 the same reference? False
26+
'After interning:
27+
' Are i1 and i2 equal in value? True
28+
' Are i1 and i2 the same reference? True
2329
'
2430
'</snippet1>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Public Class Program
2+
Public Shared Sub Main()
3+
IsInExample.Run()
4+
IsInternedExample.Run()
5+
End Sub
6+
7+
End Class
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
1-
'<snippet1>
2-
' Sample for String.IsInterned(String)
1+
' Sample for String.IsInterned(String)
32
Imports System.Text
4-
Imports System.Runtime.CompilerServices
5-
6-
' In the .NET Framework 2.0 the following attribute declaration allows you to
7-
' avoid the use of the interning when you use NGEN.exe to compile an assembly
8-
' to the native image cache.
9-
<Assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)>
10-
Class Sample
11-
Public Shared Sub Main()
12-
' String str1 is known at compile time, and is automatically interned.
13-
Dim str1 As [String] = "abcd"
14-
15-
' Constructed string, str2, is not explicitly or automatically interned.
16-
Dim str2 As [String] = New StringBuilder().Append("wx").Append("yz").ToString()
17-
Console.WriteLine()
18-
Test(1, str1)
19-
Test(2, str2)
20-
End Sub
213

22-
Public Shared Sub Test(ByVal sequence As Integer, ByVal str As [String])
23-
Console.Write("{0}) The string, '", sequence)
24-
Dim strInterned As [String] = [String].IsInterned(str)
25-
If strInterned Is Nothing Then
26-
Console.WriteLine("{0}', is not interned.", str)
27-
Else
28-
Console.WriteLine("{0}', is interned.", strInterned)
29-
End If
30-
End Sub
31-
End Class
4+
Module IsInExample
5+
Public Sub Run()
6+
'<snippet1>
7+
8+
' Constructed strings are not automatically interned.
9+
Dim s1 As String = New StringBuilder().Append("My").Append("Test").ToString()
10+
Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()
3211

33-
'This example produces the following results:
12+
' Neither string is in the intern pool yet.
13+
Console.WriteLine($"Is s1 interned? {String.IsInterned(s1) IsNot Nothing}")
14+
Console.WriteLine($"Is s2 interned? {String.IsInterned(s2) IsNot Nothing}")
3415

35-
'1) The string, 'abcd', is interned.
36-
'2) The string, 'wxyz', is not interned.
16+
' Intern s1 explicitly.
17+
Dim i1 As String = String.Intern(s1)
3718

38-
'If you use NGEN.exe to compile the assembly to the native image cache, this
39-
'example produces the following results:
19+
' Now s2 can be found in the intern pool.
20+
Dim i2 As String = String.IsInterned(s2)
4021

41-
'1) The string, 'abcd', is not interned.
42-
'2) The string, 'wxyz', is not interned.
43-
'</snippet1>
22+
Console.WriteLine($"Is s2 interned after interning s1? {i2 IsNot Nothing}")
23+
Console.WriteLine($"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}")
24+
25+
' This example produces the following results:
26+
'
27+
' Is s1 interned? False
28+
' Is s2 interned? False
29+
' Is s2 interned after interning s1? True
30+
' Are i1 and i2 the same reference? True
31+
32+
'</snippet1>
33+
End Sub
34+
End Module

snippets/visualbasic/System/String/IsInterned/isinternedex1.vb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22
' Uses IsInterned to determine whether a string is interned (True) or not (False).
33
Option Strict On
44

5-
' <Snippet1>
6-
Module Example
7-
Public Sub Main()
8-
Dim str1 As String = "a"
9-
Dim str2 As String = str1 + "b"
10-
Dim str3 As String = str2 + "c"
11-
Dim strings() As String = { "value", "part1" + "_" + "part2", str3,
12-
String.Empty, Nothing }
13-
For Each value In strings
14-
If value Is Nothing Then Continue For
15-
16-
Dim interned As Boolean = (String.IsInterned(value) IsNot Nothing)
17-
If interned Then
18-
Console.WriteLine("'{0}' is in the string intern pool.",
19-
value)
20-
Else
21-
Console.WriteLine("'{0}' is not in the string intern pool.",
22-
value)
23-
End If
24-
Next
25-
End Sub
5+
Module IsInternedExample
6+
Public Sub Run()
7+
' <Snippet1>
8+
9+
Dim str1 As String = "a"
10+
Dim str2 As String = str1 + "b"
11+
Dim str3 As String = str2 + "c"
12+
Dim strings() As String = {"value", "part1" + "_" + "part2", str3,
13+
String.Empty, Nothing}
14+
For Each value In strings
15+
If value Is Nothing Then Continue For
16+
17+
Dim interned As Boolean = (String.IsInterned(value) IsNot Nothing)
18+
If interned Then
19+
Console.WriteLine($"'{value}' is in the string intern pool.")
20+
Else
21+
Console.WriteLine($"'{value}' is not in the string intern pool.")
22+
End If
23+
Next
24+
25+
' The example displays the following output:
26+
' 'value' is in the string intern pool.
27+
' 'part1_part2' is in the string intern pool.
28+
' 'abc' is not in the string intern pool.
29+
' '' is in the string intern pool.
30+
' </Snippet1>
31+
End Sub
2632
End Module
27-
' The example displays the following output:
28-
' 'value' is in the string intern pool.
29-
' 'part1_part2' is in the string intern pool.
30-
' 'abc' is not in the string intern pool.
31-
' '' is in the string intern pool.
32-
' </Snippet1>

0 commit comments

Comments
 (0)