Lazy Evaluation not so Lazy?

旧时模样 提交于 2019-12-11 03:24:25

问题


I've always heard C# uses lazy evaluation. So for certain code like, if (true || DoExpensiveOperation() will return true without executing DoExpensiveOperation().

On an interview test I saw the following questions,

static bool WriteIfTrue(bool argument)
{
    if (argument)
    {
        Console.WriteLine("argument is true!");
    }

    return argument;
}

static void Main()
{
    // 1              0                       0                 1
    WriteIfTrue((WriteIfTrue(false) & WriteIfTrue(true)) || WriteIfTrue(true));

    // 1               1                     0                      1
    WriteIfTrue((WriteIfTrue(true) || WriteIfTrue(false)) & WriteIfTrue(true));

    //  0                 0                  0                    0
    WriteIfTrue((WriteIfTrue(false) & WriteIfTrue(true)) & WriteIfTrue(false));

    // 1                1                      0                   1
    WriteIfTrue((WriteIfTrue(true) || WriteIfTrue(false)) & WriteIfTrue(true));
}

How many times would it print "argument is true!" to the screen?

I would say 7 is the correct answer. Now if I stick into the compiler and run it, it prints it 10 times! Where did lazy evaluation all go wrong?


回答1:


I've always heard C# uses lazy evaluation.

That's far too vague a comment for me to agree with. If you'd said that the || and && operators in C# are short-circuiting, only evaluating the second operand if the overall result couldn't be determined just from the first operand, then I'd agree with it. Lazy evaluation is a more wide-ranging concept - for example, LINQ queries use lazy (or deferred) evaluation, not actually fetching any data until the result is used.

You're using the & operator, which isn't short-circuiting:

The & operator evaluates both operators regardless of the first one's value.

The && operator is short-circuiting:

The operation x && y corresponds to the operation x & y except that if x is false, y is not evaluated, because the result of the AND operation is false no matter what the value of y is.

Replace & with && everywhere in your code, and you'll see "argument is true!" printed 8 times (not 7 - count your comments again).



来源:https://stackoverflow.com/questions/13107705/lazy-evaluation-not-so-lazy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!