c# (Csharp) :- What is the use of params keyword ? (c# interview question)

Posted By on May 5, 2019

Now one of the keywords which is talked about is the Params keyword now what exactly the params keyword now in order to understand the params keyword lets try to understand the small sample example which i am flashing on the screen at this moment in the above example you can see that we have a simple add function and this add function is actually called from inside the static void main and currently this add function actually takes two numbers num1 and num2 and it basically gives back the addition of these two numbers. now let say if you want to add three number so what will do is you will go as enthusiastic developer add one more input variable here sinc int num3 and then you will go and say OK add num3 and then probably you will go here and say OK here the extra parameter and this add function will add 12 + 14 + 43 but definitely this is not what you call a permanent solution what if let say the demand for increases and probably we would like send unlimited number of numbers to this add function in other words what you would like is this add function or the add method what you have can take variable input parameters so how do you go about solving this problem so for those kind of situation we have something called as the params keyword so in other to create variable input parameters for you can do it you can say params and you are expecting variable input parameter which are of type integer and you will say list of Numbers now we this params keyword what happens is what is says that basically now if you add function is called by what ever number of input parameters these will all come into this collection called as list numbers and what you can do is you can go and Browse to this list number and do your addition and return the total to the main function so what we can do is you can say OK for each int i in ListNumbers go and do the addition and what i will do i will create a temporary variable here called as total so that we can go and add the running total into this variable i am saying here total = i + total ; and then we will return back total to the static void main and we build this right so will succeeded nice so let me just go and put a debug point here and lets go and execute this programme here so that it goes and let me just step in now if you go here to the list number you can see what ever input parameters you given here in this add function in to this add function as arguments you can see all those four values i will say five value because of the zero index is in the collection and then we are going and browsing through the collection and then we have doing the total here and if you come out you find that the y variable has the total that is 12 + 14 +43 + 34 +56 = 159 in the same way if you go and put probably one more parameter it doesn't matter now why because this input is now a params so if i just go and say here must be 100 if i again run this programme now let me again go and step in into the add function and now if you go to your list here you can see that is one more entry here in the list number collection called as 100 so in other words what we have achieved here now is basically irrespective your how much parameter comes to your add function you will be able to receive it so in other words the params keyword help you to helps your function or the methods to create a dynamic input parameters so in other words wherever you are having constraint same that is my input parameter are not fixed then you can go and define the Params keyword and datatype so i hope that you have enjoyed this so in this section we saw the what is the use of the Params keyword Now what ever video you have seen right is just gleams of what we have done so in case you are interested in our video package you can go to our site that is www.questpond.com you can call on this no +919967590707 and you can ask the complete DVD package what we have so in this DVD package what we have done is basically we have covered almost every thing what a dot net developer wants So right from basics of ASP.Net objective programming SQL Server to new technology like WCF Silverlight LINQ, Azure, Entity Framework we also have UML, Architecture, Estimation, Project management that is complete Invoicing project end to end which is covered so that you can get a better feel of how to actually Create Project in a systematic manner we have cover server products you know both for Sharepoint 2007 as well as for 2010 we have lot of best practices videos on SQL Server etc So this complete package you can get from www.questpond.com if you interested and you can call on this number +919967590707 and you can ask for the rates its very decent rate we have in the same way as compare to the video we also have one more product with us that is our Interview question books so we have different kind of Interview question books you know right from dot net interview questions to SQL server interview questions Sharepoint Interview questions Wiztalk Interview questions etc so in case you are interested in the books part you can call on these number as per your location so you can see this number on the board at this moment So i hope that you keep enjoying the videos you keep seen our site and i hope that you gain more knowledge Thank you very much …..

Posted by Lewis Heart

This article has 8 comments

  1. Wouldnt it be easier to perform the addition with array.Sum()?

    Here is what I mean,

    public static int Add(params int[] ListNumbers)
    return ListNumbers.Sum();

    Looks much better that way :c and who likes to type more than it is absolutely necessary?

  2. The descriptions on the msdn site are so confusing, this however is not. Great tutorial thanks so much for posting!

  3. shiv koirala you are such a good trainer.After saw your video all clear.Thanks sir for providing such a knowledge


Leave a Reply

Your email address will not be published. Required fields are marked *