我有以下法典,在研究时,它显示,最初的我Int和我的Float在返回之前不会改变其价值。 他们的价值每当被改变在所谓的方法中时,是否应当改变,因为每次都采用这些方法?
class Tester
{
public void Run()
{
int myInt = 42;
float myFloat = 9.685f;
Console.WriteLine("Before starting:
value of myInt: {0}
value of myFloat: {1}", myInt, myFloat);
// pass the variables by reference
Multiply( ref myInt, ref myFloat );
Console.WriteLine("After finishing:
value of myInt: {0}
value of myFloat: {1}", myInt, myFloat);
}
private static void Multiply (ref int theInt, ref float theFloat)
{
theInt = theInt * 2;
theFloat = theFloat *2;
Divide( ref theInt, ref theFloat);
}
private static void Divide (ref int theInt, ref float theFloat)
{
theInt = theInt / 3;
theFloat = theFloat / 3;
Add(ref theInt, ref theFloat);
}
public static void Add(ref int theInt, ref float theFloat)
{
theInt = theInt + theInt;
theFloat = theFloat + theFloat;
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}