Introduction to C# intern()
The reference to a given string can be retrieved from the memory location in C# using a method called intern() method and whenever the method is used to search for the reference of the string matching the given string in the memory area. The method looks for the reference of the string matching the given string in the memory area and that reference is returned if the string in memory area matches the given string and if the string matching the given string does not exist in the memory area, the given string is added to the memory area and its reference is returned by this method.
Syntax
public static string Intern(String string)
Where,
string is the string whose reference needs to be searched in the memory area.
How does the intern() work in C#?
- Whenever there is a need to search for the reference of a string matching the given string in the memory area, we make use of Intern() method in C#.
- Whenever the method is used to search for the reference of the string matching the given string in the memory area, the method looks for the reference of the string matching the given string in the memory area and that reference is returned if the string in memory area matches the given string.
- Whenever the method is used to search for the reference of the string matching the given string in the memory area, the method looks for the reference of the string matching the given string in the memory area and if the string matching the given string does not exist in the memory area, the given string is added to the memory area and its reference is returned by this method.
- The string whose reference needs to be searched in the memory area is passed as a parameter to the Intern() method.
Examples to Implement C# intern()
Below are the examples mentioned:
Example #1
C# program to demonstrate Intern() method and determine if the references to the strings are same or not using ReferenceEquals method:
Code:
using System;
//a class called program is defined
public class Program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the first string
string str1 = "Welcome to C#";
//another string variable is used to store the reference of the string one using intern method
string str2 = string.Intern(str1);
Console.WriteLine("The value of the string one is: {0}",str1);
Console.WriteLine("The value of the string two after using intern method on string one is: {0}",str2);
//ReferenceEquals method is used to check if the two strings are pointing to the same reference in the memory area or not
Console.WriteLine("If the references of the two objects are equal: {0}", Object.ReferenceEquals(str1, str2));
}
}
Output:
Explanation: In the above program, a class called program is defined. Then the main method is called within which two string variables are defined, one to store a string whose reference must be searched for in the memory area and another string variable to find the reference of the first string in the memory area by using intern() method. If the reference of the first string does not exist in the memory area, a new reference is created and that is returned by Intern() method. Then Object.ReferenceEquals method is used to verify if the references of the given two strings match with each other or not.
Example #2
C# program to demonstrate Intern() method and determine if the references to the strings are same or not using ReferenceEquals method:

4.6 (13,749 ratings)
View Course
Code:
using System;
//a class called program is defined
public class Program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the first string
string str1 = "Welcome to";
string str2 = "Welcome to C#";
//another string variable is used to store the reference of the string one using intern method
string str3 = string.Intern(str1 + " C#");
Console.WriteLine("The value of the string one is: {0}",str1);
Console.WriteLine("The value of the string two is: {0}",str2);
Console.WriteLine("The value of the string three after using intern method on string one is: {0}",str3);
//ReferenceEquals method is used to check if the two strings are pointing to the same reference in the memory area or not
Console.WriteLine("If the references of the two objects are equal: {0}", Object.ReferenceEquals(str2, str3));
}
}
Output:
Explanation: In the above program, a class called program is defined. Then the main method is called within which three string variables are defined, one to store a string whose reference must be searched for in the memory area and this is represented by str2 in the program and another string variable to find the reference of the string str2 in the memory area by using intern() method and this string is represented by str3 in the program. The string str3 is a combination of the string str1 and str3. Hence the reference of the string str2 doesn’t match with the reference of the str3 though both the strings return the same reference. If the reference of the string does not exist in the memory area, a new reference is created and that is returned by Intern() method and hence the string str3 is a combination of the string str1 and the string str3 itself. Then Object.ReferenceEquals method is used to verify if the references of the given two strings match with each other or not and it returns false as the reference of the string str2 do not match with the reference of the string string3. The output is shown in the snapshot above.
Conclusion
In this tutorial, we understand the concept of Intern() method in C# through definition, syntax, and working of Intern() method through programming examples and their outputs.
Recommended Articles
This is a guide to C# intern(). Here we discuss an introduction to C# intern(), syntax, how does it work with programming examples. You can also go through our other related articles to learn more –