📜  C#中的Uri.ReferenceEquals()方法与示例

📅  最后修改于: 2021-05-30 00:26:51             🧑  作者: Mango

Uri.ReferenceEquals()方法用于检查两个指定对象的引用。该方法本质上可以被覆盖并且是静态的。因此,如果用户要测试两个对象引用是否相等,并且不确定是否要执行Equals方法,则可以调用ReferenceEquals方法。

下面的程序说明了Uri.ReferenceEquals()方法的用法:

范例1:

C#
// C# program to demonstrate the 
// Uri.ReferenceEquals() Method 
using System; 
using System.Globalization; 
    
class GFG { 
    
     // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing value1 
        Uri  v1 = null; 
    
        // Declaring and initializing value2 
        Uri  v2 = null; 
    
        // using ReferenceEquals(Uri , 
        // Uri ) method 
        bool status = Uri.ReferenceEquals(v1, v2); 
    
        // checking the status 
        if (status) 
            Console.WriteLine("null is equal to null"); 
        else
            Console.WriteLine("null is not equal to null"); 
    } 
}


C#
// C# program to demonstrate the 
// Uri.ReferenceEquals() Method 
using System; 
using System.Globalization; 
    
class GFG { 
    
    // Main Method 
    public static void Main() 
    { 
    
        Uri  p = new Uri("http://www.geeksforgeeks.org/index.htm"); 
        Uri  q = null; 
    
        // calling get() method 
        get(p, null); 
    
        // assigning p to q 
        q = p; 
        get(p, q); 
        get(q, null); 
    } 
    
    // defining get() method 
    public static void get(Uri  v1, 
                           Uri  v2) 
    { 
    
        // using ReferenceEquals() method 
        bool status = Uri.ReferenceEquals(v1, v2); 
    
        // checking the status 
        if (status) 
            Console.WriteLine("{0} is equal to {1}", 
                                            v1, v2); 
        else
            Console.WriteLine("{0} is not equal to {1}", 
                                                v1, v2); 
    } 
}


输出:

null is equal to null

范例2:

C#

// C# program to demonstrate the 
// Uri.ReferenceEquals() Method 
using System; 
using System.Globalization; 
    
class GFG { 
    
    // Main Method 
    public static void Main() 
    { 
    
        Uri  p = new Uri("http://www.geeksforgeeks.org/index.htm"); 
        Uri  q = null; 
    
        // calling get() method 
        get(p, null); 
    
        // assigning p to q 
        q = p; 
        get(p, q); 
        get(q, null); 
    } 
    
    // defining get() method 
    public static void get(Uri  v1, 
                           Uri  v2) 
    { 
    
        // using ReferenceEquals() method 
        bool status = Uri.ReferenceEquals(v1, v2); 
    
        // checking the status 
        if (status) 
            Console.WriteLine("{0} is equal to {1}", 
                                            v1, v2); 
        else
            Console.WriteLine("{0} is not equal to {1}", 
                                                v1, v2); 
    } 
}

输出:

http://www.geeksforgeeks.org/index.htm is not equal to 
http://www.geeksforgeeks.org/index.htm is equal to http://www.geeksforgeeks.org/index.htm
http://www.geeksforgeeks.org/index.htm is not equal to 

注意:此处,将永远不会在输出中输出null。