- class
Program -
{ -
//使用out后必须对变量赋值 -
public void TestOut(out int x, out int y) -
{ -
x = 1; -
y = 2; -
} -
//此时传进来的值分别为x1:10,y1:11,输出之后的x1的值为2 -
-
public void TestRef(ref int x, ref int y) -
{ -
//引用剪剪那句话传进来的是猪,出来的可能是头牛(很精辟!) -
x = 2; -
//这边可以不对y赋值,而out那就不行了. -
-
} -
static void Main(string[] args) -
{ -
int x=10; -
int y=11; -
Program P1 = new Program(); -
P1.TestOut(out x, out y); //out会清空原来变量的值 -
Console.WriteLine("x={0},y={1}", x, y); -
//在使用之前ref必须对变量赋值 -
int x1 = 10; -
int Y1 = 11; -
P1.TestRef(ref x1, ref Y1); -
Console.WriteLine("x1={0},y1={1}", x1, Y1); -
Console.ReadKey(); -
} -
-
//el可以把参数的数值传递进函数, -
//但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空, -
//所以你必须初始化一次。这个就是两个的区别,或者说就像有的网友说的,rel是有进有出,out是只出不进。 -
-
-
}
以前不知道ref和out有个啥用,其实ref和out可以在不需要返回值的情况下,传递值,
在有返回值的方法里,除了可以得到返回的值外,还可以得到方法里面声明的ref,和out参数的值
比喻如下的一个验证方法,可以传递验证的信息,既返回了真假,又可以得到验证的信息
- ///
<summary> - ///检查表单验证信息
- ///
</summary> - private
bool CheckFromInfo(ref string name) - {
-
-
if (txt_leaveDate.Text == "") -
{ -
name="请假日期不能为空"; -
return false; -
} -
if (txt_name.Text == "") -
{ -
name = "姓名不能为空"; -
return false; -
} -
if (txt_writedate.Text == "") -
{ -
name="申请日期不能为空"; -
return false; -
-
} -
string enddate = txt_leaveDate.Text; -
DateTime date = Convert.ToDateTime(enddate); -
DateTime now =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); -
if (date < now) -
{ -
name = "请假日期不能小于申请日期(填表日期)!"; -
return false; -
} -
if (ddltype.SelectedItem.Text == "请选择") -
{ -
name = "请选择请假类型!"; -
return false; -
} -
return true; -
-
-
-
-
- }
-
-
-
- 是调用
-
string name=null; -
if (CheckFromInfo(ref name) == false) -
{ -
-
JScript.Alert(name,this.Page);//弹出提示消息 -
return; -
}
name是有值的.不然人家叫out干吗?out嘛.你不让人家带东西出来,不给面试,人家当然不乐意了
用ref的话,就不必了.如果在方法里面ref name,name的值