The Lounge is rated PG. If you're about to post something you wouldn't want your
kid sister to read then don't post it. No flame wars, no abusive conduct, no programming
questions and please don't post ads.
Sometimes the assumption makes code shorter but definetely not faster (optimizars take away all the fun) or elegant. In fact I stopped using them interchangeably a while ago, now I use integers as bools only when marshalling between different conventions or storing in exchangeable data structures - it allows me to fix the size of the data field without stumbling in the differences of bool type size between compilers and languages.
Don't get me wrong ... I'm not dissing on C and saying C# is all that great. I was simply pointing out why the if statement would fail to even compile in this particular case when trying this in C#.
You make a point where C#'s strong typing does make a programmer's life more cumbersome - you'd need to convert a byte array into specific types to do what you want, though there is readily available built-in libraries for that, or you could use an unsafe code block and use pointers to cast one into the other (just like you'd have done in C). Point is it would make for more coding to achieve the same thing.
Though it's only in special circumstances (like your example) where this is beneficial. Nearly everywhere else it means there's less to think about (and guard against) due to the compiler checking types for you. If you find you constantly run into situations where you need to cast between types - then perhaps C# isn't the correct tool for the job and you'd be better off with C instead.
I didn't get you wrong, neither I was dissing C# - each task has its tool for the job. For a graphical interface I would most definetely use C# (now I'm stuck to VB6 due to company decisions), and for almost any program. My job involves both hardware control and computation/memory heavy algorithms so C/C++ is the tool of excellence as of now.
the integer is not a boolean, but the "success" of the value assignment can be taken as a bool
I have had problems with that several times in my career. That's why I got used to put constants in first place.
if (6 = myInt) is going to throw an error in many more cases as the other possibility.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
Dump() is an extension method added to all objects within LinqPad. LinqPad is a tool which allows writing & compiling simple C# code snippets (plus a bunch of other things). (And the basic version is free: www.linqpad.com )
Plain C operators - it was a stupid mistake from my part, on the same day I made at least 3 mistakes of >= vs >. It wasn just the day - in fact I'm on sick leave today due to a strong headache I've been coddling since yesterday evening. Probably I was starting to feel it's effects in the morning too
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun