इस सवाल की विशिष्ट जरूरतों की अनदेखी कर, और जबकि इसकी कभी नहीं एक अच्छा एक बूल को स्ट्रिंग डालने का विचार, एक तरीका कनवर्ट क्लास पर ToBoolean() विधि का उपयोग करना होगा:
public static bool ToBoolean(this string s)
{
string[] trueStrings = { "1", "y" , "yes" , "true" };
string[] falseStrings = { "0", "n", "no", "false" };
if (trueStrings.Contains(s, StringComparer.OrdinalIgnoreCase))
return true;
if (falseStrings.Contains(s, StringComparer.OrdinalIgnoreCase))
return false;
throw new InvalidCastException("only the following are supported for converting strings to boolean: "
+ string.Join(",", trueStrings)
+ " and "
+ string.Join(",", falseStrings));
}
स्रोत
2014-01-02 23:34:59
किसी भी अप्रत्याशित मान इनपुट में हो सकता है, तो TryParse (http://stackoverflow.com/questions/18329001/parse-to-boolean-or-check-string-value/ उपयोग करने के लिए पर विचार 18329085 # 18329085) –