Tango-Charlie

Hi guys,
I have a small question on .NET and I going nuts over it.....Its regarding reference comparition.....Sounds simple but somehow I am not able to understan it...I was learning Strings in .NET and tested this code....But instead of understaning it more...I got more confused...Here it goes...


sing System;
using System.Collections.Generic;
using System.Text;

namespace StringOperationApp
{
class Program
{
static void Main(string[] args)
{

String name1 = "Hello";
String name2 = "Hello";


if(name1 == name2)
{
// Console.WriteLine("String1 {0} = String2 {1}", name1, name2);
Console.WriteLine("String are equal..");

}

Point p1 = new Point(10, 1);
Point p2 =new Point(10,1);
if (p1 == p2)
{
Console.WriteLine("Equal...Strang .NET world...Thanks...");
}




}
}

class Point
{
int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}

}
}

Output:
Strings are equal


Well, in the first compartion, I thought it will check for the reference equality, but surprisingly it checks for the nornal string compariton.....Then I tested it for user defined type Point....But now when I compared two referencee, it checks for the reference equality.....I thought .NET/CLR treates Strings in a special way and will have implemented == operator but when i chekced the docs, it does not have == operator implemented for String type....
Whats going on here....








Re: Visual C# Language SOS call.....String reference comparition...

boban.s

It's because strings are reference types but behave like value types. So even strings are reference types, when comparing two string variables, compare is done on strings values and not on strings references.






Re: Visual C# Language SOS call.....String reference comparition...

Tango-Charlie

Interesting...!!!
I am wondering how come they have not written any such thing in the SDK or MSDN docs for String type





Re: Visual C# Language SOS call.....String reference comparition...

Sean Fowler

It's weirder even than that!

Strings are interned, which means that behind the scenes both of your "Hello" string references point to the same string object.

And for more weirdness check out this.

Sean






Re: Visual C# Language SOS call.....String reference comparition...

Tango-Charlie

Hi Sean,
I did check the link that u have supplied here....The first link tells that it does implement == operator calling Equals method. So it checks for the value equality when we call == operator on string references.....
Then I again went to my MSDN and tried to check the docs for System.String type to make sure that I have not missed to see it, but I still could not find it.....
Then I opened my MSDN for version 1.1 and tried to check the same documentation and guess what....There they have specified that String does implement == and != operator which check the value equality as well....!!!!
Strange Microsoft world....!!!
Thats all, but thanks alot for responding to my question, I thought my question went into the "black hole" called Microsoft...!!!




Re: Visual C# Language SOS call.....String reference comparition...

micvos

It's not just microsoft. Java does the same thing with strings.




Re: Visual C# Language SOS call.....String reference comparition...

Tango-Charlie

Yeah...Now wonder...After all most of the things are copied over...!!!
I was wondering because I did not see it in the documentation of MSDN for System.String type...while its there in the previous documentation ...(i.e .NET 1.1)
But thanks for your answer...