C# | How to retrieve a reference to a specified String

String.IsInterned(String) Method method is used to retrieve a reference to a specified String. This method looks up the specified string in the intern pool.
If the specified string has already been interned, a reference to that instance is returned. Otherwise, null is returned. Here the intern pool is a table that contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of String you add programmatically by calling the Intern method.

Note: The main difference between String.Intern(String) Method and String.IsInterned(String) Method is that Intern method returns a reference if the string doesn’t exist by adding a reference of the specified string to the intern pool. But the IsInterned method returns null if the string doesn’t exist.

Syntax:



public static string IsInterned (string strA);

Here, strA is a string which is used to search for in the intern pool.

Return Value: The return type of this method is System.String. This method will return a reference to strA if it is present in the CLR(Common Language Runtime) intern pool. Otherwise, return null.

Exception: This method will give ArgumentNullException of 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 check if the given 
// string is in intern pool or not
using System;
  
class GFG {
      
    // main method
    public static void Main()
    {
        // strings
        string str1 = "Monday";
        string str2 = str1 + "-day";
        string[] pool = {"Tuesday", "Wednesday",
                         "Thursday", str2};
                           
        // checking in intern pool
        foreach(var item in pool)
        {
  
            // retrive reference of strings
            bool value = String.IsInterned(item) != null;
  
            if (value)
  
                // if the string in intern pool then print
                Console.WriteLine("{0}: Yes", item);
                  
            else
              
                // if string in not intern pool then print
                Console.WriteLine("{0}: No", item);
        }
    }
}

chevron_right


Output:

Tuesday: Yes
Wednesday: Yes
Thursday: Yes
Monday-day: No

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if the given 
// string is in intern pool or not
using System;
  
class GFG {
      
    // main method
    public static void Main()
    {
        // strings
        string str1 = "Geeks";
        string str2 = str1 + "-day";
          
        // this will give error as 
        // null is present in pool
        string[] pool = {"GFG", "DSA",
                         "C#", null};
                           
        // checking in intern pool
        foreach(var item in pool)
        {
  
            // retrive reference of strings
            bool value = String.IsInterned(item) != null;
  
            if (value)
  
                // if the string in intern pool then print
                Console.WriteLine("{0}: Yes", item);
                  
            else
              
                // if string in not intern pool then print
                Console.WriteLine("{0}: No", item);
        }
    }
}

chevron_right


Runtime Error:

Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: str

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.