The C# Intern function is employed to acquire a reference to the designated String. It searches within the intern pool (a memory section) for a string that matches the specified String. In case a matching string is found, the reference to it in the intern pool is provided. If the string is not found, the reference to the designated String is added to the intern pool, followed by returning that reference.
Signature
The signature of intern method is given below:
public static string Intern(String str)
Parameters
str: it is a parameter of type string.
C# String Intern Method Example
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "Hello C#";
string s2 = string.Intern(s1);
Console.WriteLine(s1);
Console.WriteLine(s2);
}
}
Output:
Hello C#
Hello C#