What is the Uri.GetHashCode() method in C#?

Overview

URI stands for uniform resource identifier. It is used to represent a resource on the internet. This resource could be a code snippet, an image, API, and so on.

We can get the hash of a URI object by using the GetHashCode() method. It returns an integer value that represents the hash of the object. A hash or hashcode is a unique value that an object has.

Syntax

Uri.GetHashCode()
Syntax for GetHashCode() method in C#

Parameters

This method does not take any parameters.

Return value

The value returned is an integer that is the hash value generated for the particular URI object.

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 not equal
Console.WriteLine(uri1.GetHashCode());
Console.WriteLine(uri2.GetHashCode());
Console.WriteLine(uri3.GetHashCode());
Console.WriteLine(uri4.GetHashCode());
}
}

Explanation

  • Line 7–10: We create some URI objects.
  • Line 13–16: We get the hash values of the URI objects we created and print the values to the console.

Free Resources