
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Difference between == and .Equals method in c#
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
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: True Using equals() method: True
The Equality operator is used to compare the reference identity.
Let us see another example.
Example
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: False Using equals() method: True
- Related Articles
- Difference between == and equals() method in Java.
- What is the difference between equals() method and == operator in java?
- Difference between equals() vs equalsIgnoreCase() in Java
- What is the difference between equals and compareTo in Java?
- AngularJS – equals() method
- Difference between Method Overloading and Method Overriding in Java
- Difference between Method Overriding and Method Hiding in C#
- Integer Equals() method in Java
- LocalTime equals() method in Java
- MonthDay equals() method in Java
- Instant equals() method in Java
- Period equals() method in Java
- LocalDateTime equals() method in Java
- IntBuffer equals() method in Java
- FloatBuffer equals() method in Java

Advertisements