Ich kann Ihnen die Logik nicht geben, aber ich kann sagen, warum der Compiler dieses Verhalten vom Standpunkt der Regeln hat, denen der Compiler folgen muss (was vielleicht nicht wirklich das ist, was Sie interessant zu wissen sind).
Von einer alten Kopie der C# spec (ich wahrscheinlich eine neuere Version herunterladen soll), Hervorhebung hinzugefügt:
14.2.6.2 Binary numeric promotions This clause is informative.
Binary numeric promotion occurs for the operands of the predefined +
, ?
, *
, /
, %
, &
, |
, ^
, ==
, !=
, >
, <
, >=
, and <=
binary operators. Binary numeric promotion implicitly converts both operands to a common type which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric promotion consists of applying the following rules, in the order they appear here:
- If either operand is of type decimal, the other operand is converted to type decimal, or a compile-time error occurs if the other operand is of type float or double.
- Otherwise, if either operand is of type double, the other operand is converted to type double.
- Otherwise, if either operand is of type float, the other operand is converted to type float.
- Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a compile-time error occurs if the other operand is of type sbyte, short, int, or long.
- Otherwise, if either operand is of type long, the other operand is converted to type long.
- Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.
- Otherwise, if either operand is of type uint, the other operand is converted to type uint.
- Otherwise, both operands are converted to type int.
Also, im Grunde Operanden kleiner als ein int
wird int
für diese Operatoren umgewandelt werden (und das Ergebnis wird ein int
für die nicht-relationalen Ops sein).
Ich sagte, dass ich Ihnen keine Begründung geben konnte; Ich rate jedoch zu einer - ich denke, dass die Entwickler von C# sicherstellen wollten, dass Operationen, die Informationen verlieren könnten, wenn sie eingeengt würden, diese Engstellenoperation explizit durch den Programmierer in Form einer Besetzung hätten machen müssen. Zum Beispiel:
byte a = 200;
byte b = 100;
byte c = a + b; // value would be truncated
Während diese Art von Abschneiden nicht passieren würde, wenn eine XOR-Operation zwischen zwei Byteoperanden durchgeführt wird, denke ich, dass die Sprachdesigner wahrscheinlich nicht einen komplexeren Satz von Regeln haben wollten, wo einig Operationen würden explizite Umwandlungen erfordern und andere nicht.
Nur eine kleine Anmerkung: die oben genannte Zitat ‚Informations‘ nicht ‚normative‘ ist, aber es deckt alle Fälle in einer einfachen Form zu lesen. Streng (im normativen Sinne) gesprochen, der Grund der ^
Operator verhält sich diese Art und Weise ist, weil die nächste Überlastung für den Bediener, wenn sie mit byte
Operanden zu tun ist (von 14.10.1 „Integer logischen Operatoren“):
int operator ^(int x, int y);
Wie der informative Text erklärt, werden daher die Operanden zu int
befördert und ein int
Ergebnis wird erzeugt.
in der Tat ist es seltsam, bitweise Operationen (gut, außer für die Verschiebung nach links) nicht überläuft – Hao
Byte-Operationen im Allgemeinen können Werte größer als 255, wie wenn Sie Schicht verlassen ein Byte, natürlich und/oder Operatoren nicht Ergebnisse größer als Byte erzeugen, aber aus Kompatibilitätsgründen wird das Ergebnis int! –