C# | Uri.Equals(Object) Method

Uri.Equals(Object) Method is used to compare two Uri instances for equality.

Syntax:
public override bool Equals (object comparand);
Here, it takes the Uri instance or a URI identifier to compare with the current instance.

Return Value: This method returns a Boolean value true if the two instances represent the same URI otherwise, false.

Below programs illustrate the use of Uri.Equals(Object) Method:

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Uri.Equals(Object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing address1
        Uri address1 = new Uri("http://www.contoso.com/index.htm#search");
  
        // Declaring and intializing address2
        Uri address2 = new Uri("http://www.contoso.com/index.htm");
  
        // Comparing both instance
        // using CheckHostName() method
        bool value = address1.Equals(address2);
  
        // Displaying the result
        if (value)
            Console.WriteLine("address1 is Equals to address2");
        else
            Console.WriteLine("address1 is not Equals to address2");
    }
}

chevron_right


Output:

address1 is Equals to address2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Uri.Equals(Object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling get() method
        get(new Uri("http://www.contoso.com"),
            new Uri("http://www.contoso.com"));
  
        get(new Uri("http://www.google.com"), 
            new Uri("http://www.facebook.com"));
    }
  
    // defining get() method
    public static void get(Uri address1,
                           Uri address2)
    {
  
        // Comparing both instance
        // using CheckHostName() method
        bool value = address1.Equals(address2);
  
        // Displaying the result
        if (value)
            Console.WriteLine("{0} is Equals to {1}", address1, address2);
        else
            Console.WriteLine("{0} is not Equals to {1}", address1, address2);
    }
}

chevron_right


Output:

http://www.contoso.com/ is Equals to http://www.contoso.com/
http://www.google.com/ is not Equals to http://www.facebook.com/

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.