Difference between Equality Operator (==) and Equals() Method in C# ?

1/12/2020 5:05:16 AM

Equals method is use to when compare the object reference because object location will be not same. Then we use the equals method and when we compare the only content then we use “==” symbol because this symbol use to compare the only content. On the other hand, string is class but it’s behaver different. String compare allows and allows compare as a content. Now given bellow the example and explain the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using testForClass1;
namespace testFor
    public class Program 
        static void Main(string[] args)
            //same content but locetion are defarent.
            object obj = "Farhan Sakib Jesy";
            object obj1 = new string( "Farhan Sakib Jesy".ToCharArray());
            //String compare allows allows and allows compare as a content.
            string str = "Farhan Sakib Jesy";
            string str1 = "Farhan Sakib Jesy";
            Console.WriteLine(str == str1);
            //same content but data type are defarent.
            int ints = 100;
            byte ints1 = 100;
            Console.WriteLine(ints == ints1);

We can see the first compare the object which is different location so Equals method is work fine but “==” symbol is not work. Now same content but data type is deferent are work fine. String is class but it’s behaved are different.