The C# code you posted works fine for me.
No way to know where your error is since you didn’t post a grasshopper file.
3. Attach minimal versions of all the relevant files
-Kevin