Why it is not the same?

45 views Asked by At

I have this variables inside a class:

 public const ushort HEADER_LENGTH = 5;
 public const ushort CHECKSUM_LENGTH = 2;
 public ushort longitudTrama;
 public ushort longitudTotal;

If I do that:

longitudTotal = longitudTrama;
longitudTotal += HEADER_LENGTH;
longitudTotal += CHECKSUM_LENGTH;

The compiler doesn't generate any error.

But if I do that:

longitudTotal = longitudTrama + HEADER_LENGTH + CHECKSUM_LENGTH;

The compiler says I am missing a cast because it cannot implicitly convert int to ushort. Which int!!??

Thank you.

1

There are 1 answers

0
Alce On

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators Here is all the informations about the impicit and explicit casting of the operators.