此方法用於返回從字節數組中指定位置的字節轉換而來的布爾值。
用法:
public static bool ToBoolean (byte[] value, int startIndex);
參數:
- 
value:它是必需的字節數組。
 startIndex:它是值中字節的索引。
返回值:如果value中startIndex處的字節不為零,則此方法返回true,否則將返回false。
異常:
- ArgumentNullException:如果值為空。
- ArgumentOutOfRangeException:如果startIndex小於零或大於值的長度減1。
以下示例程序旨在說明BitConverter.ToBoolean(Byte [],Int32)方法的用法:
示例1:
// C# program to demonstrate 
// BitConverter.ToBoolean(bytes, index)); 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        try { 
  
            // Define an array of byte values. 
            byte[] bytes = { 0, 1, 2, 4, 8, 16, 32, 64 }; 
  
            // Display the values of the myArr. 
            Console.Write("Initial Array: "); 
  
            // calling the PrintIndexAndValues() 
            // method to print 
            PrintIndexAndValues(bytes); 
  
            for (int index = 0; index < bytes.Length; index++) { 
  
                bool values = BitConverter.ToBoolean(bytes, index); 
                Console.WriteLine("index     element           bool"); 
                Console.WriteLine(" {0}         {1}                   {2}", 
                                         index, bytes[index], values); 
            } 
        } 
        catch (ArgumentNullException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
        catch (ArgumentOutOfRangeException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
  
    // Defining the method 
    // PrintIndexAndValues 
    public static void PrintIndexAndValues(byte[] myArr) 
    { 
        for (int i = 0; i < myArr.Length; i++) { 
  
            Console.Write("{0} ", myArr[i]); 
        } 
        Console.WriteLine(); 
        Console.WriteLine(); 
    } 
}
輸出:
Initial Array: 0 1 2 4 8 16 32 64 index element bool 0 0 False index element bool 1 1 True index element bool 2 2 True index element bool 3 4 True index element bool 4 8 True index element bool 5 16 True index element bool 6 32 True index element bool 7 64 True
示例2:對於ArgumentOutOfRangeException
// C# program to demonstrate 
// BitConverter.ToBoolean(bytes, index)); 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        try { 
  
            // Define an array of byte values. 
            byte[] bytes = { 0, 1, 2, 4, 8, 16, 32, 64 }; 
  
            // Display the values of the myArr. 
            Console.Write("Initial Array: "); 
  
            // calling the PrintIndexAndValues() 
            // method to print 
            PrintIndexAndValues(bytes); 
  
            Console.WriteLine("startindex in less than zero"); 
            bool values = BitConverter.ToBoolean(bytes, -1); 
        } 
        catch (ArgumentNullException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
        catch (ArgumentOutOfRangeException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
  
    // Defining the method 
    // PrintIndexAndValues 
    public static void PrintIndexAndValues(byte[] myArr) 
    { 
        for (int i = 0; i < myArr.Length; i++) { 
  
            Console.Write("{0} ", myArr[i]); 
        } 
        Console.WriteLine(); 
        Console.WriteLine(); 
    } 
}輸出:
Initial Array: 0 1 2 4 8 16 32 64 startindex in less than zero Exception Thrown: System.ArgumentOutOfRangeException
示例3:對於ArgumentNullException
// C# program to demonstrate 
// BitConverter.ToBoolean(bytes, index)); 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        try { 
  
            // Define an array of byte values. 
            byte[] bytes = null; 
  
            // Getting bool value 
            Console.WriteLine("byte array is null"); 
            bool values = BitConverter.ToBoolean(bytes, 0); 
        } 
        catch (ArgumentNullException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
        catch (ArgumentOutOfRangeException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}輸出:
byte array is null Exception Thrown: System.ArgumentNullException
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | BitConverter.ToBoolean() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
