110,528
社区成员
发帖
与我相关
我的任务
分享
static void Square(int x)
{
// code...
}
static void Square(ref int x)
{
// code...
}
public class TestClass
{
public int a;
}
static void Square(TestClass x)
{
x.a = 123;
}
void Main()
{
TestClass tc = new TestClass();
tc.a = 111;
Square(tc);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestClass tc = new TestClass(1);
TestClass originalTC = tc;
Console.WriteLine("The original tc...");
Console.WriteLine(string.Format("tc.Number={0}.", tc.Number));
Console.WriteLine();
Program p = new Program();
p.ModifyTestClass(tc);
Console.WriteLine("After calling ModifyTestClass()...");
Console.WriteLine(string.Format("tc.Number={0}.", tc.Number));
Console.WriteLine(string.Format("Does the current tc equal to the original tc? Answer: {0}", (tc == originalTC)));
Console.WriteLine();
Console.WriteLine("After calling ModifyTestClassRef()...");
p.ModifyTestClassRef(ref tc);
Console.WriteLine(string.Format("tc.Number={0}.", tc.Number));
Console.WriteLine(string.Format("Does the current tc equal to the original tc? Answer: {0}", (tc == originalTC)));
Console.ReadKey();
}
public void ModifyTestClass(TestClass tc)
{
tc = new TestClass(2);
}
public void ModifyTestClassRef(ref TestClass tc)
{
tc = new TestClass(2);
}
}
class TestClass
{
public TestClass(int number)
{
Number = number;
}
public int Number = 0;
}
}