c#中out与ref的用法与区别
来源:互联网 发布:js模拟ios select 编辑:程序博客网 时间:2024/05/18 02:22
Out
表示这个变量要回传值,最简单的应用是除法,比如你需要一个除法方法,同时得到余数和商,但是普通的方法只能得到一个返回值,这个时候就可以使用Out参数,把另一个值返回。
比如,你定义了一个方法int a(int b,out int c),它除了能得到返回值外,还可以在方法里对C进行赋值,这样你就可以使用C的值了。
c#中out与ref的用法与区别
1、out必须在函数体内初始化,在外面初始化没意义。也就是说,out型的参数在函数体内不能得到外面传进来的初始值。
2、ref必段在函数体外初始化。
3、两都在函数体的任何修改都将影响到外面。
例:
using System;
namespace ConsoleApplication1
{
class C
{
public static void reffun(ref string str)
{
str += " fun";
}
public static void outfun(out string str)
{
str = "test"; //必须在函数体内初始
str += " fun";
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
string test1 = "test";
string test2; //没有初始
C.reffun( ref test1 ); //正确
C.reffun( ref test2 ); //错误,没有赋值使用了test2
C.outfun( out test1 ); //正确,但值test传进去
C.outfun( out test2 ); //正确
Console.Read();
}
}
}
- c#中out与ref的用法与区别
- c#中out与ref的用法与区别
- c#中out与ref的用法与区别
- c#中out与ref的用法与区别
- C# 中out, ref和params的用法与区别
- c#中out、ref和params的用法与区别
- c#中out和ref的用法与区别
- c#中out、ref和params的用法与区别
- c#中out、ref和params的用法与区别
- c#中out、ref和params的用法与区别
- C#中ref与out的区别
- C# 中 ref 与 out 的区别
- c#中ref与out的区别
- c#中Ref与Out的区别
- C#中ref与out区别
- C# 中 ref 与 out 区别
- C# 中, --------------------- ref 与 out 区别
- c#中ref与out区别
- 10个寓言故事
- 终于看完传智播客的java邮件开发了
- 3月7日处理ADSL故障的工作日志
- 第一次做自己
- DataGridView相关知识[整理]
- c#中out与ref的用法与区别
- 2010-3-8 scull代码阅读 编译 测试
- 送给美女们
- 数据库知识浅谈
- 华为硬件面试题
- 找些不错的sql面试题(1)
- C#中ToString格式大全【整理】
- String类的split方法
- 在fedora10下面打开telnet服务