Implement fixed and variable number parameter in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 3265 red flag

We will see how to implement fixed and variable number of parameter in C#

Implement fixed and variable number parameter in C#

I hope you are already familiar with function in programming and with function parameter too. Here we will see two different approaches to send parameter to function. Basically the tow approaches are

1) Fixed number of parameter.

2) Variable number of parameters.

At first we will see fixed number of parameters in action then we will move towards variable number of parameters.

Fixed number of parameters

Fixed number of parameters is very traditional style to pass parameter to function. When we pass fixed number of argument in time of function call (actual argument) we create same number of parameter list in function definition (formal argument)  .  In below I have given one example.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Test1
{
    class Program
    {
        static void Hello(int a, String b)
        {
            Console.Write(a.ToString() + b);
        }
 
        static void Main(string[] args)
        {
            Hello(100, "sourav kayal");
            Console.ReadLine();
        }
    }
}
 

In this example, we are calling Hello() function with two parameter and in Definition of Hello() function we have created tow formal argument to capture parameter value.

Now the problem is that, if number of argument depend on use input then how we will solve problem?

In this scenario variable length of parameter comes in picture.

 

Variable number of parameter

In c#, to implement variable length parameter we have to use params keyword. And within function definition we will determine how many parameter has supplied during calling time.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Test1
{
    class Program
    {
        public static void UseParams(params int[] list)
        {
            for (int i = 0; i < list.Length; i++)
            {
Console.Write(list[i] + " ");
            }
            Console.WriteLine();
        }
 
        public static void UseParams2(params object[] list)
        {
            for (int i = 0; i< list.Length; i++)
            {
                Console.Write(list[i] + " ");
            }
            Console.WriteLine();
        }
 
 
        static void Main(string[] args)
        {
            UseParams(1, 2, 3, 4);
            UseParams2(1, 'a', "sourav");
          
            UseParams2();
 
            int[] myIntArray = { 5, 6, 7, 8, 9 };
            UseParams(myIntArray);
 
       object[] myObjArray = { 2, 'b',"Sourav", "Kayal"};
            UseParams2(myObjArray);
 
            UseParams2(myIntArray);
        Console.ReadLine();
        }
    }
}
 

In this example first user params function is taking only integer type variable number of parameter and second userparams function can able to take any type variable length argument as we are storing value in Object.

Here is sample output.

 

Conclusion :

We looked into two techniques to implement fixed and variable length argument in C#

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Deviprasads on: 7/16/2013 | Points: 25
Good job!

Login to post response

Comment using Facebook(Author doesn't get notification)