问题描述
只有在方法内部才是"账单"。增加了+5。 "globalBill"保持在0.谢谢你。
      //测试
      float globalBill;
      public Form1()
      {
          InitializeComponent();
          saveGlobalBill(globalBill);
          Console.WriteLine(" Global bill now =" + globalBill); //控制台返回0
      }
Only inside the method is "bill" increased by +5. The "globalBill" stays at 0. Thank you
//Test
float globalBill;
public Form1()
{
InitializeComponent();
saveGlobalBill(globalBill);
Console.WriteLine("Global bill now = " + globalBill); //Console returns 0
}
推荐答案
; saveGlobalBill(
ref globalBill);
saveGlobalBill( ref globalBill );
。 。 。
public void saveGlobalBill(
ref 浮动账单)
public void saveGlobalBill( ref float bill )
比尔 是一个包含
globalBill 副本的参数。因此,修改 bill 不会影响
globalBill 。 ref 关键字更改了 bill 的含义:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#passing-an-argument逐参考。现在
bill 是 globalBill 的另一个名称。
Bill was an argument that contained a copy ofglobalBill. Therefore, modifying bill did not affect globalBill. The ref keyword changes the meaning of bill: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#passing-an-argument-by-reference. Nowbill is another name for globalBill.
这篇关于为什么这个方法不会改变全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!