Open In App

C# | How to retrieve the system’s reference to the specified String

Improve
Improve
Like Article
Like
Save
Share
Report

String.Intern(String) Method is used to retrieve the system’s reference to the specified String. This method uses the intern pool to search for a string equal to the value of the specified string. 
If such a string exists, then its reference in the intern pool is returned, or if the string doesn’t exist, a reference of the specified string is added to the intern pool, then that reference is returned. 
Here the intern pool is a table that contains a single reference to each unique literal string declared or created programmatically in your program.
Syntax: 
 

public static string Intern (string strA);

Here strA is a string to search for in the intern pool.
Return Value: The return type of this method is System.String. This method will return a system’s reference to strA if it is interned. Otherwise, a new reference to a string with the value of strA.
Exception: This method will give ArgumentNullException if the str is null.
Below given are some examples to understand the implementation in a better way: 
Example 1:
 

CSharp




// C# program to illustrate Intern() method
using System;  
            
public class GFG   
{   
     
    // main method
    public static void Main(string[] args)   
    {   
        
        // string
       string strA = "This is C# tutorial"
         
       // retrieve the system reference
       // of strA string by
       // using Intern() method
       string strB = string.Intern(strA); 
         
       // Display the strings
       Console.WriteLine(strA); 
       Console.WriteLine(strB); 
    }   
}   


Output: 

This is C# tutorial
This is C# tutorial

 

Example 2:
 

CSharp




// C# program to illustrate the
// use of Intern() Method
using System;
 
class GFG {
     
    public static void Main()
    {
 
        // strings
        string strA = "GeeksforGeeks";
        string strB = "GFG";
        string strC = "Noida";
        string strD = String.Intern(strA);
        string strE = String.Intern(strC);
 
        // Display string
        Console.WriteLine("string A == '{0}'", strA);
        Console.WriteLine("string B == '{0}'", strB);
        Console.WriteLine("string C == '{0}'", strC);
        Console.WriteLine("string D == '{0}'", strD);
        Console.WriteLine("string E == '{0}'", strE);
        Console.WriteLine();
 
        // Check the reference of strings
        Console.WriteLine("Is string A have the same reference as string B: {0}",
                                                    (Object)strA == (Object)strB);
                                                     
        Console.WriteLine("Is string B have the same reference as string C: {0}",
                                                    (Object)strB == (Object)strC);
                                                     
        Console.WriteLine("Is string D have the same reference as string E: {0}",
                                                    (Object)strD == (Object)strE);
                                                     
        Console.WriteLine("Is string A have the same reference as string D: {0}",
                                                    (Object)strA == (Object)strD);
                                                     
        Console.WriteLine("Is string E have the same reference as string C: {0}",
                                                    (Object)strE == (Object)strC);
    }
}


Output: 

string A == 'GeeksforGeeks'
string B == 'GFG'
string C == 'Noida'
string D == 'GeeksforGeeks'
string E == 'Noida'

Is string A have the same reference as string B: False
Is string B have the same reference as string C: False
Is string D have the same reference as string E: False
Is string A have the same reference as string D: True
Is string E have the same reference as string C: True

 

Reference: 
 

 



Last Updated : 27 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads