Intern a String in CSharp

Description

The following code shows how to intern a String.

Example


  //from w ww .  j  ava  2  s. com
  
  
   

using System;

public class StringInterning
{
    public static void Main()
    {
        string s1 = "Hello";
        string s2 = "Hello";
        string s3 = "Hello".Substring(0, 4) + "o";
        
        Console.WriteLine("Str == : {0}", s1 == s2);
        Console.WriteLine("Ref == : {0}", (object) s1 == (object) s2);
        
        Console.WriteLine("Str == : {0}", s1 == s3);
        Console.WriteLine("Ref == : {0}", (object) s1 == (object) s3);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var