Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I have studied String is a reference type not a value type. Please correct me if I m wrong
But I saw a difference in behavior while doing equality check.

C#
Main()
{
	 String check1 = new String(new char[] {'s','i','d'});
            String check2 = new String(new char[] { 's', 'i', 'd' });

            if(check1 == check2)
            {
                Console.WriteLine("I m Similar");
            }

            Panda newPanda3 = new Panda("Sid");
            Panda newPanda4 = new Panda("Sid");

            if(newPanda3 == newPanda4)
            {
                Console.WriteLine("I m Similar Pandas");
            }
           
}

public class Panda
    {
        public string Name;
        public static int Population;

        public Panda(string name)
        {
            Name = name;
            Population = Population + 1;
        }
    }


In the above scenario when I check two string objects, the output is true
But the same when I check two Panda objects, the output is false.

If string is a reference type, then how could both the objects is equal. Please let me know your views.
Posted
Updated 31-Dec-14 18:00pm
v2
Comments
Sergey Alexandrovich Kryukov 31-Dec-14 23:49pm    
This is not "views". This is just a 100% fact: System.String is the referenced type with simulation of value semantic. You could easily understand it if you also read the original MSDN documentation more thoroughly.
—SA

1 solution

Yes, it is surely a reference type. You can see it if you take the object System.Type stringType = string.GetType() and inspect it.

But the implementation of this type is pretty unusual. Basically, it simulates value-type semantic. First of all, the semantic content of the string is immutable. There is no a way to modify string content. All string functions modifying content actually create a brand-new string object and return it. Besides, it is based on the interning mechanism and intern pool. Please see:
http://msdn.microsoft.com/en-us/library/system.string.intern(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.string.isinterned(v=vs.110).aspx[^],
http://en.wikipedia.org/wiki/String_interning[^].

And, finally, your question about equality is way too trivial. You can create the same effect on your own types. You need to override System.Object.Equals method, which will also require overriding of System.Object.GetHashCode (the reasons for enforcing this rule are pretty obvious, related to the use of the object in collections based on buckets, the mechanism based on hash code):
http://msdn.microsoft.com/en-us/library/system.object%28v=vs.110%29.aspx[^].

Also, you can define your custom == and != operators. This way, you can define your own equivalence criteria, not referential but semantic.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900