String Intern() - C# Tutorial
C# Course / Strings / String Intern()

String Intern()

BLUF: Mastering String Intern() is essential for building robust applications with the .NET ecosystem. This tutorial provides clear explanations and practical examples to help you understand and apply this C# concept.
Enterprise Development Tip: String Intern()

C# is a powerful, modern language for enterprise solutions. Discover how String Intern() enhances your development workflow in the guide below.

The C# Intern function is employed to acquire a reference to the designated String. It searches within the intern pool (a memory section) for a string that matches the specified String. In case a matching string is found, the reference to it in the intern pool is provided. If the string is not found, the reference to the designated String is added to the intern pool, followed by returning that reference.

Signature

The signature of intern method is given below:

Example

public static string Intern(String str)

Parameters

str: it is a parameter of type string.

C# String Intern Method Example

Example

using System; 
		
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
           string s1 = "Hello C#";
           string s2 = string.Intern(s1);  
           Console.WriteLine(s1);
           Console.WriteLine(s2);
        }  
    }

Output:

Output

Hello C#
Hello C#

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience