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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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";  
          
       // retrive the system reference
       // of strA string by
       // using Intern() method
       string strB = string.Intern(strA);  
          
       // Display the strings
       Console.WriteLine(strA);  
       Console.WriteLine(strB);  
    }    
}    

chevron_right


Output:

This is C# tutorial
This is C# tutorial

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.