Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

uri
c#
communitycreator

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

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

We can use the Uri.IsBaseOf() method of the Uri class to check if a particular Uri is the base of another Uri. Uri uniform resource identifier is used to represent a resource on the internet. This resource can be a code snippet, an image, an API, and so on.

Syntax

uri1.IsBaseOf(uri2)
Syntax of the IsBaseOf() method in C#

Parameter values

uri1 : We want to check if uri1 is the base of uri2.

uri2: We want to check if uri1 is its base.

Return value

The Uri.IsBaseOf() method returns a boolean value. It returns true if uri1 is the base Uri of uri2. Otherwise, it returns false.

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/edpresso");
Uri uri3 = new Uri("https://www.google.com");
Uri uri4 = new Uri("https://facebook.com");
Uri uri5 = new Uri("https://facebook.com/help.html");
// check for base Uris
Console.WriteLine(uri1.IsBaseOf(uri2));
Console.WriteLine(uri4.IsBaseOf(uri5));
Console.WriteLine(uri4.IsBaseOf(uri3));
Console.WriteLine(uri3.IsBaseOf(uri1));
}
}
Check if a particular Uri is the base of another Uri

Explanation

  • Lines 7–11: We create some Uri objects.
  • Lines 14–17: We check for some base Uris using the IsBaseOf() method. Then, we print the results to the console.

Note: The Uri.IsBaseOf() method throws an ArgumentNullException error when the Uri parameter passed to it is null.

Example

using Syst
class HelloWorld
{
static void Main()
{
// Create some Uri objects
Uri uri1 = new Uri("https://www.educative.io/");
Uri uri2 = null;
// check for base Uris
Console.WriteLine(uri1.IsBaseOf(uri2));
}
}
When a null Uri is passed to the IsBaseOf() method, an ArgumentNullException error is thrown

Explanation

  • Line 7: We create a Uri that has a value.
  • Line 8: We create a Uri that has a null reference. This means that no value was given to the Uri.
  • Line 12: We check for some base Uri using the IsBaseOf() method. Then, we print the result to the console.

As can be seen in the code, an ArgumentNullException error was thrown. This error is caused when a null Uri is passed to the Uri.IsBaseOf() method.

RELATED TAGS

uri
c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring