C# 参数

WBOY
发布: 2024-09-03 15:19:00
原创
184 人浏览过

Params 是 C# 中非常重要的关键字。当我们想要将参数的数量作为变量给出时,我们使用 param 。因此当开发人员不知道将使用的参数数量时使用它。 C# Params 关键字之后,函数中不允许添加任何其他参数。如果我们不传递任何参数,那么 params 的长度将保持为零。我们可以发送逗号分隔的值或数组。

关键字:参数

using System;
namespace Examples {
class Test {
// function containing params parameters
public static int Addittion(params int[] ListNumbers)
{
int total = 0;
// foreach loop
foreach(int i in ListNumbers)
{
total += i;
}
return total;
}
// Driver Code
static void Main(string[] args)
{
// Calling function by passing 5
// arguments as follows
int y = Addittion (12,13,10,15,56);
// Displaying result
Console.WriteLine(y);
}
}
}
登录后复制

输出:

C# 参数

带有参数关键字:

static public int add(params int[] args)
{
int add1 = 0;
foreach (var item in args)
add1= add1+item + 2;
return add1;
}
登录后复制

无参数关键字:

static public int add(int[] args)
{
int add = 0;
foreach (var item in args)
add1 = add1+item + 2;
return add1;
}
登录后复制

有了param,我们可以调用add(1,4,5)这样的方法,但是没有param,我们就不能调用。

C# 参数示例

以下示例展示了如何在 C# 中实现参数。

示例#1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public void Show(params int[] num) // Params Paramater
{
for (int a = 0; a < num.Length; a++)
{
Console.WriteLine(num[a]);
}
}
static void Main(string[] args) // main function
{
Demo program = new Demo();  // Creating Object
program.Show(20, 4, 64, 3, 20, 2, 14);  // Passing arguments of variable length
Console.ReadLine();
}
}
}
登录后复制

在上面的示例中,使用了 param 关键字,允许任何类型和类型数量。然后,在创建对象后,我们传递几个参数来显示。

输出:

C# 参数

示例#2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public void Show(params object[] val) // Params Paramater
{
for (int a = 0; a < val.Length; a++)
{
Console.WriteLine(val[a]);
}
}
static void Main(string[] args) // main method
{
Demo program = new Demo(); // Creating Object
program.Show("Javascript", "Dotnet", 11, 10.50, "Param", 'h',"Example"); // Passing arguments of variable length
Console.ReadLine();
}
}
}
登录后复制

在上面的示例中,使用了 param 关键字,允许任何类型和类型数量。创建对象后,我们传递几个参数来显示。在这里您可以看到不同数据类型的参数。

输出:

C# 参数

示例#3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public static int Addition(params int[] num) // params parameter
{
int add = 0;
// foreach loop
foreach (int a in num)
{
add += a;
}
return add;
}
static void Main(string[] args)
{
int x = Addition(23, 45, 2, 36, 76);  // call function
// Displaying result
Console.WriteLine(x);
Console.ReadLine();
}
}
}
登录后复制

在上面的例子中,使用了一个数组,并且不需要提及数组的大小,因为使用了 param 关键字,这将允许任何类型和数量的参数。  数字将采用以下格式。

[0] 25
[1] 45
[2]  2
[3] 36
[4] 76

输出:

C# 参数

示例#4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
static void Main()
{
// Call params method with five integer type arguments
int cal1 = calParameters(7);
int cal2 = calParameters(11, 23);
int cal3 = calParameters(46, 8, 45);
int cal4 = calParameters(23, 31, 21, 45);
int cal5 = calParameters(12, 12, 54, 76);
// display result of each calculations
Console.WriteLine(cal1);
Console.WriteLine(cal2);
Console.WriteLine(cal3);
Console.WriteLine(cal4);
Console.WriteLine(cal5);
Console.ReadLine();
}
static int calParameters(params int[] values)
{
int sum = 0;
foreach (int value in values)    // foreach loop and sum of integers
{
sum += value;
}
return sum;
}
}
}
登录后复制

上面的示例中使用了 param 关键字的五个参数。所有参数都是整数类型。 foreach 循环用于显示每个参数的总和。

输出:

C# 参数

它与数组有何不同?

public void test(params int[] args) { }
test(); // no compile-time error, args will be empty
登录后复制

但是如果你使用数组:

public void test(int[] args) { }
test(); // error: No overload for 'Foo' takes 0 arguments
登录后复制

所以,如果我们不传递任何参数,那么 params 的长度将保持为零。 Param关键字必须是参数列表的最后一个;否则会报错。

示例:

public void test(params int[] args,int value) { }
登录后复制

不允许此声明。

以上是C# 参数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!