What is the Uri.IsBaseOf() method in C#?
Overview
We can use the Uri.IsBaseOf() method of the Uri class to check if a particular Uri is the base of another Uri.
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 objectsUri 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 UrisConsole.WriteLine(uri1.IsBaseOf(uri2));Console.WriteLine(uri4.IsBaseOf(uri5));Console.WriteLine(uri4.IsBaseOf(uri3));Console.WriteLine(uri3.IsBaseOf(uri1));}}
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 anArgumentNullExceptionerror when the Uri parameter passed to it is null.
Example
using Systclass HelloWorld{static void Main(){// Create some Uri objectsUri uri1 = new Uri("https://www.educative.io/");Uri uri2 = null;// check for base UrisConsole.WriteLine(uri1.IsBaseOf(uri2));}}
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.