To run this program, you need to compile it with unsafe code enabled:
ZeroVal doesn’t change the i in Main, but ZeroPtr does because it has a reference to the memory address for that variable.
Note that in C#, pointers are only allowed in unsafe contexts. The unsafe keyword is used to denote code blocks where pointer operations are permitted. Additionally, you need to use the fixed statement when taking the address of a managed variable to prevent it from being moved by the garbage collector during the execution of the unsafe code.
Unlike Go, where pointers are more commonly used, C# typically uses reference types and the ref keyword for similar purposes in safe code. The use of raw pointers in C# is generally reserved for interop scenarios or performance-critical code.
When compiling, make sure to use the /unsafe flag to allow unsafe code. When running the program, the output will be similar to the Go version, demonstrating how value types and reference types (via pointers) behave differently.