惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
U
Unit 42
WordPress大学
WordPress大学
Y
Y Combinator Blog
罗磊的独立博客
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
V
V2EX
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - hateyoucode

http://tengine.taobao.org/ Mysql只Union用法 比较详细的mysql的几种连接功能分析,只要你看完就能学会的好东西 jq 判断单选是否选中 MSSQL中的bit类型 js 判断textarea 不为空 C# Split 分隔符为字符串及空格的处理 [转]asp.net中时间差的问题 [转]C# Array 数组 及 一些操作 [转]使用c# 模拟网站登录 RegisterStartupScript和RegisterClientScriptBlock的区别 asp.net 动态二维数组 - hateyoucode - 博客园 asp+ajax asp中ajax的中文乱码 - hateyoucode - 博客园 css html span 块状不换行 我们应该明确几个问题 asp.net操作XML PHP Header用于页面跳转要注意的几个问题总结 - hateyoucode - 博客园 关于PHP header函数跳转的问题 - hateyoucode - 博客园 生成xml - hateyoucode - 博客园
[转]C#中out 及 ref 区别
hateyoucode · 2010-12-28 · via 博客园 - hateyoucode

方法参数上的 out 方法参数关键字使方法引用传递到方法的同一个变量。
当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
当希望方法返回多个值时,声明 out 方法非常有用。
使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。
若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。
不必初始化作为 out 参数传递的变量。然而,必须在方法返回之前为 out 参数赋值。
属性不是变量,不能作为 out 参数传递。

方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。
当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。
ref 参数的值被传递到 ref 参数。 传递到 ref 参数的参数必须最先初始化。
将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。
属性不是变量,不能作为 ref 参数传递。

两者都是按地址传递的,使用后都将改变原来的数值。
ref可以把参数的数值传递进函数,但是out是要把参数清空
就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所以你必须初始化一次。
两个的区别:ref是有进有出,out是只出不进。

代码实例如下:

1 namespace TestOutAndRef
2 {
3     class TestApp
4      {
5
6  static void outTest(out int x, out int y)
7  {//离开这个函数前,必须对x和y赋值,否则会报错。
8   //y = x;
9   //上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
10   x = 1;
11   y = 2;
12  }
13  static void refTest(ref int x, ref int y)
14  {
15   x = 1;
16   y = x;
17  }
18
19
20 static public void OutArray(out int[] myArray)
21 {
22      // Initialize the array:
23       myArray = new int[5] { 1, 2, 3, 4, 5 };
24 }
25 public static void FillArray(ref int[] arr)
26 {
27      // Create the array on demand:
28      if (arr == null)
29           arr = new int[10];
30      // Otherwise fill the array:
31       arr[0] = 123;
32       arr[4] = 1024;
33 }
34
35
36  public static void Main()
37  {
38   //out test
39   int a,b;
40   //out使用前,变量可以不赋值
41   outTest(out a, out b);
42   Console.WriteLine("a={0};b={1}",a,b);
43   int c=11,d=22;
44   outTest(out c, out d);
45   Console.WriteLine("c={0};d={1}",c,d);
46
47   //ref test
48   int m,n;
49   //refTest(ref m, ref n);
50   //上面这行会出错,ref使用前,变量必须赋值
51
52   int o=11,p=22;
53   refTest(ref o, ref p);
54   Console.WriteLine("o={0};p={1}",o,p);
55
56
57
58   int[] myArray1; // Initialization is not required
59
60   // Pass the array to the callee using out:
61    OutArray(out myArray1);
62
63   // Display the array elements:
64    Console.WriteLine("Array1 elements are:");
65   for (int i = 0; i < myArray1.Length; i++)
66        Console.WriteLine(myArray1[i]);
67
68   // Initialize the array:
69   int[] myArray = { 1, 2, 3, 4, 5 };
70
71   // Pass the array using ref:
72    FillArray(ref myArray);
73
74   // Display the updated array:
75    Console.WriteLine("Array elements are:");
76   for (int i = 0; i < myArray.Length; i++)
77        Console.WriteLine(myArray[i]);
78 }
79      }
80
81 }