Introduction to C# intern()
The intern() function in C# is used to locate the reference of a certain string in memory. This method searches the memory area to discover a reference to a string that matches the supplied string while looking for a matching string. If a match is found, a reference to that string is given back.
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 we need to search for the reference of a string matching the given string in the memory area, we use the Intern() method in C#.
- Each time the method is called, it searches for the reference to the string in the memory region that matches the specified string. If the specified string and the string in the memory area match, the method returns the reference.
- This method adds the given string to the memory region and returns a reference if the given string matches and does not already exist there.
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 the 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: The above program defines a class called Program. The program calls the main procedure, which specifies two string variables. The intern() method creates a new reference and returns it if the first string’s reference doesn’t already exist in the memory space. The program then utilizes the Object.ReferenceEquals method to verify whether the references of the two given strings match or not.
Example #2
C# program to demonstrate Intern() method and determine if the references to the strings are the 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";
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: The above program defines a class called Program. One variable, represented by str2, stores a string for which the reference needs to be searched in the memory area. The string str3 is a combination of the string str1 and str3. Hence the reference of the string str2 doesn’t match the reference of the str3 though both strings return the same reference. If the memory area does not contain a reference for the string, the Intern() method creates a new reference and returns it. As a result, string str3 combines string str1 and itself. Then, the Object.ReferenceEquals method verifies whether the references of the two strings match, returning false as the reference of string str2 does not match that of string str3.
Conclusion
In this tutorial, we understand the concept of the Intern() method in C# through the 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, and how it works with programming examples. You can also go through our other related articles to learn more –