Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>ushort x = 5, y = 12; </code></pre> <p>The following assignment statement will produce a compilation error, <strong>because the arithmetic expression on the right-hand side of the assignment operator evaluates to int</strong> by default.</p> <pre><code>ushort z = x + y; // Error: conversion from int to ushort </code></pre> <p><a href="http://msdn.microsoft.com/en-us/library/cbf1574z(v=vs.71).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/cbf1574z(v=vs.71).aspx</a></p> <p>EDIT:<br/> In case of arithmetic operations on ushort, the operands are converted to a type which can hold all values. So that overflow can be avoided. Operands can change in the order of int, uint, long and ulong. Please see the <a href="http://www.microsoft.com/download/en/details.aspx?id=7029" rel="nofollow noreferrer">C# Language Specification</a> In this document go to section 4.1.5 Integral types (around page 80 in the word document). Here you will find:</p> <blockquote> <p>For the binary +, –, *, /, %, &amp;, ^, |, ==, !=, >, &lt;, >=, and &lt;= operators, the operands are converted to type T, where T is the first of int, uint, long, and ulong that <strong>can fully represent all possible values of both operands</strong>. The operation is then performed using the precision of type T, and the type of the result is T (or bool for the relational operators). It is not permitted for one operand to be of type long and the other to be of type ulong with the binary operators.</p> </blockquote> <p>Eric Lipper has stated in a <a href="https://stackoverflow.com/questions/8671427/why-some-types-do-not-have-literal-modifiers">question</a></p> <blockquote> <p>Arithmetic is never done in shorts in C#. Arithmetic can be done in ints, uints, longs and ulongs, but arithmetic is never done in shorts. Shorts promote to int and the arithmetic is done in ints, because like I said before, the vast majority of arithmetic calculations fit into an int. The vast majority do not fit into a short. Short arithmetic is possibly slower on modern hardware which is optimized for ints, and short arithmetic does not take up any less space; it's going to be done in ints or longs on the chip.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload