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.
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:
This is C# tutorial This is C# tutorial
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
- C# | How to retrieve a reference to a specified String
- C# | Equals(String, String) Method
- C# | How to copy a String into another String
- C# | String
- C# | String class
- C# | String.Contains() Method
- C# | Add a string to the end of the StringCollection
- C# | String Properties
- C# | String vs StringBuilder
- C# | How to get the HashCode for the string
- C# | String Operators
- C# | String Concat with examples | Set-3
- C# | Check if the specified string is in the StringCollection
- C# | String Concat with examples | Set-2
- C# | String Concat with examples | Set-1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.