Intrinsics
Intrinsic methods are special kind of methods whose implementation is handled specially by JIT compiler or written in pure IL code to achieve low (or zero, in some situations) overhead. .NET library has good example in the form of Unsafe class.
.NEXT library has numerous intrinsics which are exposed by AdvancedHelpers class and extends Unsafe and RuntimeHelpers classes with static methods.
Common Intrinsics
Common intrinsics are various methods aimed to increase performance in some situations.
AlignOf<T> method is a macros that determines the alignment of the specified type.
AreCompatible method is a macros that determines binary compatibility of two types passed as generic arguments. The two types are compatible if
- They have the same alignment
- They have the same size
For instance, byte and sbyte are compatible.
IsDefault method is the most useful method that allows to check whether the value of type T is default(T). It works for both value type and reference type.
using DotNext.Runtime.CompilerServices;
RuntimeHelpers.IsDefault(""); //false
RuntimeHelpers.IsDefault(default(string)); //true
RuntimeHelpers.IsDefault(0); //true
RuntimeHelpers.IsDefault(1); //false
Exact Type-Testing
The is operator in C# checks if the result of an expression is compatible with a given type. However, in some rare cases you need to check whether the object is of exact type. IsExactTypeOf method provides optimized implementation of this case:
"a" is object; //true
"a" is string; //true
object.IsExactTypeOf("a"); //false
string.IsExactTypeOf("a"); //true
Array Length
C# 9 introduces primitive type nuint that can be used for accessing array elements without hidden conversion of the index. However, there is no way to obtain array length as native integer. Intrinsics.GetLength provides this ability so the loop over array elements can be rewritten as follows:
using DotNext.;
var array = new int[] { ... };
for (nuint i = 0; i < Array.GetLength(array); i++)
{
}