C#.NET函数中可变参数个数的问题

特兰 2010-05-04 10:53:27
public void DoUpdate(string sqlStr, params OracleParameter[] parms)

如上的函数定义,如果把红色params的有无决定了下面调用的成功与否。

Pub.db.DoUpdate(sqlStr, model, machine, feeder, kp1, kp2, supplier, qty, feedertype);

刚开始学c#,看了几本书也没有提过这种可变参数的方法,这是不是.net特有的格式,有没有相关的资料
给俺仔细研究一下。


谢谢!
...全文
251 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
捷哥1999 2010-05-04
  • 打赏
  • 举报
回复
直接查MSDN,用params 查找,有params 关键字(c#)一文,看看就知道了。
真巧。我和海绵宝宝同时用了MSDN的。
捷哥1999 2010-05-04
  • 打赏
  • 举报
回复
params 关键字可以指定在参数数目可变处采用参数的方法参数。

在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。

// cs_params.cs
using System;
public class MyClass
{

public static void UseParams(params int[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

public static void UseParams2(params object[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test");

// An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
输出:
1
2
3

1
a
test

10
11
12

lpingz 2010-05-04
  • 打赏
  • 举报
回复
params 关键字可以指定在参数数目可变处采用参数的方法参数。

在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。


using System;
public class MyClass
{

public static void UseParams(params int[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

public static void UseParams2(params object[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test");

// An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧