C# .Equals()
Published Mar 18, 2023Updated Oct 12, 2023
Contribute to Docs
The .Equals() method determines whether two string objects have the same value or not.
Syntax
String.Equals(string1, string2)
string1.Equals(string2)
.Equals() is a method of class String. This method will take two parameters in the form of string objects and check their values for equality.
This method returns a boolean value of true or false.
Example
The following example demonstrates the .Equals() method. Two parameters are passed to the method and the return value is printed with the Console.WriteLine() method.
using System;class EqualsMethod {public static void Main(string[] args){string s1 = "Codecademy";string s2 = "Codecademy";Console.WriteLine(s1.Equals(s2));}}
This example results in the following output:
True
Codebyte Example
In this executable example, the string variables can be modified. The output will be the result of the .Equals() method:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours