|
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.
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
"Go ahead, make my day"
|
|
|
|
|
Processing OSC packets (for example) is a doddle in C/C++ but somewhat cumbersome in C#
|
|
|
|
|
Makes sense.
Jeremy Falcon
|
|
|
|
|
No, it's the use of an int as a boolean that C# complains about. The assignment in the if() is fine, provided it's a boolean:
bool x = false;
if (x = true)
"It's True".Dump();
Truth,
James
|
|
|
|
|
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.
M.D.V.
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.
|
|
|
|
|
Precisely, since I'm using both in the same code (hw management) I made my Dumb Mistake Of The Day
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
"Go ahead, make my day"
|
|
|
|
|
A massive recall of all existing code is being contemplated...
Warning: "|" is also not the same as "||".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Did I sort the "have I locked my car?" effect?
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
"Go ahead, make my day"
|
|
|
|
|
OK, my turn to learn something new: What's Dump()?
|
|
|
|
|
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 )
Truth,
James
|
|
|
|
|
|
It's an extension method provided by LINQPad[^].
EDIT: I'm sure James' response wasn't there when I posted this!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm sure James' response wasn't there when I posted this!
Well, at least you got the URL right....
Truth,
James
|
|
|
|
|
Great tip and explanation. I am currently teaching myself C# and this is extremely helpful.
A giraffe is a horse designed by a committee...
... or an Agile methodology...
|
|
|
|
|
I needed the binary AND (was checking a flag register from a custom device) so it would have been a real issue if it did not compile
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
"Go ahead, make my day"
|
|
|
|
|
Depending on the correct implementation of the "&" and "&&" operators in a fat class it shouldnt be the same
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
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
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
"Go ahead, make my day"
|
|
|
|
|
Heh, I'd like to use C# but as an embedded developer (using TI DSPs) it's not really an option for me. It's C/C++ or DSP assembly (which nobody in their right mind wants to do!!!!).
|
|
|
|
|
Details! Details!
#SupportHeForShe
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
|
|
|
|
|
den2k88 wrote: refused to yield the correct expected result.
FTFY
|
|
|
|
|
"If I put in wrong data, will the result be correct?"
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
"Go ahead, make my day"
|
|
|
|
|
|
Actually, the result is the same. The side-effects aren't. At least in C#. In C, with it's enforced type-unsafety, the result can be extremey weird and the compiler won't do jack to even warn you.
|
|
|
|
|
No. Example
2 & 4 is 0
2 && is 1 (true)
In that case I needed the first form as I was checking for a flag in a flag register, but mistakenly used the second form due to, well, being the most common (althought not so much in my field, which requires the management of a lot of driverless hardware).
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
"Go ahead, make my day"
|
|
|
|