Implementation of pivoting in C# code