Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
uri
communitycreator

How to check if two URIs are equal in C#

Theodore Kelechukwu Onyejiaku

Overview

URI stands for uniform resource identifier. It is a compact representation of a resource available to our application on the internet. It can link us to an image, a web page, a pdf file, and so on.

In C#, we have this class known as the Uri, which we can use to create a URI. We can also check if two URIs are equal using the equality operator == .

Syntax

Ur1 == Uri2
The syntax to check if two URIs are equal in C#

Parameters

Uri1 and Uri2: These are the URIs we want to check for equality.

Return value

The value returned is a Boolean.

Example

using System;
class HelloWorld
{
    static void Main()
    {
        // Create some Uri objects
        Uri 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 equal
        Console.WriteLine(uri1 == uri2); // True
        Console.WriteLine(uri1 == uri3); // False
        Console.WriteLine(uri1 == uri4); // False
    }
}
Check if two URIs are equal in C#

Explanation

  • Lines 7-10: We create some URI objects.
  • Lines 13-15: We check if some of the objects are equal using the == operator. We then print the result to the console screen.

RELATED TAGS

c#
uri
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring