Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression is always true in C#

Tags:

c#

.net

A simple C# code

    bool result;
    if (bool.TryParse("false", out result) && result)
    {
        Console.WriteLine(result);
    }

and

    bool result;
    if (bool.TryParse("tRue", out result) && result)
    {
        Console.WriteLine(result);
    }

Resharper says that result in Console.WriteLine(result) is always true. Why?

like image 532
Alexandre Avatar asked Oct 01 '12 18:10

Alexandre


1 Answers

It's due to the && result part - you'll only ever get into the body of the statement if result is true. How do you anticipate any way of getting in there with result being false?

like image 193
Jon Skeet Avatar answered Sep 29 '22 23:09

Jon Skeet