The C# IsInterned method is employed to obtain the reference of the specified string.
Intern and IsInterned methods vary in their behavior. The Intern function will intern a string if it is not already interned, while IsInterned will not do so. When a string is not interned, IsInterned will return null.
Signature
Example
public static string IsInterned(String str)
Parameter
str: it is a string type parameter.
Return
It returns a reference.
C# String IsInterned Method Example
Example
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "Hello C#";
string s2 = string.Intern(s1);
string s3 = string.IsInterned(s1);
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
}
}
Output:
Output
Hello C#
Hello C#
Hello C#
C# String Intern vs IsInterned Example
Example
using System;
public class StringExample
{
public static void Main(string[] args)
{
string a = new string(new[] {'a'});
string b = new string(new[] {'b'});
string.Intern(a); // Interns it
Console.WriteLine(string.IsInterned(a) != null);//True
string.IsInterned(b); // Doesn't intern it
Console.WriteLine(string.IsInterned(b) != null);//False
}
}
Output:
Output
True
False