c# params 工作原理。

高级语言之所以成为高级语言,编译器付出了很多。params在给我们带来方便的同时,编译器也是贡献不少。

实例代码:

using System;
public class Test
{	
        public static void Main()
	{    
             Console.WriteLine("结果1");	
             say("hello","world");
             Console.WriteLine("结果2");	
             say("aaaa","sssss","ddddd");
            Console.ReadKey();
        }
	public static void say(string str,string str2)	
        {	
             Console.WriteLine("single");
	     Console.WriteLine(str);
             Console.WriteLine(str2);	
        }
	public static void say(params string[] str)
	{
	    Console.WriteLine("params");
	    foreach(string s in str)	    
            {
	        Console.WriteLine(s);
            }
        }
}

说明下目的,

 say("hello","world");

是为了验证在参数同时适配

public static void say(string str,string str2)

public static void say(params string[] str)

的时候,编译器是调用哪个函数。

  say("aaaa","sssss","ddddd");

是为了察看params的运作过程

代码结果:

120015_4z57_2403989.png

分析:

从结果1可看出在同时符合确定变量和可变变量的时候,编译器调用的是确定变量的函数,至于原因等说完params在提。

反编译这段代码。

120438_tIU1_2403989.png

注意参数,这里并没有出现params字样,说明到这个层级是不存在params了,接着看main函数。

.method public hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       79 (0x4f)
  .maxstack  3
  .locals init (string[] V_0)
  IL_0000:  ldstr      bytearray (D3 7E 9C 67 31 00 )                               // .~.g1.
  IL_0005:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000a:  ldstr      "hello"
  IL_000f:  ldstr      "world"
  IL_0014:  call       void Test::say(string,
                                      string)                                                       // 结果1的调用
  IL_0019:  ldstr      bytearray (D3 7E 9C 67 32 00 )                               // .~.g2.
  IL_001e:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0023:  ldc.i4.3
  IL_0024:  newarr     [mscorlib]System.String
  IL_0029:  stloc.0
  IL_002a:  ldloc.0
  IL_002b:  ldc.i4.0
  IL_002c:  ldstr      "aaaa"
  IL_0031:  stelem.ref
  IL_0032:  ldloc.0
  IL_0033:  ldc.i4.1
  IL_0034:  ldstr      "sssss"
  IL_0039:  stelem.ref
  IL_003a:  ldloc.0
  IL_003b:  ldc.i4.2
  IL_003c:  ldstr      "ddddd"
  IL_0041:  stelem.ref
  IL_0042:  ldloc.0
  IL_0043:  call       void Test::say(string[])                                       //结果2的调用
  IL_0048:  call       valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
  IL_004d:  pop
  IL_004e:  ret
} // end of method Test::Main

从结果1可以清楚地看到调用的是确定参数函数,往下的IL_0024构建了一个数组,然后在一个个的把参数推送进数组,最后调用了say(string[] )函数.

结论: 

params的实现只是将单个的参数打包成数组,,调用数组参数函数,事实上你并不能同时声明,say(string[])和say(params string[]),会提示已存在.在这里也能解释为什么在既可变参,也可定参的情况下使用定参的原因了.使用变参需要额外的代码打包参数.























转载于:https://my.oschina.net/hunjixin/blog/517025

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值