Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions snippets/csharp/System/String/Intern/string_intern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ class Sample
{
public static void Main()
{
string s1 = "MyTest";
string s1 = new StringBuilder().Append("My").Append("Test").ToString();
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}");
Console.WriteLine($"Are s1 and s2 equal in value? {s1 == s2}");
Console.WriteLine($"Are s1 and s2 the same reference? {Object.ReferenceEquals(s1, s2)}");

string i1 = String.Intern(s1);
string i2 = String.Intern(s2);
Console.WriteLine($"After interning:");
Console.WriteLine($" Are i1 and i2 equal in value? {i1 == i2}");
Console.WriteLine($" Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}");
}
}
/*
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
Are s1 and s2 equal in value? True
Are s1 and s2 the same reference? False
After interning:
Are i1 and i2 equal in value? True
Are i1 and i2 the same reference? True
*/
//</snippet1>
51 changes: 20 additions & 31 deletions snippets/csharp/System/String/IsInterned/isin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,35 @@
// 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 strings are not automatically interned.
string s1 = new StringBuilder().Append("My").Append("Test").ToString();
string s2 = new StringBuilder().Append("My").Append("Test").ToString();

// 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);
}
// Neither string is in the intern pool yet.
Console.WriteLine($"Is s1 interned? {String.IsInterned(s1) != null}");
Console.WriteLine($"Is s2 interned? {String.IsInterned(s2) != null}");

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);
}
}
// Intern s1 explicitly.
string i1 = String.Intern(s1);

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

//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:
Console.WriteLine($"Is s2 interned after interning s1? {i2 != null}");
Console.WriteLine($"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}");
}
}

//1) The string, 'abcd', is not interned.
//2) The string, 'wxyz', is not interned.
// This example produces the following results:
//
// Is s1 interned? False
// Is s2 interned? False
// Is s2 interned after interning s1? True
// Are i1 and i2 the same reference? True

//</snippet1>
4 changes: 1 addition & 3 deletions snippets/fsharp/System/String/Intern/fs.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Intern1.fs" />
<Compile Include="Intern2.fs" />
<Compile Include="string_intern.fs" />
</ItemGroup>
</Project>
</Project>
22 changes: 14 additions & 8 deletions snippets/fsharp/System/String/Intern/string_intern.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ module string_intern.fs
open System
open System.Text

let s1 = "MyTest"
let s1 = StringBuilder().Append("My").Append("Test").ToString()
let s2 = StringBuilder().Append("My").Append("Test").ToString()
let s3 = String.Intern s2
printfn $"s1 = {s1}"
printfn $"s2 = {s2}"
printfn $"s3 = {s3}"
printfn $"Is s2 the same reference as s1?: {s2 :> obj = s1 :> obj}"
printfn $"Is s3 the same reference as s1?: {s3 :> obj = s1 :> obj}"
printfn $"Are s1 and s2 equal in value? {s1 = s2}"
printfn $"Are s1 and s2 the same reference? {Object.ReferenceEquals(s1, s2)}"

let i1 = String.Intern s1
let i2 = String.Intern s2
printfn "After interning:"
printfn $" Are i1 and i2 equal in value? {i1 = i2}"
printfn $" Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}"
(*
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
Are s1 and s2 equal in value? True
Are s1 and s2 the same reference? False
After interning:
Are i1 and i2 equal in value? True
Are i1 and i2 the same reference? True
*)
//</snippet1>
48 changes: 18 additions & 30 deletions snippets/fsharp/System/String/IsInterned/isin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,29 @@ module isin.fs
// Sample for String.IsInterned(String)
open System
open System.Text
open 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)>]
do ()
// Constructed strings are not automatically interned.
let s1 = StringBuilder().Append("My").Append("Test").ToString()
let s2 = StringBuilder().Append("My").Append("Test").ToString()

let test sequence str =
printf $"%d{sequence}) The string, '"
let strInterned = String.IsInterned str
if isNull strInterned then
printfn $"{str}', is not interned."
else
printfn $"{strInterned}', is interned."
// Neither string is in the intern pool yet.
printfn $"Is s1 interned? {String.IsInterned(s1) <> null}"
printfn $"Is s2 interned? {String.IsInterned(s2) <> null}"

// String str1 is known at compile time, and is automatically interned.
let str1 = "abcd"
// Intern s1 explicitly.
let i1 = String.Intern(s1)

// Constructed string, str2, is not explicitly or automatically interned.
let str2 = StringBuilder().Append("wx").Append("yz").ToString()
printfn ""
test 1 str1
test 2 str2
// Now s2 can be found in the intern pool.
let i2 = String.IsInterned(s2)

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

//This example produces the following results:

//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.
// This example produces the following results:
//
// Is s1 interned? False
// Is s2 interned? False
// Is s2 interned after interning s1? True
// Are i1 and i2 the same reference? True

//</snippet1>
22 changes: 14 additions & 8 deletions snippets/visualbasic/System/String/Intern/string_intern.vb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ Imports System.Text
Class Sample

Public Shared Sub Main()
Dim s1 As String = "MyTest"
Dim s1 As String = New StringBuilder().Append("My").Append("Test").ToString()
Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()
Dim s3 As String = String.Intern(s2)
Console.WriteLine($"s1 = {s1}")
Console.WriteLine($"s2 = {s2}")
Console.WriteLine($"s3 = {s3}")
Console.WriteLine($"Is s2 the same reference as s1?: {s2 Is s1}")
Console.WriteLine($"Is s3 the same reference as s1?: {s3 Is s1}")
Console.WriteLine($"Are s1 and s2 equal in value? {s1 = s2}")
Console.WriteLine($"Are s1 and s2 the same reference? {s1 Is s2}")

Dim i1 As String = String.Intern(s1)
Dim i2 As String = String.Intern(s2)
Console.WriteLine("After interning:")
Console.WriteLine($" Are i1 and i2 equal in value? {i1 = i2}")
Console.WriteLine($" Are i1 and i2 the same reference? {i1 Is i2}")
End Sub
End Class
'
's1 = MyTest
's2 = MyTest
's3 = MyTest
'Is s2 the same reference as s1?: False
'Is s3 the same reference as s1?: True
'Are s1 and s2 equal in value? True
'Are s1 and s2 the same reference? False
'After interning:
' Are i1 and i2 equal in value? True
' Are i1 and i2 the same reference? True
'
'</snippet1>
7 changes: 7 additions & 0 deletions snippets/visualbasic/System/String/IsInterned/Program.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Public Class Program
Public Shared Sub Main()
IsInExample.Run()
IsInternedExample.Run()
End Sub

End Class
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
65 changes: 28 additions & 37 deletions snippets/visualbasic/System/String/IsInterned/isin.vb
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
'<snippet1>
' Sample for String.IsInterned(String)
' Sample for String.IsInterned(String)
Imports System.Text
Imports 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 Shared Sub Main()
' String str1 is known at compile time, and is automatically interned.
Dim str1 As [String] = "abcd"

' Constructed string, str2, is not explicitly or automatically interned.
Dim str2 As [String] = New StringBuilder().Append("wx").Append("yz").ToString()
Console.WriteLine()
Test(1, str1)
Test(2, str2)
End Sub

Public Shared Sub Test(ByVal sequence As Integer, ByVal str As [String])
Console.Write("{0}) The string, '", sequence)
Dim strInterned As [String] = [String].IsInterned(str)
If strInterned Is Nothing Then
Console.WriteLine("{0}', is not interned.", str)
Else
Console.WriteLine("{0}', is interned.", strInterned)
End If
End Sub
End Class
Module IsInExample
Public Sub Run()
'<snippet1>

' Constructed strings are not automatically interned.
Dim s1 As String = New StringBuilder().Append("My").Append("Test").ToString()
Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()

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

'1) The string, 'abcd', is interned.
'2) The string, 'wxyz', is not interned.
' Intern s1 explicitly.
Dim i1 As String = String.Intern(s1)

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

'1) The string, 'abcd', is not interned.
'2) The string, 'wxyz', is not interned.
'</snippet1>
Console.WriteLine($"Is s2 interned after interning s1? {i2 IsNot Nothing}")
Console.WriteLine($"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}")

' This example produces the following results:
'
' Is s1 interned? False
' Is s2 interned? False
' Is s2 interned after interning s1? True
' Are i1 and i2 the same reference? True

'</snippet1>
End Sub
End Module
54 changes: 27 additions & 27 deletions snippets/visualbasic/System/String/IsInterned/isinternedex1.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
' Uses IsInterned to determine whether a string is interned (True) or not (False).
Option Strict On

' <Snippet1>
Module Example
Public Sub Main()
Dim str1 As String = "a"
Dim str2 As String = str1 + "b"
Dim str3 As String = str2 + "c"
Dim strings() As String = { "value", "part1" + "_" + "part2", str3,
String.Empty, Nothing }
For Each value In strings
If value Is Nothing Then Continue For

Dim interned As Boolean = (String.IsInterned(value) IsNot Nothing)
If interned Then
Console.WriteLine("'{0}' is in the string intern pool.",
value)
Else
Console.WriteLine("'{0}' is not in the string intern pool.",
value)
End If
Next
End Sub
Module IsInternedExample
Public Sub Run()
' <Snippet1>

Dim str1 As String = "a"
Dim str2 As String = str1 + "b"
Dim str3 As String = str2 + "c"
Dim strings() As String = {"value", "part1" + "_" + "part2", str3,
String.Empty, Nothing}
For Each value In strings
If value Is Nothing Then Continue For

Dim interned As Boolean = (String.IsInterned(value) IsNot Nothing)
If interned Then
Console.WriteLine($"'{value}' is in the string intern pool.")
Else
Console.WriteLine($"'{value}' is not in the string intern pool.")
End If
Next

' The example displays the following output:
' '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.
' </Snippet1>
End Sub
End Module
' The example displays the following output:
' '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.
' </Snippet1>
Loading
Loading