麻烦前辈帮我看一下这个程序~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace p3_7
{
class Program
{
static void Main(string[] args)
{
BankCard c1 = new BankCard() { id = "001", money = 500 };
EditCard(c1);
c1.Query();
ChangeCard(ref c1);
c1.Query();
}
static void EditCard(BankCard c)
{
c.money = 1000;
c = new BankCard() { id = "002" };
}
static void ChangeCard(ref BankCard c)
{
c = new BankCard() { id = "002" };
}
}
class BankCard
{
public string id;
public decimal money=0;
public void Query()
{
Console.WriteLine("卡号{0} 余额{1}",id,money);
}
}
}
请问这个程序为什么结果是卡号001 然后金额是1000而不是500.。。
哪一句程序将值换了,谢谢大家了!
------解决方案--------------------
BankCard c1 = new BankCard() { id = "001", money = 500 };
↓
c1--> BankCard0{ id = "001", money = 500 } 地址0001
——————————————————————————————————
EditCard(c1)
↓
c1--> BankCard0{ id = "001", money = 500 } 地址0001
c --> BankCard0{ id = "001", money = 500 } 地址0001
c.money = 1000;
↓
c1--> BankCard0{ id = "001", money = 1000} 地址0001
c --> BankCard0{ id = "001", money = 1000} 地址0001
c = new BankCard() { id = "002" };
↓
c1--> BankCard0{ id = "001", money = 1000} 地址0001
c --> BankCard1{ id = "002"} 地址0002