2014-02-27 2 views
7

Ich habe geschockt zu wissen, dass es keine +, -*, / und % arithmetischen Operator für 8 und 16 Bit Ganzzahlen in C# gibt. Ich lese "C# 5.0 Pocket Reference" auf Seite 23 wie folgt.Warum gibt es in C# keine arithmetischen Operatoren (+, -, *, /,%) für 8- und 16-Bit-Ganzzahlen?

enter image description here

Der folgende Code nicht kompiliert.

class Program 
{ 
    static void With16Bit() 
    { 
     short a = 1; 
     short b = 2; 
     short c = a + b; 
     Console.WriteLine(c); 
    } 

    static void With8Bit() 
    { 
     byte a = 1; 
     byte b = 2; 
     byte c = a + b; 
     Console.WriteLine(c); 
    } 

    static void Main(string[] args) 
    { 
     With8Bit(); 
     With16Bit(); 
    } 
} 

Warum haben die C# -Designer das gemacht? Was sind ihre Überlegungen dazu?

+4

in diesem Kommentar von Eric Lippert Beantwortet http://stackoverflow.com/questions/941584/byte-byte-int-why/941627#comment750078_941584 – keyboardP

Antwort

9

Es gibt arithmetics mit Int8, Int16; aber das Ergebnis ist int32 und so haben Sie Stimmen:

class Program 
{ 
    static void With16Bit() 
    { 
     short a = 1; 
     short b = 2; 
     short c = (short) (a + b); // <- cast, since short + short = int 
     Console.WriteLine(c); 
    } 

    static void With8Bit() 
    { 
     byte a = 1; 
     byte b = 2; 
     byte c = (byte) (a + b); // <- cast, since byte + byte = int 
     Console.WriteLine(c); 
    } 

    static void Main(string[] args) 
    { 
     With8Bit(); 
     With16Bit(); 
    } 
} 
+2

Ja, aber das war nicht seine Frage: "Warum haben die C# -Designer das gemacht? Was haben sie darüber gedacht?" – Dirk

+0

Dies erklärt, warum nicht kompiliert wird, aber nicht beantwortet, warum die Entwickler die Operatoren nicht für Bytes, Bytes, Kurzschlüsse, ushorts überladen. – keyboardP

+0

@keyboardP Es gibt ** Überladungen, es ist nur ein Cast-Fehler. – ken2k

5

Plese daran erinnern, dass, wenn Sie Additionsoperation durchführen auf short und byte der Standard Ergebnis Integer sein würde.

So ist die:

Byte + Byte = int
short + short = int

Also, wenn Sie den aktuellen Wert Sie es brauchen, um wieder wollen zurück zu werfen.

Try This:

 short a = 1; 
     short b = 2; 
     short c =(short) (a + b); 
     Console.WriteLine(c); 

     byte a = 1; 
     byte b = 2; 
     byte c =(byte) (a + b); 
     Console.WriteLine(c); 

Vom Source:

Dieses Verhalten ist, weil die Designer haben nicht berücksichtigt, dass byte und short als tatsächliche Zahlen, aber sie haben sie als betrachtet nur Sequenz von bits. so arithmetische Operationen auf ihnen macht keinen Sinn so, wenn das der Fall Int und lange würde den Zweck erfüllen.

2

Von kek444 answer

All operations with integral numbers smaller than Int32 are widened to 32 bits 
before calculation by default. The reason why the result is Int32 is 
simply to leave it as it is after calculation. If you check the 
MSIL arithmetic opcodes, the only integral numeric type they operate 
with are Int32 and Int64. It's "by design". 
Verwandte Themen