If we assume we have this little snippet of code:

string str = "checked";
bool test1;

if (str == "checked")
{
    test1 = true;
}
else
{
    test1 = false;
}


Is it bad practice to change a simple statement like this to the following?:

bool test2 = (str == "checked");


Because they work exactly the same, and work as required, so I can't imagine how it would be. However, as a young, inexperienced programmer I am not aware of whether such a thing is frowned upon or not. Can anyone tell me, if this is NOT ok, why not?

The following test program:

using System;

public class Test
{

    public static void Main()
    {
        string str = "checked";

        bool test1;

        if (str == "checked")
        {
            test1 = true;
        }
        else
        {
            test1 = false;
        }

        bool test2 = (str == "checked");

        bool test3 = (str != "checked");

        Console.WriteLine(test1.ToString());
        Console.WriteLine(test2.ToString());
        Console.WriteLine(test3.ToString());
    }
}


Outputs:

True
True
False


Any insight etc is appreciated.


Question: Was Miss Marples lover, Captain Ainsworth, married?
Answer: No, Captain Ainsworth only appears in the 2004 ITV series and is not a character who appears in the original books. And it is indeed implied that he was married when Miss Marple had the affair with him which explains why they never married.