porting code to c sharp