2
class Program
{
    private static bool _ret = true;

    static void Main()
    {
        _ret &= Method();
        Console.WriteLine(_ret);
        Console.Read();
    }

    private static bool Method()
    {
        _ret &= false;
        return true;
    }
}

We came across this issue in a larger application we are developing and was wondering if it was expected functionality? This is written in c# with Visual Studio 2010

erothvt
  • 73
  • 5

3 Answers3

2

My bet is that _ret &= Method() is translated to _ret = _ret & Method() and the _ret on the RHS is being evaluated before Method() is called.

Since _ret is true initially, it's true when it evaluates _ret in the RHS, and then _ret is changed to false in Method(), but that doesn't matter since Method() returns true and true & true = true.

This is probably compiler/environment specific... it relies on left-to-right evaluation, which you shouldn't count on.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • It'd help to know which language this is. If it's Java, then there shouldn't be any such thing as "compiler specific". – Kerrek SB Jul 22 '11 at 18:34
  • +1 for "it relies on left-to-right evaluation, which you shouldn't count on." – Dharini Chandrasekaran Jul 22 '11 at 18:34
  • @Kerrek: True, but either way, he shouldn't assume that this code will work the same when translated to e.g. C++, Python, or Ada. – Patrick87 Jul 22 '11 at 18:36
  • If this were C++, would that be a violation of reading a variable more than once before a sequence point? – Kerrek SB Jul 22 '11 at 18:38
  • 3
    In C# you can rely on left-to-right evaluation; that's in the language specification. – Eric Lippert Jul 22 '11 at 21:04
  • @Eric: Still, I think you'd agree it's not a good habit to get into. In general, relying on side-effects is bad, outside of very well-understood OOP constructs. At the very least, you should know that it's OK to expect this in C# because it's in the specification... knowledge is key here. – Patrick87 Jul 22 '11 at 21:10
  • 2
    Yes, I totally agree. *Relying on an obscure language rule about the order in which side effects are observed to happen* is bad. *Calling a method that is useful for both its value and its side effects* is bad too! – Eric Lippert Jul 22 '11 at 21:23
2

As explained by Eric Lippert in his blog post "Precedence vs Associativity vs Order",

The expression F() + G() * H() is equivalent to F() + (G() * H()), but C# does NOT evaluate G() * H() before F(). Rather, this is equivalent to:

temp1 = F();

temp2 = G();

temp3 = H();

temp4 = temp2 * temp3;

result = temp1 + temp4;

So in your case, it evaluates _ret prior to calling Method(), and the fact that _ret changes inside Method() does not affect the outer call.

See: http://blogs.msdn.com/b/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx

Adam V
  • 6,256
  • 3
  • 40
  • 52
0

Your method return shoule be like below

    private static bool Method() 
    {
        return _ret &= false; 
    } 
Rahul
  • 76,197
  • 13
  • 71
  • 125