Tuesday, May 1, 2012

How to : Variable Prameters in c# function.

 

We can use “params” to enable a method to accept variable number of parameters. For using this we can send comma parameter list as somefunction(1,2,3,1,3,4) to the method.

1 public class VariableParamClass
2 {
3 //Variable Int Paramenter List
4 public static void VariableParms(params int[] parameterList)
5 {
6 for (int i = 0; i < parameterList.Length; i++)
7 {
8 Console.Write(parameterList[i] + " ");
9 }
10 Console.WriteLine();
11 }
12
13 //Variable Object Parameter List
14 public static void objectVariableParamFunc(params object[] parameterList)
15 {
16 for (int i = 0; i < parameterList.Length; i++)
17 {
18 Console.Write(parameterList[i] + " ");
19 }
20 Console.WriteLine();
21 }
22
23 static void Main()
24 {
25 VariableParms(1, 2, 3, 4);
26 objectVariableParamFunc(1, 'a', "test");
27 objectVariableParamFunc();
28 int[] myIntArray = { 5, 6, 7, 8, 9 };
29 VariableParms(myIntArray);
30 object[] myObjArray = { 2, 'b', "test", "again" };
31 objectVariableParamFunc(myObjArray);
32 objectVariableParamFunc(myIntArray);
33 }
34 }

Check out this video form more understanding :


param in c#

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...