How to subtract bytes on one line in C #

This is really weird. Can anyone explain this?

This code does NOT work:

const byte ASC_OFFSET = 96; string Upright = "firefly"; byte c7 = (byte)Upright[6] - ASC_OFFSET; //Cannot implicitly convert type 'int' to 'byte'. 

This code also DOES NOT work:

 const byte ASC_OFFSET = 96; string Upright = "firefly"; byte c7 = (byte)Upright[6] - (byte)ASC_OFFSET; //Cannot implicitly convert type 'int' to 'byte'. 

However, subtraction on a separate line works very well:

 const byte ASC_OFFSET = 96; string Upright = "firefly"; byte c7 = (byte)Upright[6]; c7 -= ASC_OFFSET; 

I don't mind putting instructions on separate lines if I need to ... but I need to wonder ...

Why?

+4
source share
3 answers

I noticed that too. I think because the -= operator -= predefined for byte types, whereas in the first cases you really put an int inside a byte , which is unacceptable. The reason why they did this does not necessarily make sense, but it is consistent with the rules, because in previous cases the compiler cannot "peek" into the statement - when performing the task.

If you really need to subtract one line, instead of saying:

 byte c7 = (byte)Upright[6] - ASC_OFFSET; 

Say:

 byte c7 = (byte)(Upright[6] - ASC_OFFSET); 
+3
source

This is due to the fact that 1) byte operations lead to int (see why here: http://blogs.msdn.com/b/oldnewthing/archive/2004/03/10/87247.aspx ) and 2) the following code WITH#

 c7 -= ASC_OFFSET; 

will be "magically" compiled backstage in

 c7 = (byte)(c7 - ASC_OFFSET); 

This is explicitly described in the C # specification here: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf

14.14.2 Compound purpose:

An operation of the form x op = y is processed by applying the binary operator (Β§14.2.4), as if the operation were written x op y. Then,

β€’ If the return type of the selected operator is implicitly convertible to type x, the operation evaluates to x = x op y, except that x is evaluated only once.

β€’ Otherwise, if the selected operator is a predefined operator, if the return type of the selected operator is explicitly converted to type x, and if y is implicitly converted to type x or the operator is a shift operator, then the operation is evaluated as x = (T) (x op y) , where T is the type of x, except that x is evaluated only once.

β€’ Otherwise, the composite assignment is incorrect and a compile-time error occurs

+7
source

The reason your first two samples do not compile is because:

  • Casting binds β€œtougher” than subtraction. That is: (C) de 'means' ((C) d) -e', not '(C) (de)'. The casting operator has a higher priority.
  • Therefore, the type of both operands for subtraction is a byte, regardless of the cast.
  • The subtraction type is int, because there is no subtraction operator in bytes.
  • Therefore, you assign an int to a byte without a cast, which is illegal.

There is no subtraction operator in bytes because, suppose you have a byte containing 7 and you subtract a byte containing 8 from it, do you really want it to be byte 255? I think most people would like this to be int -1.

Finally, why do you do this in bytes in the first place? It makes no sense. Balls are not bytes in C #; if you want to do arithmetic on characters, then why not subtract char 96 from char 'y' instead of doing a crash and dangerous conversion to bytes?
+4
source

Source: https://habr.com/ru/post/1333977/


All Articles