The String.Intern
method in C# is used to ensure that only one instance of a particular string value exists in the application’s intern pool. The intern pool is a shared table of unique string literals used by the runtime to optimize memory usage and string comparisons.
The String.Intern
method takes a string str
as input and returns a reference to the unique instance of that string in the intern pool. If the string is already present in the intern pool, the method returns a reference to the existing instance. If the string is not present, it adds the string to the intern pool and returns a reference to the newly added instance.
Here’s an example usage of the String.Intern
method:
string str1 = "Hello"; string str2 = "Hello"; string str3 = String.Intern("Hello"); Console.WriteLine(Object.ReferenceEquals(str1, str2)); // Output: True Console.WriteLine(Object.ReferenceEquals(str1, str3)); // Output: True
In this example, the strings str1
and str2
are assigned the same string literal “Hello”. When we compare their references using Object.ReferenceEquals
, it returns True
, indicating that they refer to the same object in memory.
Similarly, we use String.Intern
to intern the string “Hello” explicitly and assign it to str3
. When we compare its reference with str1
, it also returns True
, indicating that str3
refers to the same instance of the string as str1
.
By interning strings, you can reduce memory usage by ensuring that only one instance of each unique string value exists in memory, which can be beneficial in certain scenarios.
Signature:
The signature of the String.Intern
method in C# is as follows:
public static string Intern(string str)
The method takes a single parameter str
of type string
, which is the string to be interned. It returns a string that is a reference to the interned instance of the input string.
Parameters:
The String.Intern
method in C# has the following parameter:
str
(string): The string to be interned. This is the input string that you want to ensure has only one instance in the intern pool.
The str
parameter represents the string value that you want to check or add to the intern pool. It can be any valid string in C#, including string literals or dynamically constructed strings.
Here’s an example usage of the String.Intern
method:
string str = "Hello, World!"; string internedStr = String.Intern(str);
In this example, the str
parameter is assigned the value "Hello, World!"
. The String.Intern
method is then called with str
as the argument. The return value, internedStr
, will be a reference to the interned instance of the string "Hello, World!"
in the intern pool.
C# String Intern() Method Example:
The String.Intern
method in C# is used to intern a string, ensuring that only one instance of a particular string value exists in the application’s intern pool. Here’s an example that demonstrates the usage of the String.Intern
method:
using System; class Program { static void Main() { string str1 = "Hello"; // String literal string str2 = new string("Hello".ToCharArray()); // Dynamically created string // Intern the strings string internedStr1 = String.Intern(str1); string internedStr2 = String.Intern(str2); // Compare references Console.WriteLine(Object.ReferenceEquals(str1, internedStr1)); // Output: True Console.WriteLine(Object.ReferenceEquals(str2, internedStr2)); // Output: True } }
In this example, we have two strings: str1
and str2
. str1
is a string literal, and str2
is a dynamically created string with the same value as str1
.
We use the String.Intern
method to intern both strings. String.Intern(str1)
returns a reference to the existing interned instance of "Hello"
in the intern pool. String.Intern(str2)
adds the string to the intern pool since it is not already present and returns a reference to the newly interned instance.
The Object.ReferenceEquals
method is used to compare the references of the original strings with the interned strings. In this case, both comparisons return True
, indicating that the interned strings (internedStr1
and internedStr2
) refer to the same instances as str1
and str2
, respectively.
By interning strings, you can ensure that only one instance of each unique string value exists in memory, which can be helpful for memory optimization and efficient string comparisons.