Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Uri.Equals(Object) trong C#


Phương thức Uri.Equals() trong C# so sánh hai thể hiện Uri để kiểm tra sự bằng nhau.

Cú pháp

Dưới đây là cú pháp:

public override bool Equals (object comparand);

Trong đó, tham số comparand là thể hiện Uri hoặc định danh URI để so sánh với thể hiện hiện tại.

Ví dụ 1

Hãy xem ví dụ sau để triển khai phương thức Uri.Equals():

using System;
public class Demo {
    public static void Main(){
        Uri newURI1 = new Uri("https://www.tutorialspoint.com/index.htm");
        Console.WriteLine("URI = " + newURI1);
        Uri newURI2 = new Uri("https://www.tutorialspoint.com/index.htm");
        Console.WriteLine("URI = " + newURI2);
        if(newURI1.Equals(newURI2))
            Console.WriteLine("Both the URIs are equal!");
        else
            Console.WriteLine("Both the URIs aren't equal!");
    }
}

Đầu ra

Chương trình sẽ cho ra kết quả sau:

URI = https://www.tutorialspoint.com/index.htm
URI = https://www.tutorialspoint.com/index.htm
Both the URIs aren't equal!

Ví dụ 2

Ví dụ khác triển khai phương thức Uri.Equals():

using System;
public class Demo {
    public static void Main(){
        Uri newURI1 = new Uri("https://www.tutorialspoint.com/index.htm");
        Console.WriteLine("URI = " + newURI1);
        Uri newURI2 = new Uri("https://www.tutorialspoint.com/");
        Console.WriteLine("URI = " + newURI2);
        if(newURI1.Equals(newURI2))
            Console.WriteLine("Both the URIs are equal!");
        else
            Console.WriteLine("Both the URIs aren't equal!");
    }
}

Đầu ra

Chương trình sẽ cho ra kết quả sau:

URI = https://www.tutorialspoint.com/index.htm
URI = https://www.tutorialspoint.com/
Both the URIs aren't equal!