Home »
C#.Net
String.Inequality (!=) operator with example in C#
C# String.Inequality operator: Here, we are going to learn about the String.Inequality (!=) operator, its usages etc.
Submitted by IncludeHelp, on March 18, 2019
C# String.Inequality operator
"!=" is a String.Inequality operator in C#, it is used to check whether two strings objects have the same values or not (returns true if strings do not have the same values).
Syntax:
public static bool operator != (string a, string b);
Parameter: It has two parameters both are strings to compare..
Return value: bool – it returns a Boolean value. If strings have the same value, it returns false, else it returns true.
Example:
Input:
string str1 = "IncludeHelp";
string str2 = "IncludeHelp";
String.Inequality:
str1 != str2;
Output:
false
C# Example to compare two strings using String.Inequality (!=) operator
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string str1 = "IncludeHelp";
string str2 = "IncludeHelp";
//comparing strings
Console.WriteLine("str1!=str2: " + (str1 != str2));
if (str1 != str2)
Console.WriteLine("str1 and str2 don't have the same values");
else
Console.WriteLine("str1 and str2 have the same values");
str1 = "Hello world";
str2 = "IncludeHelp";
//comparing strings
Console.WriteLine("str1!=str2: " + (str1 != str2));
if (str1 != str2)
Console.WriteLine("str1 and str2 don't have the same values");
else
Console.WriteLine("str1 and str2 have the same values");
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
str1!=str2: False
str1 and str2 have the same values
str1!=str2: True
str1 and str2 don't have the same values
Reference: String.Inequality(String, String) Operator