C# Pointers in a Method's arguments?

后端 未结 5 2029
一个人的身影
一个人的身影 2021-01-11 22:24

I wish to directly modify a variable\'s value outside of a method from inside it.
Pointers are the way, correct?

How?

5条回答
  •  轮回少年
    2021-01-11 22:34

    Pointers are the way, correct?

    Not in C# they're not. Use ref parameters:

     void Times2(ref int a) { a = a * 2; }
    
     void Foo()
     {
          int x = 1;
          Times2(ref x);
          // x is now 2 
     }
    

提交回复
热议问题