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();
}
}
}
这两段代码效果一样,引用的都是同一个变量!
|
请发表评论