1
using System;
2
3
public partial class testref : System.Web.UI.Page
4
{
5
static void outTest(out int x, out int y)
6
{//离开这个函数前,必须对x和y赋值,否则会报错。
7
//y = x;
8
//上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
9
x = 1;
10
y = 2;
11
}
12
static void refTest(ref int x, ref int y)
13
{
14
x =x+ 1;
15
y = y+1;
16
}
17
18
protected void Page_Load(object sender, EventArgs e)
19
{
20
//out test
21
int a, b;
22
//out使用前,变量可以不赋值
23
outTest(out a, out b);
24
Response.Write("a={0};b={1}"+a+b);
25
int c = 11, d = 22;
26
outTest(out c, out d);
27
Response.Write("c={0};d={1}"+c+d);
28
29
//ref test
30
int m, n;
31
//refTest(ref m, ref n);
32
//上面这行会出错,ref使用前,变量必须赋值
33
34
int o = 11, p = 22;
35
refTest(ref o, ref p);
36
Response.Write("o={11};p={22}" + o + p);
37
38
39
}
40
}
41
using System;2

3
public partial class testref : System.Web.UI.Page4
{5
static void outTest(out int x, out int y)6
{//离开这个函数前,必须对x和y赋值,否则会报错。 7
//y = x; 8
//上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行 9
x = 1;10
y = 2;11
}12
static void refTest(ref int x, ref int y)13
{14
x =x+ 1;15
y = y+1;16
} 17

18
protected void Page_Load(object sender, EventArgs e)19
{20
//out test 21
int a, b;22
//out使用前,变量可以不赋值 23
outTest(out a, out b);24
Response.Write("a={0};b={1}"+a+b);25
int c = 11, d = 22;26
outTest(out c, out d);27
Response.Write("c={0};d={1}"+c+d);28

29
//ref test 30
int m, n;31
//refTest(ref m, ref n); 32
//上面这行会出错,ref使用前,变量必须赋值 33

34
int o = 11, p = 22;35
refTest(ref o, ref p);36
Response.Write("o={11};p={22}" + o + p);37

38

39
}40
}41



浙公网安备 33010602011771号