Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method that accept n Number of Parameters in C #

Tags:

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 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

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.

like image 466
Nitish Katare Avatar asked Nov 19 '10 07:11

Nitish Katare


People also ask

How many types of parameters method can have?

Out Parameters. Default Parameters or Optional Arguments (C# 4.0 and above) Dynamic parameter (dynamic keyword). Value parameter or Passing Value Types by Value (normal C# method param are value parameter)

What are named parameters in C#?

Named parameters provides us the relaxation to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. Using named parameters in C#, we can put any parameter in any sequence as long as the name is there.


1 Answers

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    } } 
like image 69
Cheng Chen Avatar answered Oct 20 '22 00:10

Cheng Chen