Sponsored Ad

Monday, January 17, 2011

How to Pass and Handle Variable Number of Arguments in C#

This example will help you to pass the variable number of arguments in C# and handle them. Please note that you can fix the parameters before the params keyword and keep the rest of arguments variable. It is very useful when you do not know the length and type of parameters.

How to Pass and Handle Variable Number of Arguments in C#

 

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DisplayArgs("Variabal Arguments Example 1: ", 44.22f);
            DisplayArgs("Variabal Arguments Example 2: ", "Arg2", 100);  

            Console.ReadLine();
          }

        static void DisplayArgs(params object[] myArg)
        {
            foreach (object oArg in myArg)
            {
                Console.WriteLine(oArg);
            }
        }       
    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers