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:
// 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); } } |
This is C# tutorial This is C# tutorial
Example 2:
// 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); } } |
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:
Recommended Posts:
- 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.Contains() Method
- C# | String vs StringBuilder
- C# | String Properties
- C# | String class
- C# | Add a string to the end of the StringCollection
- C# | How to get the HashCode for the string
- C# | String Operators
- C# | How to get TypeCode for the class String
- C# | Verbatim String Literal - @
- C# | Check if two String objects have the same value | Set-1
- C# | Check if the specified string is in the StringCollection
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.



