InsertEmp(int [] a)之间的区别InsertEmp(params int [] a) [英] difference between InsertEmp( int[] a) InsertEmp( params int[] a)

查看:57
本文介绍了InsertEmp(int [] a)之间的区别InsertEmp(params int [] a)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于params方法,我们可以这样传递"insertEmp(1,2,3)"之类的单个元素,但是对于数组,我们不能提供值,对吗?解决方案

这不过是语法糖( http://en .wikipedia.org/wiki/Syntactic_sugar [ ^ ] )允许模仿单个参数而不是数组传递语法.考虑一下:

  int  first =  1 ;
 int  second =  2 ;
 int  third =  3 ;

无效 InsertEmp( int  []值){/*   ... */}

//  ... 

// 您需要执行以下操作:
InsertEmp(  int  [] {第一,第二,第三,}); // 注意,最后一个逗号不是bug;这是允许的,而且非常有用

// ,但您不能这样做:
//  InsertEmp(第一,第二,第三);  



如果添加params,则可以同时执行以下操作:

 无效 InsertEmp(参数  int  []值){/*   ... */}

//  ... 

InsertEmp(  int  [] {第一,第二,第三,});

// 与此完全等效:

InsertEmp(first,second,third); 



—SA


for params method we can pass individal elements like ''insertEmp(1,2,3)'' this way we can ,but for array we cant provide the values, is it right?

解决方案

This is nothing more but a syntactic sugar (http://en.wikipedia.org/wiki/Syntactic_sugar[^]) allowing for the syntax imitating passing of individual parameters instead of the array. Consider this:

int first = 1;
int second = 2;
int third = 3;

void InsertEmp(int[] values) { /* ... */}

//...

// you need to do this:
InsertEmp(new int[] {first, second, third, }); // note, last comma is not a bug; this is allowed and very useful

// but you cannot do this:
//InsertEmp(first, second, third);



If you add params, you can do both:

void InsertEmp(params int[] values) { /* ... */}

//...

InsertEmp(new int[] {first, second, third, });

// is exactly equivalent to this:

InsertEmp(first, second, third);



—SA


这篇关于InsertEmp(int [] a)之间的区别InsertEmp(params int [] a)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆