DEMO[C#]refKeyword and outKeyword 关于ref与out关键字区别的DEMO
相关ref与out的语法规则请查询MSDN
ref : ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.chs/dv_csref/html/b8a5e59c-907d-4065-b41d-95bf4273c0bd.htm
out : ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.chs/dv_csref/html/7e911a0c-3f98-4536-87be-d539b7536ca8.htm
以上两个链接是适合Express版,其他版本未测试,应该可以通用。
两个DEMO基本上一样,但是在区别部分已经使用注释以示区别了。
DEMO1(ref)
程序输出:
In Main() before Sum(): num1=3,num2=12
In Sum() : num1=3,num2=12 -------3+12=15
In Main() after Sum(): num1=3,num2=444
DEMO2(out)
ref : ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.chs/dv_csref/html/b8a5e59c-907d-4065-b41d-95bf4273c0bd.htm
out : ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.chs/dv_csref/html/7e911a0c-3f98-4536-87be-d539b7536ca8.htm
以上两个链接是适合Express版,其他版本未测试,应该可以通用。
两个DEMO基本上一样,但是在区别部分已经使用注释以示区别了。
DEMO1(ref)
using System;
using System.Collections.Generic;
using System.Text;
namespace CA_refKeyword
{
class Program
{
static void Sum(int num1, ref int num2)
{
//num2 = 1; //此句可不写!!!!!!!!!!与out不同之处!!!!!!!!!!!!!!!!
Console.WriteLine("In Sum() : num1={0},num2={1} -------{0}+{1}={2}", num1, num2, num1 + num2);
num2 = 444;
}
static void Main(string[] args)
{
int num1 = 3, num2 = 12;
//int num1 = 3, num2; //ERROR!
Console.WriteLine("In Main() before Sum(): num1={0},num2={1}", num1, num2);
Sum(num1, ref num2);
Console.WriteLine("In Main() after Sum(): num1={0},num2={1}", num1, num2);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace CA_refKeyword
{
class Program
{
static void Sum(int num1, ref int num2)
{
//num2 = 1; //此句可不写!!!!!!!!!!与out不同之处!!!!!!!!!!!!!!!!
Console.WriteLine("In Sum() : num1={0},num2={1} -------{0}+{1}={2}", num1, num2, num1 + num2);
num2 = 444;
}
static void Main(string[] args)
{
int num1 = 3, num2 = 12;
//int num1 = 3, num2; //ERROR!
Console.WriteLine("In Main() before Sum(): num1={0},num2={1}", num1, num2);
Sum(num1, ref num2);
Console.WriteLine("In Main() after Sum(): num1={0},num2={1}", num1, num2);
Console.ReadKey();
}
}
}
程序输出:
In Main() before Sum(): num1=3,num2=12
In Sum() : num1=3,num2=12 -------3+12=15
In Main() after Sum(): num1=3,num2=444
DEMO2(out)
using System;
using System.Collections.Generic;
using System.Text;
namespace CA_outKeyword
{
class Program
{
static void Sum(int num1, out int num2)
{
num2 = 1; //必须重新赋值
Console.WriteLine("In Sum() : num1={0},num2={1} -------{0}+{1}={2}", num1, num2, num1 + num2);
num2 = 444;
}
static void Main(string[] args)
{
int num1 = 3, num2 = 12;
Console.WriteLine("In Main() before Sum(): num1={0},num2={1}", num1, num2);
Sum(num1, out num2);
Console.WriteLine("In Main() after Sum(): num1={0},num2={1}", num1, num2);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace CA_outKeyword
{
class Program
{
static void Sum(int num1, out int num2)
{
num2 = 1; //必须重新赋值
Console.WriteLine("In Sum() : num1={0},num2={1} -------{0}+{1}={2}", num1, num2, num1 + num2);
num2 = 444;
}
static void Main(string[] args)
{
int num1 = 3, num2 = 12;
Console.WriteLine("In Main() before Sum(): num1={0},num2={1}", num1, num2);
Sum(num1, out num2);
Console.WriteLine("In Main() after Sum(): num1={0},num2={1}", num1, num2);
Console.ReadKey();
}
}
}
程序输出:
In Main() before Sum(): num1=3,num2=12
In Sum() : num1=3,num2=1 -------3+1=4
In Main() after Sum(): num1=3,num2=444
希望两个DEMO能够让大家理解ref与out的区别。呵呵
posted on 2006-11-08 23:46 volnet(可以叫我大V) 阅读(1567) 评论(6) 编辑 收藏 举报