Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Equating URIs

Tags:

c#

c#-4.0

What is the standard way to determine that these 2 similar Uris are actually the same?

var a = new Uri("http://sample.com/sample/");
var b = new Uri("http://sample.com/sample");
Console.WriteLine(a.Equals(b));

// False

What is the best way to determine that a == b? I could compare the sub properties of the Uri object such as Host, LocalPath, etc, but I'm wondering if there is a better way.

Edit: Thanks everybody. I basically just have to have the user enforce whether that are the same or not.

like image 269
Mark Avatar asked Feb 21 '23 10:02

Mark


1 Answers

The Uri class overrides Equals and implements the == operator in order to determine equality.

The way to test if the two Uri instances are equivalent is:

if(a == b)

In your case, they are not equivalent. The directory terminator at the end of a has specific meaning, whereas b might be a directory or a file.

like image 78
Oded Avatar answered Feb 23 '23 22:02

Oded