本文摘自PHP中文网,作者黄舟,侵删。
本文主要讨论params关键字,ref关键字,out关键字。非常不错,具有参考借鉴价值,需要的朋友参考下吧关于这三个关键字之前可以研究一下原本的一些操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System;
using System.Collections.Generic;
using System.Text;
namespace ParamsRefOut
{
class Program
{
static void ChangeValue( int i)
{
i=5;
Console.WriteLine( "The ChangeValue method changed the value " +i.ToString());
}
static void Main( string [] args)
{
int i = 10;
Console.WriteLine( "The value of I is " +i.ToString());
ChangeValue(i);
Console.WriteLine( "The value of I is " + i.ToString());
Console.ReadLine();
}
}
}
|
观察运行结果发现

值并没有被改变,也就是说此时的操作的原理可能也是跟以前C语言的函数操作是一样的

本文主要讨论params关键字,ref关键字,out关键字。
1)params关键字,官方给出的解释为用于方法参数长度不定的情况。有时候不能确定一个方法的方法参数到底有多少个,可以使用params关键字来解决问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using System;
using System.Collections.Generic;
using System.Text;
namespace ParamsRefOut
{
class number
{
public static void UseParams( params int [] list)
{
for ( int i=0;i<list.Length;i++)
{
Console.WriteLine(list[i]);
}
}
static void Main( string [] args)
{
UseParams(1,2,3);
int [] myArray = new int [3] {10,11,12};
UseParams(myArray);
Console.ReadLine();
}
}
}
|
2)ref关键字:使用引用类型参数,在方法中对参数所做的任何更改都将反应在该变量中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System;
using System.Collections.Generic;
using System.Text;
namespace ParamsRefOut
{
class number
{
static void Main()
{
int val = 0;
Method( ref val);
Console.WriteLine(val.ToString());
}
static void Method( ref int i)
{
i = 44;
}
}
}
|
3) out 关键字:out 与ref相似但是out 无需进行初始化。
以上就是具体详解C#中三个关键字(params,Ref,out)的详细内容!
相关阅读 >>
C#动态对象dynamic实现方法和属性动态代码详解
浅谈C#方法的六种参数
C#是什么?有什么用?
文件路径和文件夹路径在C#中使用浏览按钮获得的方法
详细介绍data url生成工具C#版第二版的示例代码
详细介绍C#批量生成随机密码必须包含数字和字母并用加密算法加密的代码案例
浅谈C# 之 hashtable 与 dictionary的代码实例
用C#向word文档插入和隐藏段落的方法介绍
C#之解决百度地图api app sn校验失败问题(代码实例)
C#中抽象类和接口的区别
更多相关阅读请进入《C#》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » 具体详解C#中三个关键字(params,Ref,out)