Practical tips on the execution of parallel conditions in C # if statements

Source: Internet
Author: User
Tags explode reflector string find
The same is true in the C # language. When multiple conditions are logically and manipulated, the decision is executed from the left side of the expression to the right, and any one that is false will not be followed. This is smart, but if the following conditions throw an exception, it is a potential problem. Once the condition is true, it will continue execution, execution to throw the exception condition, the program will explode, haha.
We can write a simple demo to try. The following code is pit Dad, then I will explain the reason, but we can first from the intuitive level of understanding, and finally I will give the correct test methods.
Copy Code code 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 appears to be fine at first, and at runtime It also gives us the desired result, that is, that the initial statement output shit, does not throw an exception (the current face is false, the exception is thrown after the DS.) Tables[0]. Rows.Count > 0 does not do), and the second statement throws an exception because it is true before the dataset is executed. However, if you decompile the assembly with reflector, you will find that the compiler has optimized the above code to the following form, and that true and false in our if statement have been castrated, so it does not explain the problem of if statement execution.
Copy Code code 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, in the process of entering this code, VS is already prompted if (False && DS). Tables[0]. Rows.Count > 0), the latter is not up to. This is the effect of Just-in-time compilation. Since Just-in-time compilation says the following code is unreachable, it means that unreachable code will be cut off at compile time. So the result of the compilation we saw just now is the natural thing.
Similarly, if you spell the terms 1 = 0, 1 = 1, the compiler will also find out. So we're looking for a way not to be found by the compiler, to let our conditional decision code execute only at run time, not at compile time. For example, the following:
Copy Code code 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 we'll see that this time the result is really satisfying our purpose, and it illustrates the execution mechanism of C # when multiple conditions are logically related:

The meaning of writing this article is to let everyone in the writing program, pay attention to the conditions may occur in the abnormal place. For example, we simulate String.IsNullOrEmpty ().
In an or relationship, the entire expression is true as long as there is one true. But if you let a statement that might throw an exception precede the statement that returns TRUE, it explodes.
For example, this would explode, because the premise of length is to have a string:
Copy Code code as follows:

public static bool IsNullOrEmpty (String str)
{
if (str. Length = = 0 | | str = NULL)
{
return true;
}
return false;
}

This is normal to write:
Copy Code code 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 Code code as follows:

public static bool IsNullOrEmpty (string value)
{
if (value!= null)
{
return (value. Length = = 0);
}
return true;
}

The above code can use reflector to open the mscorlib in the System.String find ~

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.