params,out和ref的关键字区别
(一)params---------可以让参数随意变化的关键字
1 staticvoid Main(string[] args)
2 {
3 TestParams(1, 2, 3);
4 TestParams(1, 2, 3, 4, 5, 6);//注意参数随意变换
5
6 TestParams2(1, "a", "b", 12, 52, 16);
7
8 Console.ReadLine();
9 }
10
11 staticvoid TestParams(paramsint[] list)
12 {
13 string str =string.Empty;
14 for (int i =0; i < list.Length; i++)
15 {
16 str = str + list[i] +";";
17 }
18 Console.WriteLine(str);
19 }
20
21 staticvoid TestParams2(paramsobject[] list)
22 {
23 string str =string.Empty;
24 for (int i =0; i < list.Length; i++)
25 {
26 str = str + list[i] +";";
27 }
28 Console.WriteLine(str);
29 }
2 {
3 TestParams(1, 2, 3);
4 TestParams(1, 2, 3, 4, 5, 6);//注意参数随意变换
5
6 TestParams2(1, "a", "b", 12, 52, 16);
7
8 Console.ReadLine();
9 }
10
11 staticvoid TestParams(paramsint[] list)
12 {
13 string str =string.Empty;
14 for (int i =0; i < list.Length; i++)
15 {
16 str = str + list[i] +";";
17 }
18 Console.WriteLine(str);
19 }
20
21 staticvoid TestParams2(paramsobject[] list)
22 {
23 string str =string.Empty;
24 for (int i =0; i < list.Length; i++)
25 {
26 str = str + list[i] +";";
27 }
28 Console.WriteLine(str);
29 }
(二)out------一个引用传递
因为传递的不是值,而是引用,所以对out参数做的任何修改都会反映在该变量当中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | static void Main( string [] args) { string str; //str不必初始化,因为str进入方法后,会清掉自己,使自己变成一个干净的参数, //也因为这个原因,在从方法返回前,必须给str赋值,否则会报错。 TestOutPar( out str); Console.WriteLine(str); Console.ReadLine(); } static void TestOutPar( out string str) { str = "hello!" ; } |
结果显示:"hello!"
如果在TestOutPar方法里注释掉 //str = "hello!";那么程序会报错
用途:
当希望一个方法能够返回多个值时,这个out就变得非常有用。比如在分页方法中,我们需要返回当前页,页数等等数据。
1 staticvoid Main(string[] args)
2 {
3 int pageCount;
4 int pageSize;
5 string result=TestOutPar(out pageCount, out pageSize);
6
7 Console.WriteLine(pageCount);
8 Console.WriteLine(pageSize);
9 Console.WriteLine(result);
10 Console.ReadLine();
11 }
12
13 staticstring TestOutPar(outint pageCount, outint pageSize)
14 {
15 pageCount =100;
16 pageSize =12;
17 return"hello!";
18 }
19
20 结果显示:100,12,hello!
2 {
3 int pageCount;
4 int pageSize;
5 string result=TestOutPar(out pageCount, out pageSize);
6
7 Console.WriteLine(pageCount);
8 Console.WriteLine(pageSize);
9 Console.WriteLine(result);
10 Console.ReadLine();
11 }
12
13 staticstring TestOutPar(outint pageCount, outint pageSize)
14 {
15 pageCount =100;
16 pageSize =12;
17 return"hello!";
18 }
19
20 结果显示:100,12,hello!
(三)ref-----------仅仅是一个地址
1 staticvoid Main(string[] args)
2 {
3 int value =10;//必须初始化,因为value进入方法后还是他自己,也因为这个原因,value可以在方法内不操作
4 TestRefPar(ref value);
5 Console.WriteLine(value);
6 Console.ReadLine();
7 }
8
9 staticstring TestRefPar(refint value)
10 {
11 //value = 100; //value在方法内不操作
12 return"hello!";
13 }
2 {
3 int value =10;//必须初始化,因为value进入方法后还是他自己,也因为这个原因,value可以在方法内不操作
4 TestRefPar(ref value);
5 Console.WriteLine(value);
6 Console.ReadLine();
7 }
8
9 staticstring TestRefPar(refint value)
10 {
11 //value = 100; //value在方法内不操作
12 return"hello!";
13 }
+
(^_^)打个赏喝个咖啡(^_^)

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂