此方法用于将指定的逻辑值的字符串表示形式转换为其等效的布尔值。
用法:
public static bool Parse (string value);
在此,值是包含要转换的值的字符串。
返回值:如果value等于TrueString,则此方法返回true;如果value等于FalseString,则此方法返回false。
异常:
- ArgumentNullException:如果字符串值为null。
- FormatException:如果该值不等于TrueString或FalseString。
以下示例程序旨在说明Boolean.Parse(String)方法的用法:
示例1:
// C# program to demonstrate
// Boolean.Parse(String)
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// passing different values
// to the method to check
checkParse("true");
checkParse("TRUE");
checkParse("false");
checkParse("FALSE");
checkParse(bool.TrueString);
checkParse(bool.FalseString);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining checkParse method
public static void checkParse(string input)
{
// declaring bool variable
bool val;
// getting parsed value
val = bool.Parse(input);
Console.WriteLine("'{0}' parsed as {1}", input, val);
}
}
输出:
'true' parsed as True 'TRUE' parsed as True 'false' parsed as False 'FALSE' parsed as False 'True' parsed as True 'False' parsed as False
示例2:对于ArgumentNullException
// C# program to demonstrate
// Boolean.Parse(String)
// Method for ArgumentNullException
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// passing null value as a input
checkParse(null);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining checkparse method
public static void checkParse(string input)
{
// declaring bool variable
bool val;
// getting parsed value
val = bool.Parse(input);
Console.WriteLine("'{0}' parsed as {1}", input, val);
}
}
输出:
Exception Thrown: System.ArgumentNullException
示例3:对于FormatException
// C# program to demonstrate
// Boolean.Parse(String)
// Method for FormatException
using System;
class GFG {
// Main Method
public
static void Main()
{
try {
// passing truee is not equivalent
// to true value as a input
checkParse("truee");
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining checkParse method
public static void checkParse(string input)
{
// declaring bool variable
bool val;
// getting parsed value
val = bool.Parse(input);
Console.WriteLine("'{0}' parsed as {1}", input, val);
}
}
输出:
Exception Thrown: System.FormatException
注意:值参数(可以选择在空格前或后)必须包含TrueString或FalseString,否则将引发异常,并且比较为大小写敏感。
参考:
相关用法
- 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# | Boolean.Parse() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。