Random C# and CLR thoughts

– Overflow checking is Off:

CLR has different instructions based on whether to perform overflow checking: add, subtract, multiply and its corresponding add.ovf, subtract.ovf and multiply.ovf.  To improve performance it does not check for overflow when performing calculations on primitive types (except System.Decimal, which we will get to later).  So, if you do not want overflows to happen in your system then wrap your statement in C# checked operator which will force the CLR to use the xxx.ovf instruction and throw OverflowException if it finds one.  Now going back to the Decimal data type, the checked/unchecked operator has no effect on it and is simply ignored (Decimal always throw an OverflowException if the operation can’t be performed safely).  On other note, Decimal is not considered a primitve data type by the CLR, which means that manipulating it will be slower than primitive values since it does not have IL instructions built into it.

overflowPNG

 

– Implicit/explicit conversion operators – what..?

Simply put, these convert an object from one type to another type either implicitly or explicitly.  I prefer the ‘explicit’ operator and force the developer to make a conscious decision on what the heck they are doing (by applying the cast operator).  Example is as follows:

implicit_explicit

In either case (implicit/explicit) the compiler will always explicitly apply the cast operator, which we can see here

compiler casting

 

– Layout.Auto/Layout.Sequential attributes

The Auto attribute tells the CLR that it is ok to rearrange fields in memory and group them in certain ways to improve performance, whereas the Sequential attribute says not to mess with the programmer defined field order.  By default Layout.Auto is applied to Reference types and Layout.Sequential to Value types, because the value type field ordering might be important when dealing with unmanaged code.  However, if you know that your code won’t have to integrate with unmanaged code you can apply the Layout.Auto attribute to your value types in order to allow the CLR to optimize your code.

LayoutKind

 

– C# is and as operators

They are different from an explicit cast operator in that both of these operators does not throw an exception. is returns boolean and as performs the cast if applicable or returns null, blah, blah… stuff we all know.  However, the second code snippet is more performant no matter how small, so pay attention when you code.

Inheritance_bad

inheritance_good

 

– More random thoughts

Value types will never throw NullReferenceException because a value type variable isn’t a pointer to some reference on the managed heap, it is actually allocated on the thread stack itself (no GC as well).

Value types derive from System.ValueType which derives from System.Object.

Enum types derive from System.Enum which derives from System.ValueType.

So, as they say – System.Object is the mother of all freak’n objects – enough said Smile



Leave a comment