Posts

Showing posts from August, 2010

Checking Overflow in C#

Image
When you use value type in your application for arithmetic operations, you will need to check the boundaries of data types after the operation completes every time i.e., have to ensure the value of data type does not exceeds the MaxValue of the data type, Overflow does not occur. Else you will get overflow Exception at runtime. Different languages treat this overflow in different Manner( C, C++ does not throw overflow exception rather wrap up the value, MS Visual Basic throws the Overflow Exception by default) Whereas   .Net does not check for the overflow exception until we explicitly we tell the CLR to check for Overflow Exception. By Default Overflow check is set off . Let's see an Example : {                      short val = 32767;        val =   (short)(val + 40000);  } In above code, we try to add constant value to Val variable of short data type and error was expected when i ran   the application. But Surprise, it runs with no Exception But Why ?? Let'