
- 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
Equals(String, String) Method in C#
The Equals() method in C# is used to check whether two String objects have the same value or not.
Syntax
bool string.Equals(string s1, string s2)
Above, s1 and s2 are the strings to be compared.
Example
using System; public class Demo { public static void Main(string[] args) { string s1 = "Kevin"; string s2 = "Tom"; string s3 = s2; Console.WriteLine("String1 = "+s1); Console.WriteLine("String2 = "+s2); Console.WriteLine("Are both the strings equal? = "+s1.Equals(s2)); Console.WriteLine("Are both the strings equal? = "+s2.Equals(s3)); } }
Output
String1 = Kevin String2 = Tom Are both the strings equal? = False Are both the strings equal? = True
Example
using System; public class Demo { public static void Main(string[] args) { string s1 = "David"; string s2 = "David"; string s3 = s2; string s4 = "Tom"; string s5 = s4; Console.WriteLine("String1 = "+s1); Console.WriteLine("String2 = "+s2); Console.WriteLine("String3 = "+s3); Console.WriteLine("String4 = "+s4); Console.WriteLine("String5 = "+s5); Console.WriteLine("Is s1 and s2 equal? = "+s1.Equals(s2)); Console.WriteLine("Is s2 and s3 equal? = "+s2.Equals(s3)); Console.WriteLine("Is s3 and s4 equal? = "+s3.Equals(s4)); Console.WriteLine("Is s4 and s5 equal? = "+s4.Equals(s5)); } }
Output
String1 = David String2 = David String3 = David String4 = Tom String5 = Tom Is s1 and s2 equal? = True Is s2 and s3 equal? = True Is s3 and s4 equal? = False Is s4 and s5 equal? = True
- Related Articles
- Java String equals() method.
- Java String equals() method example.
- String compare by equals() method in Java
- Java Program to Differentiate String == operator and equals() method
- Golang Program to Differentiate String == operator and equals() method
- Explain the equals() method of the Object, String and, StringBuffer classes.
- Convert.FromBase64String(String) Method in C#
- Char.Parse(String) Method in C#
- Uri.EscapeDataString(String) Method in C#
- Uri.CheckHostName(String) Method in C#
- Uri.CheckSchemeName(String) Method in C#
- C# String CopyTo() Method
- C# Enum Equals Method
- Char.IsControl(String, Int32) Method in C#
- Convert.ToBoolean(String, IFormatProvider) Method in C#

Advertisements