A mechanism, similar to JS

Source: Internet
Author: User
Tags explode reflector

We know that when the two conditions are logical and operational, either of these conditions is false, the result of the expression is false. Therefore, encountered (A and B) this expression, if a is false, B is not true or false is irrelevant, when encountering a false condition, the program will not need to go to the extra judgment of the rest of the thing The same is true in the C # language. When multiple conditions are logical and operational, the decision is executed from the left side of the expression to the right, and any one is false, and none of the following is done. This is smart, but if the following conditions throw an exception, it is a potential problem. Once the previous condition is true, the execution continues, and when the condition of the exception is thrown, the program explodes, haha.
We can write a simple demo to try. The following code is the pit Daddy, I will explain why, but we can first understand from the intuitive level, and 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 not a problem at first, and it also gives us the desired result at run time, that is, the output of shit, without throwing an exception (the current face is false, and the DS behind the exception is thrown.) Tables[0]. Rows.Count > 0 does not do), and the second statement is executed because it was true before the dataset is judged, so throws an exception. However, if you use reflector to decompile the assembly, you will find that the compiler has optimized the above code to the following form, and that the true and false of the write-dead in our if statement has been castrated, so it is not possible to describe the problem with the 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 look closely, VS will already prompt if (false && DS) in the process of entering this code. Tables[0]. Rows.Count > 0), the latter is unreachable. This is the effect of instant compilation. Since the immediate compilation says that the following code is unreachable, it means that the unreachable code will be cut off at compile time. So, the compilation results we saw just now are natural things.
Similarly, if you directly put 1 = = 0, 1 = = 1 Such conditions to spell, the compiler will also find. So we're looking for a way to not be found by the compiler, so that our conditional judgment code can only be executed at runtime, not at compile time. such as the following:
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 ();
}
We'll do it again, and find out that the result is really fulfilling our purpose, stating that when multiple conditions are logically associated with, C # 's execution mechanism:

The meaning of writing this article is to let everyone in the writing process, pay attention to the conditions may occur anomalies in the place. For example, we simulate String.IsNullOrEmpty ().
In an or relationship, as long as there is a true, the entire expression is true. But if you let the statement that could throw an exception precede the statement that returns true before it is executed, it will explode.
For example, this will explode, because the premise of judging length is to have a string:
Copy CodeThe code is as follows:
public static bool IsNullOrEmpty (String str)
{
if (str. Length = = 0 | | str = = NULL)
{
return true;
}
return false;
}

It is normal to write this:
Copy CodeThe code is as follows:
public static bool IsNullOrEmpty (String str)
{
if (str = = NULL | | str. Length = = 0)
{
return true;
}
return false;
}

That's what Microsoft wrote, the bunker!
Copy CodeThe code is as follows:
public static bool IsNullOrEmpty (string value)
{
if (value! = null)
{
return (value. Length = = 0);
}
return true;
}
The above code can be opened with reflector mscorlib in the System.String found ~ Articles you may be interested in:

A mechanism, similar to JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.