ref:传递引用,需要先初始化,必须先“有”,才能引用。应用场景,内部对外部的值进行改变。

out:内部为外部赋值,不需要初始化,外部初始化也没用。应用场景,内部为外部的变量赋值。out一般用在函数有多个返回值的场所。

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 // ref的用法 传递引用,必须先“有”值,即先初始化。
11 static void Main(string[] args)
12 {
13 int i = 10;
14 int j = 13;
15 change(ref i,ref j);
16 Console.WriteLine("{0} {1}",i,j);
17 Console.ReadKey();
18 }
19 static void change(ref int x,ref int y)
20 {
21 int temp;
22 temp = x;
23 x = y;
24 y = temp;
25 }
26 }
27 }
View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 static void Main(string[] args) //out应用于函数有过个返回值的场所。是在内部为外部赋值。外部无需赋值
11 { //外部初始化也没用。
12 string s = Console.ReadLine();
13 int i;
14 if (int.TryParse(s, out i)) //两个返回值,一个是int型,一个是bool型
15 {
16 Console.WriteLine("转换成功,值是{0}",i);
17 }
18 else
19 {
20 Console.WriteLine("对不起转换失败!");
21 }
22 Console.ReadKey();
23 }
24 }



posted on 2012-03-31 12:29  The Soul  阅读(140)  评论(0编辑  收藏  举报