📜  C#中的Uri.Equality()运算符与示例

📅  最后修改于: 2021-05-29 16:03:40             🧑  作者: Mango

Uri.Equality()运算符用于比较两个Uri对象。如果两个Uri对象包含相同的Uri,则它返回true,否则返回false

句法:

public static bool operator == (Uri uri1, Uri uri2);

参数:此方法具有以下参数:

返回值:该方法返回布尔值。如果Uri实例等效,则返回true,否则返回false。

下面的程序说明了Uri.Equality()运算符的用法:

范例1:

C#
// C# program to demonstrate the 
// Uri.Equality() Operator
  
using System;  
    
class GFG { 
    
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing address1 
        Uri address1 = new Uri("http://www.abcs.com/index.htm#search"); 
    
        // Declaring and intializing address2 
        Uri address2 = new Uri("http://www.abcd.com/index.htm"); 
    
        // Comparing both instance 
        // using Uri.Equality() Operator
        if (address1 == address2)
            Console.WriteLine("address1 is Equals to address2"); 
        else
            Console.WriteLine("address1 is not Equals to address2"); 
    } 
}


C#
// C# program to demonstrate the 
// Uri.Equality() Operator
  
using System;  
    
class GFG { 
      
    // defining get_equality() method 
    public static void get_equality(Uri address1, 
                           Uri address2) 
    { 
    
        // Comparing both instance 
        // using Uri.Equality() Operator
        if (address1 == address2) 
            Console.WriteLine("{0} is Equals to {1}", address1, address2); 
        else
            Console.WriteLine("{0} is not Equals to {1}", address1, address2); 
    } 
    
    // Main Method 
    public static void Main() 
    { 
        // calling get_equality() method 
        get_equality(new Uri("http://www.abcd.com"), 
            new Uri("http://www.abcd.com")); 
    
        get_equality(new Uri("http://www.google.com"),  
            new Uri("http://www.facebook.com"));  
    } 
}


输出
address1 is not Equals to address2

范例2:

C#

// C# program to demonstrate the 
// Uri.Equality() Operator
  
using System;  
    
class GFG { 
      
    // defining get_equality() method 
    public static void get_equality(Uri address1, 
                           Uri address2) 
    { 
    
        // Comparing both instance 
        // using Uri.Equality() Operator
        if (address1 == address2) 
            Console.WriteLine("{0} is Equals to {1}", address1, address2); 
        else
            Console.WriteLine("{0} is not Equals to {1}", address1, address2); 
    } 
    
    // Main Method 
    public static void Main() 
    { 
        // calling get_equality() method 
        get_equality(new Uri("http://www.abcd.com"), 
            new Uri("http://www.abcd.com")); 
    
        get_equality(new Uri("http://www.google.com"),  
            new Uri("http://www.facebook.com"));  
    } 
}
输出
http://www.abcd.com/ is Equals to http://www.abcd.com/
http://www.google.com/ is not Equals to http://www.facebook.com/