if ステートメントのベスト プラクティスをいくつか見つけようとしています。何らかの等価性をオンにする必要がある場合、つまり if-else 構文が必要な場合、通常は「等しくない」という条件を記述します。この背後にある理由は、通常、不成功の結果が発生した場合、命令の数とその複雑さが少ないためです。比較プロセスに違いはありますか?等しい (==) と等しくない (!=) の実行時間に違いはありますか?
例 (かなり単純なものですが、一般的な考え方は成り立ちます):
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
}
if-else ステートメントの順序を変更すると、実行時間に違いはありますか?
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...!");
}