📜  C#中的Uri.IsLoopback属性与示例

📅  最后修改于: 2021-05-29 19:50:51             🧑  作者: Mango

Uri.IsLoopback属性是Uri类的属性,用于检查指定的Uri是否引用了本地主机。

句法:

public bool IsLoopback { get; } 

返回值:该属性的返回类型为Boolean,如果此Uri引用本地主机,则返回true,否则返回false。

异常:如果此实例表示相对Uri,则此属性将提供InvalidOperationException,因为此属性仅适用于绝对URI。

范例1:

C#
// C# program to demonstrate the  
// Uri.IsLoopback property  
using System;  
using System.Globalization;  
      
class GFG {  
      
     // Main Method  
    public static void Main()  
    {  
        // Declaring and initializing value1  
        Uri  v1 = new Uri("https://www.geeksforgeeks.org/");  
      
        // using IsLoopback property  
        bool status = v1.IsLoopback;  
      
        // checking the status  
        if (status)  
            Console.WriteLine("Given Uri is a reference of localhost");  
        else
            Console.WriteLine("Given Uri is not reference of localhost");  
    }  
}


C#
// C# program to demonstrate the  
// Uri.IsLoopback property  
using System;  
using System.Globalization;  
      
class GFG {  
      
     // Main Method  
    public static void Main()  
    {  
        // Declaring and initializing value1  
        Uri  v1 = new Uri("http://localhost");  
      
        // using IsLoopback property  
        bool status = v1.IsLoopback;  
      
        // checking the status  
        if (status)  
            Console.WriteLine("Given Uri is a reference of localhost");  
        else
            Console.WriteLine("Given Uri is not reference of localhost");  
    }  
}


输出:

Given Uri is not reference of localhost

范例2:

C#

// C# program to demonstrate the  
// Uri.IsLoopback property  
using System;  
using System.Globalization;  
      
class GFG {  
      
     // Main Method  
    public static void Main()  
    {  
        // Declaring and initializing value1  
        Uri  v1 = new Uri("http://localhost");  
      
        // using IsLoopback property  
        bool status = v1.IsLoopback;  
      
        // checking the status  
        if (status)  
            Console.WriteLine("Given Uri is a reference of localhost");  
        else
            Console.WriteLine("Given Uri is not reference of localhost");  
    }  
}

输出:

Given Uri is a reference of localhost