C# version of code