using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void ShowDouble(ref int val)
{
val *= 2;
Console.WriteLine("val doubled = {0}", val);
}
static void Main(string[] args)
{
int val = 5;
Console.WriteLine("val = {0}", val); // 5
ShowDouble(ref val); // 同一个内存,10
Console.WriteLine("val = {0}", val); // 10
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static int val;
static void ShowDouble()
{
val *= 2;
Console.WriteLine("val doubled = {0}",val);
}
static void Main(string[] args)
{
val = 5;
Console.WriteLine("val = {0}",val); // 5
ShowDouble(); // 同一个内存,10
Console.WriteLine("val = {0}", val); // 10
Console.ReadKey();
}
}
}
这两段代码效果一样,引用的都是同一个变量!
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6748055.html,如需转载请自行联系原作者