C# Boolean.TryParse関数で文字列をbool型に変換する
構文
string 変数名1 ="true/false";
Boolean.TryParse(変数名1, out 変数名2);
Boolean.TryParse関数を使って、文字列をbool型に変換します。
使用例
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string str = "false";
Boolean res;
Boolean.TryParse(str, out res);
Console.WriteLine("文字列をbool型に変更する結果");
Console.WriteLine(res);
Console.WriteLine("データの型");
Console.WriteLine(res.GetType());
}
}
}
実行結果
文字列をbool型に変更する結果
False
データの型
System.Boolean