r/badcode • • Feb 02 '22

c# Oh hey, its more of my code.

Post image
902 Upvotes

95 comments sorted by

View all comments

Show parent comment

-3

u/[deleted] Feb 03 '22

[removed] — view removed comment

3

u/[deleted] Feb 03 '22

C# is perfectly capable of converting int to bool.

int num = 1;
Convert.ToBoolean(num);
// or
bool result = num == 1;

There's no reason that conversion should be implicit. Bool and Int32 are completely different types with no relation to each other.

-3

u/Knuffya Feb 03 '22

I've quickly tried (bool)34, which failed.

1

u/[deleted] Feb 04 '22

Obviously it did. Why should such casts be allowed?

1

u/Knuffya Feb 04 '22

Why shouldnt they?!

0

u/[deleted] Feb 04 '22

Because booleans and integers have nothing to do in common.

1

u/Knuffya Feb 04 '22

That doesn't fucking matter if i want to dump the bits of cEmployee into an array of doubles, i'll better be able to do so. That should be explicit but definitely allowed!

1

u/[deleted] Feb 04 '22

Woah man, language.

You can easily just use an equality operator, as I already pointed out, such as this:

cEmployee.SomeProperty == 1 // 1 for true

Your bad implementation doesn't mean the language is poorly designed because it doesn't suit your needs. Booleans are not numbers. Same way strings aren't numbers and you can't cast them to ints.