I have started learning C# and need to clear some of confusion on overflown concept. As we aware of that if we exceed the limit of any data type , C# simply returns 0 .
eg : byte b = 255; if we increase the value of b by 1, then the value of b will be zero.for below code i am getting output as 256.
using System;
namespace HelloWorld{
class program{
static void Main(){
byte b = 255;
Console.WriteLine(b+1);
}
}
}
Instead of 0, i am getting output as 256 , which is out of the limit of b of type byte. How is this possible ?.
using System;
namespace HelloWorld{
class program{
static void Main(){
byte b = 255;
b = b+1
Console.WriteLine(b);
}
}
for above code i am getting compilation error I.e error CS0266: Cannot implicitly convert type int' tobyte'. An explicit conversion exists (are you missing a cast?)
Help !!!!
byte b = 255;b++;gives you the expected result+defined forbyte. So the compiler automatically uses the one forint. The same applies toshort,charand probably a few other integral types.