Important: A solider understanding of basal concepts is critical to any. NET Framework developer's long-term success. turst me: having a solid grasp of these concepts will allow you to build efficient applications faster and easier.
Any data types the complier directly supports are called primitive types. C # has 15 primitive types:Sbyte, byte, short, ushort, int, uint, long, ulong|Char | float (System. Single), double, decimal | bool | object string.
In c #,String(A keyword) maps exactlySystem. String(A FCL Type), there is no difference and either can be used.
Look at this code:
Byte b =100;
b = (Byte) (b+200);// b now contains 44
ByteCast on the second line of the preceding code is required, because the CLR perfor arithmetic operations on 32-bit and 64-bit values only.
Different Versions ages handle overflow in different ways. C and C ++ don't consider overflows to be an error and allow the value to wrap, the application continues running with its fingers crossed. visual Basic, on the other hand, always considers overflows as errors and throwsSystem. OverflowExceptionException when it detects an overflow.
C # allows the programmer to decide how overflows shocould be handled. by default, overflow checking is turned off. one way to get the c # compiler to control overflows is to use the/checked + command-line switch. the code executes more slowly because the CLR is checking these operations to see whether an overflow will occur.
TheCheckedOperator and statement affect only which versions of the add, subtract, multiply, and data conversion IL instructions are produced, calling a method within a checked operator or statement has no impact on the method. the following code demonstrates:
checked{//Assume Somethod tries to load 400 into a Byte.SomeMethod(400);//SomeMethod might or might not throw an OverflowException.//It would if Somethod were compiled with checked instructions.}
TheSystem. DecimalType is a very special type. Although extends programming ages (c # and Visual Basic supported DED) considerDecimalA primitive type, the CLR does not. This means that the CLR doesn't hava IL instructions that know how to manipulateDecimalValue. So when you compile code usingDecimalValues, the compiler generates code to call Decimal's members to perform the actual operations. This means that manipulatingDecimalValues is slower than manipulating CLR primitive values. Also, because there are no IL instructions for manipulatingDecimalValues,CheckedAndUncheckedOperators, statements, and compiler command-line options hava no effect. Operations onDecimalValues always throw an OverflowException if the operation can't be performed med safely.
Boxing and Unboxing value types
It's possible to convert a value type to a reference type using a mechanic called boxing.
Internally,Boxing:
1. memory is allocated from the managed heap. the amount of memory allocated is the size the value type requires plus any additional overhead to consider this value type to be a true object. the additional overhead has des a method table pointer and a SyncBlockIndex.
2. The value type's fields are copied to the newly allocated heap memory.
3. The address of the object is returned.
In contrast, internally,Unboxing:
1. If the reference is null, a NullreferenceException is thrown.
2. If the reference doesn't refer to an object that is a boxed value of the desired value type, an InvalidCastException exception is thrown.
3. A pointer to the value type contained inside the object is returned. the value type that this pointer refers to doesn't konw anything about the usual overhead associated with a true object: a method table pointer and a SyncBlocdIndex. in effect, the pointer refers to the unboxed portion in the boxed object.
So, unboxing is not the exact opposite of boxing. the unboxing operation is much less costly than boxing. unboxing is really just the operation of obtaining a pointer to the raw value type (data fields) contained within an object. so, unlike boxing, unboxing doesn' t involve the copying of any bytes in memory. however, an unboxing operation is typically followed by copying the fields, making these two operations the exact opposite of a boxing operation.
Unboxed value types are lighter-weight types than reference types for two reasons:
A. They are not allocated on the managed heap;
B. They don't have the additional overhead members that every object on the heap has: a method table pointer and a SyncBlockIndex.
[From <applied. Microsoft. NET. Framework. Programming>]