The string is an array of characters. The String class represents the text as a series of Unicode characters and it is defined in the .NET base class library. The main use of the String class is to provide the properties, operators and methods so that it becomes easy to work with strings.
There are two types of operators present in the String class:
- Equality(String, String) Operator
- Inequality(String, String) Operator
Equality(String, String) Operator
This operator is used to check whether the given string contains the same value or not. It returns true if both the string are equal. Otherwise, return false.
Syntax:
public static bool operator == ( string x, string y );
Parameters:
string x: String x is the first string to compare.
string y: String y is the second string to compare.
Return value: The return type of this operator is System.Boolean. It returns true if string x is equal to string y, otherwise return false.
Example 1:
CSharp
// C# program to illustrate the
// Equality operator
using System;
class GFG {
// Main Method
public static void Main(string[] args)
{
// variables
string s1 = "WelcomeToGeeks";
string s2 = "WelcomeToGeeks";
string s3 = "Geeksforgeeks";
bool result1, result2;
// Equality operator return true
// as both strings are equal
result1 = s1 == s2;
Console.WriteLine("s1 is equal to s2: {0} ", result1);
// Equality operator return false
// as both strings are not equal
result2 = s1 == s3;
Console.WriteLine("s1 is equal to s3: {0} ", result2);
}
}
Output:
s1 is equal to s2: True
s1 is equal to s3: False
Example 2:
CSharp
// C# program to illustrate
// the Equality operator
using System;
class GFG {
// Main method
public static void Main()
{
// function calling
Check("GEEKS");
Check("geeks");
Check("GEEKS");
}
// Function to check the
// string for equality
static void Check(String value)
{
// string str
String str = "geeks";
// Display the comparison between strings
Console.WriteLine("String 1: {0}", str);
Console.WriteLine("String 2: {0}", value);
Console.WriteLine("Comparison of string 1 and string 2: {0}",
str == value);
}
}
Output:
String 1: geeks
String 2: GEEKS
Comparison of string 1 and string 2: False
String 1: geeks
String 2: geeks
Comparison of string 1 and string 2: True
String 1: geeks
String 2: GEEKS
Comparison of string 1 and string 2: False
Inequality(string, string) Operator
This operator is used to check whether the given strings contain different values or not. It returns true if both the strings are different from each other. Otherwise, return false.
Syntax:
public static bool operator != ( string x, string y );
Parameters:
string x: String x is the first string to compare.
string y: String y is the second string to compare.
Return Value: The return type of this operator is System.Boolean. It returns true if string x is not equal to string y, otherwise return false.
Below given are some examples to understand the implementation in a better way:
Example 1:
CSharp
// C# program to illustrate the
// Inequality operator
using System;
class GFG {
// Main Method
public static void Main(string[] args)
{
// variables
string s1 = "WelcomeToGeeks";
string s2 = "WelcomeToGeeks";
string s3 = "Geeksforgeeks";
bool result1, result2;
// Inequality operator return true
// as both strings are different from each other
result1 = s1 != s3;
Console.WriteLine("s1 is different from s3: {0} ", result1);
// Inequality operator return false
// as both strings are equal
result2 = s1 != s2;
Console.WriteLine("s1 is different from s2: {0} ", result2);
}
}
Output:
s1 is different from s3: True
s1 is different from s2: False
Example 2:
CSharp
// C# program to illustrate the concept
// of Inequality operator
using System;
class GFG {
// main method
public static void Main()
{
// function calling
Check("GEEKS");
Check("geeks");
Check("GEEKS");
}
// method to check the string value
static void Check(String value)
{
// string str
String str = "geeks";
// Display the comparison between strings
Console.WriteLine("string 1: {0}", str);
Console.WriteLine("string 2: {0}", value);
Console.WriteLine("Comparison of string 1 and string 2: {0}",
str != value);
}
}
Output:
string 1: geeks
string 2: GEEKS
Comparison of string 1 and string 2: True
string 1: geeks
string 2: geeks
Comparison of string 1 and string 2: False
string 1: geeks
string 2: GEEKS
Comparison of string 1 and string 2: True
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.2#operators
Similar Reads
C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her
15+ min read
C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used
15+ min read
C# .NET Framework (Basic Architecture and Component Stack) C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Data Types Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read