为什么的方法含有多个参数,为什么调用的时候给一个参数就可以了?
你们调用sqlhelper中的方法,你给的参数是个个数并不是需要全部,而只是部分,或许是我搞错了,可能sqlhelper里面含有对应的参数个数的方法!
为什么的方法含有多个参数,为什么调用的时候给一个参数就可以了?
答案:6 悬赏:30
解决时间 2021-02-16 10:21
- 提问者网友:迷茫庸人
- 2021-02-16 03:11
最佳答案
- 二级知识专家网友:虚伪的现实
- 2021-02-16 03:18
使用 params 关键字声明参数。 一个函数只能有一个动态参数,而且是最后一个参数,限制有点类似于c/c++里的缺省参数。
1、参数类型,必须是数组类型。
2、必须作为函数的最后一个参数。
比如
void Method( param object[] obj );
void Method1( int a, string b, param int[] c );
而下面的定义是错误的。
void Method( param string[] s, int x );
void Method( int a, param float b );
关于调用:
对于形如
void Method( param T[] x )函数,这里的T代表任何数据类型。
其调用可以有如下三种情况:
1、无参数调用: Method();
2、不定参数数目调用:Method( t1, t2, t3, t4 ); 其中t1, t2, t3, t4是T类型的。
3、数组参数调用。 Method( t ); 基中 t 为 T[] 类型。
例子:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main( string[] args )
{
MyMethod();
MyMethod( "fdsf" );
MyMethod( "sdfssdf", "sdf", "adf", "ssssss" );
string[] array = new string[]{
"adfs",
"bdsf"
};
MyMethod( array );
Console.ReadKey();
}
static void MyMethod( params string[] values )
{
if ( values != null )
{
Console.WriteLine( "MyMethod invoked with {0} parameters.", values.Length );
for ( int i = 0; i < values.Length; i ++ )
{
Console.WriteLine( "parameter[{0}] = {1}", i, values[i] );
}
}
else
{
Console.WriteLine( "MyMethod invoked with none parameter." );
}
}
}
}
更多相关的内容,看看MSDN就知道了
1、参数类型,必须是数组类型。
2、必须作为函数的最后一个参数。
比如
void Method( param object[] obj );
void Method1( int a, string b, param int[] c );
而下面的定义是错误的。
void Method( param string[] s, int x );
void Method( int a, param float b );
关于调用:
对于形如
void Method( param T[] x )函数,这里的T代表任何数据类型。
其调用可以有如下三种情况:
1、无参数调用: Method();
2、不定参数数目调用:Method( t1, t2, t3, t4 ); 其中t1, t2, t3, t4是T类型的。
3、数组参数调用。 Method( t ); 基中 t 为 T[] 类型。
例子:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main( string[] args )
{
MyMethod();
MyMethod( "fdsf" );
MyMethod( "sdfssdf", "sdf", "adf", "ssssss" );
string[] array = new string[]{
"adfs",
"bdsf"
};
MyMethod( array );
Console.ReadKey();
}
static void MyMethod( params string[] values )
{
if ( values != null )
{
Console.WriteLine( "MyMethod invoked with {0} parameters.", values.Length );
for ( int i = 0; i < values.Length; i ++ )
{
Console.WriteLine( "parameter[{0}] = {1}", i, values[i] );
}
}
else
{
Console.WriteLine( "MyMethod invoked with none parameter." );
}
}
}
}
更多相关的内容,看看MSDN就知道了
全部回答
- 1楼网友:零负荷的放任
- 2021-02-16 08:32
楼上大侠 你在搞笑吗 不懂别来误导别人行吗
- 2楼网友:woshuo
- 2021-02-16 07:13
因为,没给的取默认的值。 默认值来源于构造函数
- 3楼网友:哭不代表软弱
- 2021-02-16 06:33
重载呀
- 4楼网友:陪伴是最长情的告白
- 2021-02-16 05:31
自己再看清楚源代码吧!肯定用了重载!
- 5楼网友:哥在撩妹请勿打扰
- 2021-02-16 04:35
你说的是脚本语言吧,比如JAVASCRIPT
强类型语言的话是必须给完的,除非方法有多个重载
JS中未声明的参数都是作为undefined处理的
对于补充:
sqlhelper 里卖是函数重载和Parameter类型的参数,
那样的是可以的
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯