C# Boolean.TryParse関数で文字列をbool型に変換する

構文
string 変数名1 ="true/false";
Boolean.TryParse(変数名1, out 変数名2);
Boolean.TryParse関数を使って、文字列をbool型に変換します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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());
}
}
}
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()); } } }
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

C#

Posted by arkgame