ref 参数与 out 参数
来源:互联网 发布:淘宝客怎么玩才能赚钱 编辑:程序博客网 时间:2024/04/29 22:24
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication2
- {
- class Program
- {
- /**//*
- * ref
- * 通过值传送变量是默认的,也可以迫使值参数通过引用传送给方法。为此,
- * 要使用ref关键字,如果把一个参数传递给方法,且这个方法的输入参数前
- * 带有ref关键字,则该方法对变量所作的任何改变都会影响原来对象的值。
- * 传递给方法之前,必须初始化变量
- *
- *
- * out
- * 把输出值赋给通过引用传递给方法的变量可以使函数从一个例程中输出多个值,
- * 这种情况在C风格的语言中很常见,通常,变量通过引用传递的初值并不重要,
- * 这些值会被函数重写,甚至函数根本没用到过。
- *
- * 如果可以在C#中使用这种约定,就会非常方便。便C#编译器要求变量在被引用
- * 前必须用一个初值进行初始化,这样做是没有必要的,有时甚至会引起混乱。
- * 但有一种方法能够简化C#编译器所坚持的输入参数的初始化。
- *
- * 编译器使用out关键字来初始化。当在方法的输入参数前加上out关键字时,传递
- * 给该方法的变量可以不初始化。该变量通过引用传递,所以在从被调用的方法中
- * 退出后,方法对该变量进行的任何改变保留下来。在调用方法时还需要要使用
- * out关键字,正如在定义的时候一样。
- * */
- static void Main(string[] args)
- {
- int i=0;
- Console.WriteLine("Original value of i:/t" + i);
- Console.WriteLine("now calling a common function");
- CommonFunctin(i);
- Console.WriteLine("now the value of i:/t"+i);
- //as out para,has no value initialize
- Console.WriteLine("now calling a outfunction");
- OutFunction(out i);
- Console.WriteLine("now the value of i:/t" + i);
- Console.ReadLine();
- //as ref para,has value before
- Console.WriteLine("now calling a reffunction");
- RefFunction(ref i);
- Console.WriteLine("now the value of i:/t" + i);
- Console.ReadLine();
- }
- static void OutFunction(out int i)
- {
- i = 100;
- }
- static void RefFunction(ref int i)
- {
- i = 200;
- }
- static void CommonFunctin(int i)
- {
- i = 999;
- }
- }
- }
- ref 与 out 参数
- ref 参数与 out 参数
- ref 参数与 out 参数
- ref和out参数
- out 和ref参数
- 参数关键字out,ref
- ref out 参数传递
- Out和Ref参数
- params out ref 参数
- ref参数和out参数
- c#之方法的out参数与ref参数
- 使用ref和out参数
- 方法参数(params,ref,out)
- 方法参数:ref out params
- 使用ref和out参数
- params,ref,out 参数问题
- C#中Ref/Out参数
- [C#]浅析ref、out参数
- 防止闪烁的ListView显示方案
- 串口通信
- INI文件操作(WINAPI)
- C#中使用哈希表Hashtable
- C#中Vista风格的进度条
- ref 参数与 out 参数
- 手机实现翻屏
- 2008SD2C大会见闻(一)——技术天下势
- 如何注册 CPL 文件,使得可以在控制面板中显示
- JUnit入门
- 《Joomla!建站步步通》最新进展
- spartan 3 starter kit SRAM&emc 使用
- 数学符号表-英文读法,含义,用法
- 利用存储过程进行分页