.NET interview questions: – Benefits of Params Keyword in C#.NET.

This is the one the typical .NET interview questions and also the favorable question of the interviewers.

Params: – The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

So, let’s take a scenario where we can have the use of the Params keyword.

Below is the scenario for the same.

    class Program
        static void Main(string[] args)
            int y = Add(10, 20);
            Console.WriteLine("The Addition of two Numbers is : " + y);
        static int Add(int Num1, int Num2)
            return Num1 + Num2;

In the above code snippet you can see that I have created a function “Add”
which takes two parameters as “Num1” and “Num2” and return the addition of two numbers. The value for the parameters are been passed from the Main and it simply print the output to the screen of the “y” variable.

Now, simply just run your Application and will see the result like below diagram.

The above code snippet seems to be working fine but what if want to pass multiple arguments at a single instance. So, the Params Keyword helps us to achieve the above scenario in much better manner.

Let’s create a simple example to understand the use of Params Keyword in much better manner.

In order to perform practically you just need to follow the following steps.

Step1: – create a simple Console Application for that just open Visual Studio go to >> File >> New >> Project >> Windows >> Select Console Application.

Step2: – Now, simply just add the below Code Snippet in to Program.cs file of your Console Application.

    class Program
        static void Main(string[] args)
//Passed 5 different values.
            int y =  Add(10,20,30,40,50);
            Console.WriteLine("The Addition of  Numbers are : " + y);
//Created a listNumbers variable as declared as Params.
        static int Add(params int[] listNumbers)
            int Total = 0;
            foreach (int i in listNumbers)
                Total = i + Total;
            return Total;

In the above code snippet you can see that I have created a function “Add” with a Variable “listNumbers”
declared using params.

Now, simply just run your Console Application and will see the result like below diagram.

In the above diagram of output you can see the addition of the five numbers using params keyword.

If not understood from the above article, then see the following video on params keyword: –

Get our articles on interview questions and answers for .NET for preparation.


See author’s other Most asked Dotnet interview questions



About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s