c# interview question :- Difference between == VS .Equals()

29
1



For more such videos visit
For more such videos subscribe

See our other Step by Step video series below :-
Learn C# Step by Step
Learn Design Pattern Step by Step:-
Learn Angular tutorial step by step
Learn MVC Core step by step :-
Learn Azure Step by Step :-
Learn SharePoint Step by Step in 8 hours:-
Python Tutorial for Beginners:-
Learn Data Science in 1 hour :-
Learn Power BI Step by Step:-
Learn MSBI Step by Step in 32 hours:-
Learn SQL Server Step by Step
Learn Tableau step by step :-

What is the Difference between == VS .Equals() ?. This is a interesting c# interview questions which is making rounds in the .NET interviews.

“==” compares if the object references are same while “.Equals()” compares if the contents are same.

We are also distributing a 200 page Ebook “.NET Interview Question and Answers”. If you want this ebook please share this video in your facebook/twitter/linkedin account and email us on
questpond@questpond.com with the shared link and we will email you the PDF.

Nguồn:https://wijstaanvooronzegrondrechten.org/

Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe

29 COMMENTS

  1. Obvious caveat to your insistence on using the word "always" :
    * "==" only definitively compares reference equivalence if the operator for the particular object(s) haven't been overridden.
    * ".Equals()" only definitively compares the contents if the .Equals() method for the given Class(es) haven't been overridden.
    Furthermore, how can "==" perform a reference evaluation when / if one or both sides of the equation are constants as constants cannot possibly share a reference with an object, so what happens then?

  2. I would say that == actually is an operator which can be overwritten. String type overwrites == operator therefore it is equivalent to Equal()

  3. Great Video.

    I have been coding for years now, and consider myself good, but have been trying to discipline myself more, understanding the basic structure of C# better and how to properly utilize it.

    This has been one that has bugged me, and your video finally cleared it up for me. It all makes sense now.

    The way I see it, if I am doing a string comparison, do .Equals. This way, if I were to change the type of the object down the line, I wouldn't have to change any comparisons, and would just have to make sure the .Equals logic for the new type does as I expect (I rarely work in type object)

  4. string datatype does not always compare content. It is comparing reference when you use == operator. The reason it is returning true resides in the concept of string pooling.
    If you are creating another string with the same content then there is no second object being created ; it will first check if there is any string with the same content; if yes it will return the address of that object (i.e. both references will be pointing to same object on the heap). String pooling is the reason behind making string immutable.

  5. This does not make sense. So I made a simple class human with property name and age. Then I wrote this test. Its returning False and False. I was thinking it would be false and true just like at 3 min mark.

    Human h1 = new Human() { name = "Tom", age = 10 };
                Human h2 = new Human() { name = "Tom", age = 10 };
                //Human h2 = h1;

                Console.WriteLine(h1 == h2);
                Console.WriteLine(h1.Equals(h2));

     public class Human
        {
            public string name { get; set; }
            public int age { get; set; }
        }

  6. When i wrote
    char o1='a';
    int o2='a';
    Console.WriteLine(o1 == o2);
     Console.WriteLine(o1.Equals(o2));

    Equals() is returning true.I understand the Equals(),but why == is returning true.

  7. Thank you. Great videos!

    question:
    in the following code there is TRUE for both.
    How come the object reference is the same?

                    object o1 = "text";
                    object o2 = "text";

                    Console.WriteLine(o1 == o2);        // TRUE
                    Console.WriteLine(o1.Equals(o2));   // TRUE

  8. questions like this, sometimes can confuse on the job interview even if you are with 3+ year experience, that means that we have to honing our skills each day

  9. At the end of the day i have learned something 😛
    Always always always always and yes always we should learn something new from this nice teacher 🙂

  10. The correct answer is because in .NET strings are immutable. That means that, once created, the content of a string cannot be altered. This restriction is imposed by .NET to facilitate a more efficient manipulation of the strings. One consequence of this is that all string objects with the same content will have the same reference. In the video above, the two different strings created have the same content & will point to the same reference. When compared for reference, you'll get TRUE.

LEAVE A REPLY

Please enter your comment!
Please enter your name here