Posted by: R Manimaran | July 9, 2008

Params -Passing a Variable number of Arguments to a C# Method

Using the params keyword we can pass the variable number of arguments to a method at runtime. There is some restriction using the params keyword

 

  • You can only use the params keyword for one parameter in your method declaration.
  • params must always be the last parameter

 

Example

 

public void ObjectParamsMethod(string strMessage, params object[] p)

    {

      Console.Write(strMessage);

      for (int i = 0; i < p.Length; i++)

        {

           Console.WriteLine(p[i].ToString());

         }

  }

 

//Calling ObjectParamsMethod

ObjectParamsMethod(“Variable Object parameters using param”, “name”, ‘c’, 2008, 12.4);

 

If we know the type of arguments we are going to passing then we can optimize our params as follows. Suppose our argument is going to be variable number of string then we can pass this as

 

public void ObjectParamsMethod(string strMessage, params string[] p)

 

    {

      Console.Write(strMessage);

      for (int i = 0; i < p.Length; i++)

        {

           Console.WriteLine(p[i].ToString());

         }

  }

 //Calling ObjectParamsMethod

ObjectParamsMethod(“Variable string parameters using param”, “name”, “Age”,  

                   “Location”, “ZipCode”, “Contact”);

 

 

 

If it is of int type then

 public void ObjectParamsMethod(string strMessage, params int[] p)

    {

      Console.Write(strMessage);

      for (int i = 0; i < p.Length; i++)

        {

           Console.WriteLine(p[i].ToString());

         }

  }

//Calling ObjectParamsMethod

ObjectParamsMethod(“Variable int parameters using param”,10,12,35,56);

 

 Instead of using comma as a Separated we can pass an array as the parameter.

 string[] MyParamArray ={ “name”, “Age”, “Location”, “ZipCode”“Contact” };

ObjectParamsMethod(“Variable Object parameters using param”,  MyParamArray);

 

 

 

About these ads

Responses

  1. Hi,

    I was reading your article and I would like to appreciate you for making it very simple and understandable. This article gives me a basic idea of passing parameter in methods using C#.Net, It helped me lot. I’ve found another nice post over the internet related to this post which also having wonderful explanation on parameter passing in methods using c#.net. For more information on this post check out this url…

    http://mindstick.com/Articles/bcf933f0-0140-4bc6-a725-4edb2743b988/?Passing%20parameters%20in%20C%20Sharp

    Thank you very much!

  2. Exactly what I needed. Thanks a lot.

  3. Good information. Lucky me I came across your blog by accident (stumbleupon).
    I’ve saved as a favorite for later!

  4. Wow! This blog looks just like my old one! It’s on a entirely
    different subject but it has pretty much the same page layout and design.
    Wonderful choice of colors!

  5. Thank you a lot for sharing this with all folks you really know
    what you’re talking about! Bookmarked. Please additionally talk over with my
    site =). We will have a link exchange contract among us

  6. I like what you guys tend to be up too. This type of clever work and coverage!

    Keep up the very good works guys I’ve incorporated you
    guys to my blogroll.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: