ref 与 out传递参数


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
 
RefOut值类型按引用传递 p4 = new RefOut值类型按引用传递();
int a = 1, b = 2;
p4.Swap(ref a,ref b);
int s;
p4.Total(out s);
Console.WriteLine("调换的顺序为{0},{1}",a,b );
Console.WriteLine("Total的值为{0}",s);

Console.ReadKey();
}
}


public int AddParams(params int[] number)
{
int sum = 0;
foreach (int item in number)
{
sum += item;
}
return sum;
}
}
public class Person3
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public int this[int a]
{
get { return age; }
set { age = value; }
}



}


public class RefOut值类型按引用传递
{
public void Swap(ref int a, ref int b)
{
int t;
t = b;
b = a;
a = t;
}
public void Total(out int s)
{
s = 1000;
}


}
}

 

 

 ref : 谁调用,谁负责

out: 输出参数,传参数到方法之前,可以先不赋值。在方法内部,必须要有给参数赋值的语句

posted @ 2016-03-12 15:53  肆季风  阅读(215)  评论(0编辑  收藏  举报