The same is true in C. When multiple conditions are used for logic and operation, the decision will be executed from the left side of the expression to the right side. If any condition is false, it will not be executed later. This is clever, but if the following conditions throw an exception, it is a potential problem. Once the preceding conditions are true, the execution will continue. When an exception is thrown, the program will burst. Haha.
We can try to write a simple demo. The following code is a pitfall. I will explain the cause later, but you can understand it from an intuitive perspective. Finally, I will give the correct test method.
Copy codeThe Code is as follows: static void Main (string [] args)
{
DataSet ds = null;
If (false & ds. Tables [0]. Rows. Count> 0)
{
Console. WriteLine ("Fuck ");
}
Else
{
Console. WriteLine ("Shit ");
}
If (true & ds. Tables [0]. Rows. Count> 0)
{
Console. WriteLine ("WOW ");
}
Else
{
Console. WriteLine ("KAO ");
}
Console. ReadKey ();
}
This code is okay at first glance, and the expected result is also provided during the runtime, that is, the first statement outputs Shit and does not throw an exception (the current side is false, an exception ds will be thrown later. tables [0]. rows. count> 0), and the second statement is true, so the dataset judgment must be executed, so an exception is thrown. However, if we use reflector to decompile the Assembly, we will find that the compiler has optimized the above Code into the following form. The true and false values written in our if statement have been castrated, therefore, it does not explain the issue of if statement execution.Copy codeThe Code is as follows: private static void Main (string [] args)
{
DataSet ds = null;
Console. WriteLine ("Shit ");
If (ds. Tables [0]. Rows. Count> 0)
{
Console. WriteLine ("WOW ");
}
Else
{
Console. WriteLine ("KAO ");
}
Console. ReadKey ();
}
In fact, if you observe carefully, during the input of this Code, VS has already indicated if (false & ds. tables [0]. rows. in Count> 0), the latter is not reachable. This is the result of real-time compilation. Since real-time compilation says the code below is not reachable, it means that the code that is not reachable will be cut off during the compilation period. Therefore, the compilation result we just saw above is a natural thing.
Similarly, if you directly splice conditions such as 1 = 0, 1 = 1, the compiler will also find out. Therefore, we need to find a way of writing that will not be discovered by the compiler, so that our condition judgment code can only be executed at runtime, instead of being adjusted during compilation. For example:Copy codeThe Code is as follows: static void Main (string [] args)
{
DataSet ds = null;
Int I = 0;
Int j = 1;
If (I + j = 0 & ds. Tables [0]. Rows. Count> 0)
{
Console. WriteLine ("Fuck ");
}
Else
{
Console. WriteLine ("Shit ");
}
If (I + j = 1 & ds. Tables [0]. Rows. Count> 0)
{
Console. WriteLine ("WOW ");
}
Else
{
Console. WriteLine ("KAO ");
}
Console. ReadKey ();
}
Let's execute it again and find that this result truly satisfies our purpose. It shows the execution mechanism of C # When multiple conditions are logical and consistent:
The significance of this article is to let everyone pay attention to the possible exceptions in the conditions when writing a program. For example, we simulate String. IsNullOrEmpty ().
In the or relationship, if there is a true value, the entire expression is true. However, if you run a statement that may cause an exception before it returns true, it will pop up.
For example, if this is the case, it will pop up, because the prerequisite for determining the Length is that there must be a string:Copy codeThe Code is as follows: public static bool IsNullOrEmpty (string str)
{
If (str. Length = 0 | str = null)
{
Return true;
}
Return false;
}
This is normal:Copy codeThe Code is as follows: public static bool IsNullOrEmpty (string str)
{
If (str = null | str. Length = 0)
{
Return true;
}
Return false;
}
This is what Microsoft wrote!Copy codeThe Code is as follows: public static bool IsNullOrEmpty (string value)
{
If (value! = Null)
{
Return (value. Length = 0 );
}
Return true;
}
You can use reflector to open System. String in mscorlib ~