开发者

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. Currently i am using this code txtboxname.text=string.empty; for each textbox

So can it be possible to writ开发者_JAVA技巧e 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

the main requirement is to write a method which accept the n number of parameter i.e The parameter size will be unknown.

If any body having idea about how to do this then please help me. Thanks in Advance.


With the params keyword.

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

The params keyword lets you specify a method parameter that takes a variable number of arguments.


You can use params, e.g. Foo(params Bar[] bars) will accept any number of Bar instances as input.


You could also pass a collection e.g. a dictionary or List to your method as a parameter.

E.g.

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


Yes, you can have an array of TextBoxes TextBox[] as a paremter to your method and you can then iterate over them in your method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜