News for hackers. Stuff that matters.
Re:Is C# a low-level language? by jedimastert on Saturday March 02, 2019 @07:59PM (#19292100)
Slightly to the side of the topic, but this quote here:

>Yep, Bob uses i++ instead of ++i everywhere. Meh, what’s the difference.

I know what the difference is, but if you're just using it on its own line or in a for loop, why would you use `++i` instead of `i++`?

Re:Is C# a low-level language? by tokyodude on Saturday March 02, 2019 @11:35PM (#19292748)
Because you should write what you mean, not just assume the compiler will convert what you wrote into what you mean.

    i++
means

    var temp = i;
    i = i + 1;
    return temp;
where as

    ++i
means

    i = i + 1
    return i;
So if you actually mean to create a temp then use i++ , if you don't use ++i

See example of actually implementing them

https://stackoverflow.com/a/3846374

Re:Is C# a low-level language? by eMSF on Saturday March 02, 2019 @08:23PM (#19292163)
Why didn't you ask the question the other way around? :-)

For some inexplicable reason the postfix operators are usually taught and learnt first, and thus considered more natural, even though there are more objective reasons for the opposite, at least in C++ world.

Re:Is C# a low-level language? by derefr on Saturday March 02, 2019 @09:00PM (#19292278)
> For some inexplicable reason the postfix operators are usually taught and learnt first, and thus considered more natural

The (unary) postfix operators might have semantics that are more surprising than the prefix variants, but the postfix syntax itself feels a bit more natural and fitting with other syntax of the Algol language family, IMHO, when compared to the prefix syntax. I think that's what leads people to introduce it first.

You can sort of read `i++` as an "abbreviation" of `i += 1`—in both cases, you've got an operator that follows an lvalue variable, and which both reads from and mutates the memory referred to by that variable. The only difference is that one has an additional "argument"—the amount to mutate the lvalue by—while the other has a "default" argument of +1.

`++i`, on the other hand, is a surprising bit of syntax, the first time you see it. It takes (and mutates) an lvalue... on its right! None of the (few) other unary prefix operators in Algol-like languages take lvalues, so it's kind of a shock.

(Also, for newcomers to programming, unary - [numeric negation] is commonly interpreted as part of the grammar of a number literal rather than being an expression; and unary ! is commonly seen as just "part of the language" (i.e. less like & and |; more like && and ||). Unary ~ is the only one I'd expect it would occur to any new programmer as even being a "unary prefix operator" rather than just "the way the language is." And it's a pure-functional transformation of its input.)

Re:Is C# a low-level language? by paulddraper on Saturday March 02, 2019 @11:37PM (#19292756)
I too prefer prefix operators due to simpler/more intuitive semantics.

But postfix operators have simpler/more intuitive presentation.

++i and --i ressemble + +i and - -i

Whereas i++ and i-- resemble abbreviations of i += 1 and i -= 1

Re:Is C# a low-level language? by jedimastert on Saturday March 02, 2019 @10:33PM (#19292542)
> even though there are more objective reasons for the opposite, at least in C++ world.

That's my question. What are those reason?

Re:Is C# a low-level language? by isaachier on Saturday March 02, 2019 @10:02PM (#19292431)
Google style guide has me sort of leaning to always using ++i:

https://google.github.io/styleguide/cppguide.html#Preincreme...

Re:Is C# a low-level language? by saagarjha on Saturday March 02, 2019 @08:10PM (#19292124)
Habit?