C# 中的 Uri.Equals(Object) 方法

csharpserver side programmingprogramming更新于 2025/9/23 19:22:17

C# 中的 Uri.Equals() 方法用于比较两个 Uri 实例是否相等。

语法

以下是语法 −

public override bool Equals (object comparand);

以上参数 comparand 是要与当前实例进行比较的 Uri 实例或 URI 标识符。

示例

现在让我们看一个实现 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!");
   }
}

输出

这将产生以下输出 −

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

示例

现在让我们看另一个实现 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!");
   }
}

输出

这将产生以下输出 −

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

相关文章