How to use the != operator to check if two URIs are equal in C#
Overview
URI stands for Uniform Resource Identifier. It is used to represent a resource on the internet. This resource may be a code snippet, an image, an API, and so on.
We can check if two URIs in C# are unequal using the inequality operator.
Syntax
Uri1 != Uri2
Parameter values
Uri1 and Uri2: These are the URIs we want to check to see if they are unequal.
Return value
The value returned is a boolean. The value true is returned if they are not equal. Otherwise, the value false is returned.
Example
using System;class HelloWorld{static void Main(){// Create some Uri objectsUri uri1 = new Uri("https://www.educative.io/");Uri uri2 = new Uri("https://www.educative.io/");Uri uri3 = new Uri("https://www.educative.io/index.html");Uri uri4 = new Uri("https://www.educative.io/edpresso");// check if they are not equalConsole.WriteLine(uri1 != uri2); // FalseConsole.WriteLine(uri1 != uri3); // TrueConsole.WriteLine(uri1 != uri4); // True}}
Explanation
- Lines 7–10: We create some URI objects.
- Lines 13–15: We check if some of the objects are not equal using the
!=operator, and then print the results to the console.