Sunday, January 24, 2010

Variable Methods and use of "params" in C#.

Hi All,

Recently in one of my requirements i require do make some method which will accepts n numbers of parameters, here i am not sure howmany parameters as argument will required.

I come up with solution with "params" keyword. below is a sample example of how i am able to solve it,i have used the console application to show how the "params" keyword will be used.



    6 public static void Main()


    7     {


    8         Console.Clear();


    9         Console.WriteLine("Hello World");


   10         Console.ReadLine();


   11         paramsExample(" vikas", 10, 11, " Aks");


   12     }


   13     public static void paramsExample(params object[] arglist)


   14     {


   15         foreach (object obj in arglist)


   16         {


   17             Console.Write("Hi{0}", obj);


   18         }


   19     }





The output of the above code will simply shows the,

"Hi vikas", "Hi 10","Hi 11","Hi Aks".

Hope you could be easily understand the code and use of "params" keyword.

Happy Coding :)

No comments:

Post a Comment