Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Uri.Equals(Object) Method in C#
The Uri.Equals(Object) method in C# compares two Uri instances for equality. This method performs a case-sensitive comparison of the complete URI strings, including scheme, host, port, path, query, and fragment components.
Syntax
Following is the syntax −
public override bool Equals(object comparand);
Parameters
comparand − The Uri instance or object to compare with the current instance. Can be null.
Return Value
Returns true if the specified object is a Uri instance equivalent to the current Uri instance; otherwise, false. If the comparand is null or not a Uri object, the method returns false.
Using Uri.Equals() with Identical URIs
When two Uri objects contain exactly the same URI string, the Equals method returns true −
using System;
public class Demo {
public static void Main() {
Uri newURI1 = new Uri("https://www.tutorialspoint.com/index.htm");
Console.WriteLine("URI1 = " + newURI1);
Uri newURI2 = new Uri("https://www.tutorialspoint.com/index.htm");
Console.WriteLine("URI2 = " + newURI2);
if(newURI1.Equals(newURI2))
Console.WriteLine("Both the URIs are equal!");
else
Console.WriteLine("Both the URIs aren't equal!");
}
}
The output of the above code is −
URI1 = https://www.tutorialspoint.com/index.htm URI2 = https://www.tutorialspoint.com/index.htm Both the URIs are equal!
Using Uri.Equals() with Different URIs
When Uri objects contain different URI strings, the Equals method returns false −
using System;
public class Demo {
public static void Main() {
Uri newURI1 = new Uri("https://www.tutorialspoint.com/index.htm");
Console.WriteLine("URI1 = " + newURI1);
Uri newURI2 = new Uri("https://www.tutorialspoint.com/");
Console.WriteLine("URI2 = " + newURI2);
if(newURI1.Equals(newURI2))
Console.WriteLine("Both the URIs are equal!");
else
Console.WriteLine("Both the URIs aren't equal!");
}
}
The output of the above code is −
URI1 = https://www.tutorialspoint.com/index.htm URI2 = https://www.tutorialspoint.com/ Both the URIs aren't equal!
Using Uri.Equals() with Null and Non-Uri Objects
The method handles null values and non-Uri objects gracefully by returning false −
using System;
public class Demo {
public static void Main() {
Uri newURI = new Uri("https://www.tutorialspoint.com/");
Console.WriteLine("URI = " + newURI);
// Compare with null
Console.WriteLine("Equals(null): " + newURI.Equals(null));
// Compare with string (non-Uri object)
string uriString = "https://www.tutorialspoint.com/";
Console.WriteLine("Equals(string): " + newURI.Equals(uriString));
// Compare with another Uri object
Uri anotherURI = new Uri("https://www.tutorialspoint.com/");
Console.WriteLine("Equals(Uri): " + newURI.Equals(anotherURI));
}
}
The output of the above code is −
URI = https://www.tutorialspoint.com/ Equals(null): False Equals(string): False Equals(Uri): True
Conclusion
The Uri.Equals(Object) method provides a reliable way to compare Uri instances for exact equality. It performs case-sensitive string comparison of the complete URI and returns false for null values or non-Uri objects, making it safe to use in various scenarios.
