i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here.
see my program full code
- int x = 1000;
- int y = 1000;
-
- if (x == y)
- Console.WriteLine("Value is same");
- else
- Console.WriteLine("Value is not same");
-
- if (x.Equals(y))
- Console.WriteLine("Value is same");
- else
- Console.WriteLine("Value is not same");
-
- if (object.ReferenceEquals(x,y))
- Console.WriteLine("ref is same");
- else
- Console.WriteLine("ref is not same");
-
-
- string s1 = "Hello";
- string s2 = "Hello";
-
- if (s1 == s2)
- Console.WriteLine("Value is same");
- else
- Console.WriteLine("Value is not same");
-
- if (s1.Equals(s2))
- Console.WriteLine("Value is same");
- else
- Console.WriteLine("Value is not same");
-
- if (object.ReferenceEquals(s1, s2))
- Console.WriteLine("ref is same");
- else
- Console.WriteLine("ref is not same");
i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ?
how to check the reference ?
- if (object.ReferenceEquals(x,y))
- Console.WriteLine("ref is same");
- else
- Console.WriteLine("ref is not same");
i saw in this case else portion execute........why because different memory is allocated for x and y ?
see more for string reference check
- if (object.ReferenceEquals(s1, s2))
- Console.WriteLine("ref is same");
- else
- Console.WriteLine("ref is not same");
in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same?
i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this.
one guy said string in dotnet is interned.
please discuss with a example just to clarify the meaning of string is interned in dotnet.
thanks