-5

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. thanks

Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
  • 1
    In .NET strings are interned. This means that if you have 2 different string variables with the same value, the CLR might decide to make them point to the same memory address. This allows for more efficient resource management as it would consume less memory. – Darin Dimitrov May 08 '17 at 14:53
  • 1
    @DarinDimitrov: I don't think the CLR ever decides to do that unilaterally. If it's a compile-time constant, it's guaranteed by the language. Otherwise, it will only happen if you call `Intern`. – Jon Skeet May 08 '17 at 14:56
  • 3
    Please use capital letters when referring to yourself ("I"), and at the start of sentences. Please refrain from txtspk as well, since "you" is really not too much trouble to type. I tried to repair this question, but it was overwritten in a parallel edit, and I am not fixing it again. – halfer May 08 '17 at 14:56
  • `Equals` and `==` are implementation dependent for each class (can be overridden that is). The .NET `Equals` for string first checks if the references are equal, then failing that, if the contents are equal. http://referencesource.microsoft.com/#mscorlib/system/string.cs – BurnsBA May 08 '17 at 14:57
  • @JonSkeet, good point, I stand corrected. Only compile time constants are indeed interned automatically. – Darin Dimitrov May 08 '17 at 15:09
  • @DarinDimitrov what is the meaning of `In .NET strings are interned` ? intern or internal is same ? – Monojit Sarkar May 09 '17 at 08:12

1 Answers1

2

i know that this kind of checking if (x == y) is based on value

That's false. It will use whatever implementation of equality the relevant types have decided to implement, whether that be a value comparison or a reference comparison.

Equals is also work like == operator

It's not strictly the same. The Equals method uses the actual runtime type of the first operand to determine which type's implementation to use, and the == operator uses the compile time type of both operands to determine which implementation to use. Additionally, different types can provide whatever implementation they want for either (although it's a very bad idea to ever give the two different behavior for the same type).

why because different memory is allocated for x and y ?

Since x and y are value types, and you're passing them to a method expecting object parameters, the two are both going to be boxed, and each will be boxed to a separate location, so they'll have different references. It's never meaningful to call object.ReferenceEquals when either parameter is a variable of a value type. It will always be false.

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?

The two string literals will use the same object reference as an optimization, this is called string interning.

Servy
  • 202,030
  • 26
  • 332
  • 449