Posts

Showing posts with the label C#

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 valu...

C# Data types

Image
C# Primitive Data types : Any data types directly supported by the compiler are called primitive types. Primitive types map directly to types that exist in the base class library. These basic data types are used across programming language compilers regardless of managed code or unmanaged code, since it is essential to meet needs of storing the basic data(Numeric, character, Bool) in application development. Let us see Primitive Data types of C# : The CLR supports two different types    Value Type    Reference Type   Value Type : The variable representing the instance doesn’t contain a pointer to an instance; the variable contains the fields of the instance itself. Value type instances are usually allocated on a thread’s stack Value types are derived from System.ValueType 1. Value types holds the value itself and values are allocated in stack. 2. Being represented in two forms (boxing & ...