Difference between == and .Equals method in c# (2024)

Table of Contents
Example Output Example Output

The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string.

The Equals() method compares only content.

Example

Live Demo

using System;namespace ComparisionExample { class Program { static void Main(string[] args) { string str = "hello"; string str2 = str; Console.WriteLine("Using Equality operator: {0}", str == str2); Console.WriteLine("Using equals() method: {0}", str.Equals(str2)); Console.ReadKey(); } }}

Output

Using Equality operator: TrueUsing equals() method: True

The Equality operator is used to compare the reference identity.

Let us see another example.

Example

Live Demo

using System;namespace Demo { class Program { static void Main(string[] args) { object str = "hello"; char[] values = {'h','e','l','l','o'}; object str2 = new string(values); Console.WriteLine("Using Equality operator: {0}", str == str2); Console.WriteLine("Using equals() method: {0}", str.Equals(str2)); Console.ReadKey(); } }}

Output

Using Equality operator: FalseUsing equals() method: True

Difference between == and .Equals method in c# (30)

karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started

Difference between == and .Equals method in c# (31)

Advertisem*nts

';adpushup.triggerAd(ad_id); });

Difference between == and .Equals method in c# (2024)
Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 6201

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.