Convert String to Bool Value Or Null - CSharp System

CSharp examples for System:Boolean

Description

Convert String to Bool Value Or Null

Demo Code


using System.Text;
using System.IO;/*w  w w.  j  ava  2  s .  co  m*/
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        public static bool? BoolValueOrNull(this string value)
        {
            bool result = false;
            if (bool.TryParse(value, out result))
            {
                return result;
            }
            else
            {
                if (value.ToUpper() == "Y" || value.ToUpper() == "YES" || value.ToUpper() == "1" || value.ToUpper() == "T" || value.ToUpper() == "TRUE")
                    return true;
                else if (value.ToUpper() == "N" || value.ToUpper() == "NO" || value.ToUpper() == "0" || value.ToUpper() == "F" || value.ToUpper() == "FALSE")
                    return false;
                else
                    return null;
            }

        }
}

Related Tutorials