简体   繁体   English

在C#中接受n个参数的方法

[英]Method that accept n Number of Parameters in C #

I am Developing an windows Application, oftentimes I need to clear the Textboxes whenever the user save the Record or click on clear button. 我正在开发一个Windows应用程序,通常我需要在用户保存记录或单击清除按钮时清除文本框。 Currently i am using this code txtboxname.text=string.empty; 目前我正在使用此代码txtboxname.text = string.empty; for each textbox 对于每个文本框

So can it be possible to write a method that accept the n number of parameter like reading the all the Textboxes in an array and using foreach we can clear them 那么可以编写一个接受n个参数的方法,比如读取数组中的所有Textbox并使用foreach我们可以清除它们

the main requirement is to write a method which accept the n number of parameter ie The parameter size will be unknown. 主要要求是编写一个接受n个参数的方法,即参数大小是未知的。

If any body having idea about how to do this then please help me. 如果有任何机构知道如何做到这一点,请帮助我。 Thanks in Advance. 提前致谢。

With the params keyword. 使用params关键字。

Here is an example: 这是一个例子:

public void MyMethod(params int[] numbers)
{
   for (int i = 0; i < numbers.Length; i++)
   {
       //numbers[i] is one of the parameters
   }
}

Have a look at params 看看params

The params keyword lets you specify a method parameter that takes a variable number of arguments. params关键字允许您指定采用可变数量参数的方法参数。

您可以使用params ,例如Foo(params Bar[] bars)将接受任意数量的Bar实例作为输入。

You could also pass a collection eg a dictionary or List to your method as a parameter. 您还可以将集合(例如字典或列表)作为参数传递给方法。

Eg 例如

public void DoSomething(List<myCustomObject> lst){
    ...
}

是的,您可以将一个TextBoxes TextBox[]数组作为方法的paremter,然后您可以在方法中迭代它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 修改方法以接受不同数量和类型的参数 - Modifying method to accept different number and type of parameters c# function 接受 integer n 并返回可以除数的最小数 1..n - c# function accept integer n and return lowest number that can divide numbers 1..n 通用参数数目可变的C#方法 - c# method with variable number of generic parameters 如何适应 specflow 步骤定义以使用一种方法从 specflow 特征文件中接受不同数量的参数 - How to accommodate specflow step definition to accept deferent number of parameters from specflow feature file with one method 如何编写C#函数以接受可变数量的参数? - How can I code a C# function to accept a variable number of parameters? C#方法可以定义可变数目的对象参数,以及可变数目的整数参数吗? - Can a C# method define a variable number of object parameters, and a variable number on integer parameters? 获取方法的参数值,无论其数量或类型为C# - Get Values of Parameters of a Method Regardless their number or type C# C#如何调用参数数量未知的方法 - C# How to call a method with unknown number of parameters C#如何继承具有不同数量的必需参数的通用方法 - C# how to inherit generic method with differing number of required parameters C#方法重载取决于签名还是参数数量? - C# method overloading depend on signature or number of parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM