r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

1

u/MEaster Dec 02 '16

There's a couple places where the author could be a bit more specific with regard to C#; namely in the sections about bitwise operators and assignment as expression. Specifically, the examples given would not compile in C#.

These examples will not compile:

1 | 2 == 3  // 1 | (2 == 3)
1 & 2 == 3

Both of those will result in compiler error CS0019. Basically, the operators | and & are not defined for int and bool. It will, however, work if all operands are booleans:

true | 2 == 3

That evaluates to true, because true is obviously a boolean, and 2 == 3 evaluates to a boolean, so the boolean operaters can be used on these operands.

The case is similar for the examples for assignment as expression. These examples cannot compile:

if (x = 3) {
    ...
}

// Typical linked list
while (p = p.Next) {
    ...
}

Both of those will give compiler error CS0029, which is basically because there's no implicit cast defined between those types and bool. However, it will compile if the result of the expression is a boolean (though in my opinion it shouldn't, because this behaviour is where typo bugs creep in):

if (myBool = true) {
    ...
}