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.
Below are the examples mentioned:
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.
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.
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.
The above is the detailed content of C# intern(). For more information, please follow other related articles on the PHP Chinese website!