1

In C and C++ mixing signed and unsigned types in expressions can be tricky since here

void foo(void)
{
 unsigned int a = 6;
 int b = -20;
 (a+b > 6) ? puts("> 6") : puts("<= 6");
}

output could (unexpectedly) be ">6" due to promotions. b will be promoted to unsigned. Do same things happen in C#? Is it also dangerous to mix signed and unsigned in C# expressions?

  • did you try it? `uint a = 6; int b = -20; Console.WriteLine(a + b > 6 ? ">6" : "<6");` – DLeh Oct 26 '15 at 20:22
  • 2
    @DLeh: Even if I try that doesn't give me general answer –  Oct 26 '15 at 20:22
  • 2
    http://stackoverflow.com/questions/26368068/addition-of-int-and-uint – DLeh Oct 26 '15 at 20:23
  • it's dangerous if you don't take possible issues into account. try `Console.WriteLine(typeof(a+b));` That will tell you the type of the result. – GreatAndPowerfulOz Oct 26 '15 at 20:25
  • 1
    @Gread.And.Powerful.Oz that will rather get you a syntax error :) – Lucas Trzesniewski Oct 26 '15 at 20:25
  • @DLeh: So in C# you don't get surprises because of such operations? that answer answers just one case –  Oct 26 '15 at 20:26
  • @LucasTrzesniewski:So in C# you don't get surprises because of such operations? that answer answers just one case –  Oct 26 '15 at 20:27
  • @LucasTrzesniewski and what syntax error would you expect to get? It's an object and WriteLine takes an object and will call ToString() on it. – GreatAndPowerfulOz Oct 26 '15 at 20:27
  • @Gread.And.Powerful.Oz `typeof` expects a type name – Lucas Trzesniewski Oct 26 '15 at 20:27
  • 2
    You can also `(a+b).GetType()` – Ben Voigt Oct 26 '15 at 20:27
  • @BenVoigt: Can I get unexpected results by mixing these types? –  Oct 26 '15 at 20:28
  • @DLeh: Actually that what you marked duplicate doesn't answer the question in the way I stated! –  Oct 26 '15 at 20:28
  • @Quser nothing is "unexpected" since every type promotion is well-defined in C#. When in doubt, simply use `var` (this is the equivalent of C++'s `auto`) – Lucas Trzesniewski Oct 26 '15 at 20:28
  • @Quser: I don't recall the C# type-handling rule offhand. Type promotions are covered in the language spec. `((int)(a+b))` should be safe. – Ben Voigt Oct 26 '15 at 20:29
  • @quser from the answer in the other thread: `The other implicit numeric conversions never lose any information. `, so you will be safe doing this. – DLeh Oct 26 '15 at 20:29
  • @LucasTrzesniewski: In C++ you can see the result is clearly unexpected, so I was wondering how in C#? I have feeling this is not duplicate of the question linked –  Oct 26 '15 at 20:30
  • @LucasTrzesniewski gaaah! – GreatAndPowerfulOz Oct 26 '15 at 20:31
  • @Quser actually I'm pretty sure type promotion is well-defined in C++ too, but not necessarily to the same types as in C#. Most of C# code in the wild doesn't bother to use unsigned types, and just default to `int`, even when the value should intrinsically be unsigned. – Lucas Trzesniewski Oct 26 '15 at 20:31
  • C++ can bring and make more surprises in such situations than C# – Schullz Oct 26 '15 at 20:32
  • @LucasTrzesniewski: It is well defined but if you don't know promotion rules would you expect ">6" in my example? –  Oct 26 '15 at 20:32
  • @DLeh: I don't think this is exact dupe because this question could have a more general answer but you closed it –  Oct 26 '15 at 20:35
  • @Quser my stance on this is that you should know the language you're using, no excuses :-) In C++ I'd expect at least a compiler warning for this, and for me getting a warning means the code is broken. The duplicate question's answers contain relevant spec citations, and there's no more general answer than the detailed spec. – Lucas Trzesniewski Oct 26 '15 at 20:36
  • @LucasTrzesniewski: "In C++ I'd expect at least a compiler warning for this, and for me getting a warning means the code is broken" - I don't think you will necessarily get warning, depends on compiler maybe –  Oct 26 '15 at 20:37
  • @LucasTrzesniewski: Then I meant more digestable answer –  Oct 26 '15 at 20:38
  • @LucasTrzesniewski: I'd advise to vote to reopen –  Oct 26 '15 at 20:44
  • @DLeh: I'd advise to vote to reopen, maybe this one attracts more digestable answers –  Oct 26 '15 at 20:46
  • @Quser i will vote how i choose to vote, thank you very much. And I think that your bolded question "is it dangerous" is easily answered by the quote I mentioned before which specifically says that it is always safe. – DLeh Oct 26 '15 at 20:47
  • 1
    @Quser that's not going to happen - your question is the textbook definition of a duplicate, and the answers in the other question are very good and complete. There's no need to duplicate that information here. – Lucas Trzesniewski Oct 26 '15 at 20:50

0 Answers0