Saturday 27 April 2013

C# Passing multiple parameter to a method with PARAMS

With c# param you can passing comma-separated list of arguments to your "Method".
Here i show an example with SUM method,Sum method accept a comma-separated list of arguments.

Example
    class multiple_ParameterWithParam
    {
         
        public static int SUM(params int[] val) 
        {
            int totalSum = 0;
            for (int i = 0; i < val.Length; i++) 
            {
                totalSum += val[i]; 
            }
            return totalSum;
        }

        static void Main()
        {
            Console.WriteLine("Total Sum (1-3) = {0}", SUM(1, 2, 3));
            Console.WriteLine("Total Sum (1-6) = {0}", SUM(1,2,3,4,5,6));
            Console.ReadLine();
        }
    }


No comments:

Post a Comment