I'm trying to figure out some best-practices for if-statements. When I need to switch on some equality, i.e. need an if-else construct, I usually write condition on "not-equal". The reason behind this is that usually, when a non-successful result comes, the number of instructions and also their complexity, are low. Is there any difference on the comparison process? Is there a difference in execution time between equal (==) and not equal (!=)?
Example (a rather simple one, but the general idea holds):
string myString = "weekend";
if(myString != "weekend")
{
Console.WriteLine("No fun...!");
}
else
{
//do a bunch of fun stuff with relatively high complexity
//can expand over many lines of code
}
Is there any difference in execution time if I change the order of the if-else statements?
string myString = "weekend";
if(myString == "weekend")
{
//do a bunch of fun stuff with relatively high complexity
//can expand over many lines of code
}
else
{
Console.WriteLine("No fun...!");
}