2

Would this result in an overflow?if no then why not? because 'byte' type would still remain of size 8 bits , and if it wo'nt result in an overflow then does that mean that 'casting' increases the 'byte' type's allocation in memory from 8 bits to 32 bits?..ultimately what how does type casting [implicit] actually works?

//a part of c# program

byte b = 100;
b = (Byte)(b + 200);
Console.WriteLine("VALUE OF BYTE TYPE'S OBJECT {0}",b);

//end 
Hemant Kumar
  • 141
  • 11
  • MSDN says "Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes." .what actually happens? does the memory allocation size gets increased and hence no data is lost ? – Hemant Kumar Jul 28 '13 at 19:51
  • 1
    It's not entirely clear what you are asking. `b` is a `byte` and stays the same size. `+` is not defined for the `byte` type, but it is defined for `int` so the compiler implicitly converts `b` to an `int` and the result is an `int`. – Mike Zboray Jul 28 '13 at 19:57

5 Answers5

3

If an expression produces a value that is outside the range of the destination type, the result is truncated.

If you want to know if your code produce overflow you should use checked statement (msdn):

checked
{
    byte b = 100;
    b = (Byte)(b + 200);
    Debug.WriteLine("VALUE OF BYTE TYPE'S OBJECT {0}", b); 
}
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
2

The implicit conversion is the b+200. That implicitly becomes an int, equal to 300, just like the MSDN doc says. Then you explicitly cast to byte, which truncates it to 8 bits. As kmatyaszek suggests, you can enable overflow checking at runtime within a checked block.

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
1

Consider your snippet written as below:

byte b = 100;
Console.WriteLine(b);

b = (Byte)(b + 200);

Console.WriteLine(b);

This code will output 100 and 44 respectively. When this code executes what is actually happening is that the adding operation results in a larger than 8 bit value, but the program does not care and just takes the last 1 byte (e.g. last 8 bits in a 32 bit value)

EDIT: Forget to mention that it will not result in an overflow error/exception when performing that operation. Consider the code:

b = Byte.MaxValue;
Console.WriteLine(b);
b++;
Console.WriteLine(b);

Which outputs 255 & 0.

However this code will:

b = Byte.MaxValue;
Console.WriteLine(b);
b = checked((byte)(b + 1));
Console.WriteLine(b);

See:

Community
  • 1
  • 1
jrbeverly
  • 1,611
  • 14
  • 20
1

No because when you perform an explicit cast(and you have to since it inplicitly becomes int) its like you say to the compiler "yes i know what i am doing,just do it anyway",so the result will be (300 - 256 = 44),so to result in an overflow exception you would need to add the checked key word like this in a proper try/catch block:

try
{
   byte b = 100;
   b = checked((byte)(b + 200));
   Console.WriteLine("VALUE OF BYTE TYPE'S OBJECT {0}", b);
}
catch(OverFlowException e)
{
   Console.WriteLine(e.Message);
}
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
1

See C# language specification:

7.6.12 The checked and unchecked operators

The following operations are affected by the overflow checking context established by the checked and unchecked operators and statements:
...
Explicit numeric conversions (§6.2.1) from one integral type to another integral type, or from float or double to an integral type.

When one of the above operations produce a result that is too large to represent in the destination type, the context in which the operation is performed controls the resulting behavior:
...
• In an unchecked context, the result is truncated by discarding any high-order bits *that do not fit in the destination type*.

For non-constant expressions (expressions that are evaluated at run-time) that are not enclosed by any checked or unchecked operators or statements, the default overflow checking context is unchecked unless external factors (such as compiler switches and execution environment configuration) call for checked evaluation.

This - (byte) makes an explicit numeric conversion from int, that by default runs in unchecked context, hence, truncates the result.

Dennis
  • 37,026
  • 10
  • 82
  • 150