If I declared a bool isTrue = false; // init it to false

and I can get the value from a string strVal = T; // I assumed it is the TRUE value

I heard it is not a good code style to compare string in C# like

if (isTrue.tostring() == strVal) {}.

Some time, I covert the the string variable to enum then I can compare it more convenient.

Is there any good method to do it?

+1  A: 

There is no need to convert boolean values to strings in order to compare the two. You can simply compare the two boolean values directly:

if (isTrue == boolVal) {}

Update: (following updated question)

You can parse a string into a boolean and use the resulting boolean in your comparison (as above), using either bool.Parse or bool.TryParse.

Sorry for my original post above. Acturally the `boolVal` is a ` strVal` - `T`. I take is as a `bool` value `TRUE`.
Nano HE
+4  A: 

Yes, you parse the string into a boolean first.

Try this:

bool someBool = false; 

string boolVal = "true";
bool stringBool;
bool.TryParse(boolVal, out stringBool);

if (someBool == boolVal) 


Alternatively to handle 'T' and 'F' try these methods:

public bool ParseString(string maybeBool)
    return ParseString(maybeBool, false);

public bool ParseString(string maybeBool, bool def)
    bool stringBool;
    if (bool.TryParse(maybeBool, out stringBool))
        return stringBool;

    if (string.Equals(maybeBool, "T", StringComparison.OrdinalIgnoreCase))
        return true;

    if (string.Equals(maybeBool, "F", StringComparison.OrdinalIgnoreCase))
        return false;

    return def;
@all. Thank you.
Nano HE
@Graphain. I debugged and found it doesn't work when the `boolVal = "T"` not `"true"`; Then I can get `stringBool = false` after `TryParse()` How can I cover the problem with your method? Thanks.
Nano HE
No TryParse doesn't handle "T" it will return false (i.e failed to parse) in this case. Check its return value and if TryParse returns false do some custom parsing to handle "T", "F" etc... You should maybe throw an exception if boolVal still doesn't match anything.
Bear Monkey
See my update Nano HE to handle T and F
The new example will not compile because 'default' is a keyword.
Bear Monkey
@mjf196 okay so rename the variable and I'll do the same on the example, thanks
+2  A: 

Try bool.Parse() method instead.

or bool.TryParse is even better if you aren't sure that the input string is a valid bool value.
+1  A: 
bool.Parse(boolVal) == isTrue 
Al Bundy
+1  A: 

you may compare boolean type instead.

bool temp = bool.Parse(strVal);

if(isTrue == temp)

If you really want to do string comparisons:

if (string.Equals(isTrue.ToString(), strValue)) { }
it's case sensitive won't work if srtValue ="true" or "True"
add .ToLower() on both sides then! ;)
Filip Ekberg
+1  A: 

Yet another version which i use a lot is simply Convert.ToBoolean(stringFromBoolVal)