25
Jan

Learn How To Program In C# Part 22 – Params Keyword


Hey, everyone. In this video, we’ll be learning about the
reserved keyword params, which is a special keyword on the method that allows us to pretty
much pass in as many values as we desire into an array. I’ve already created a project, and I named
it Params. static void Main(string[] args)
{ Console.WriteLine(GetSum(1,2,3));
} static int GetSum(int number1, int number2,
int number3) {
return number1 + number2 + number3; }
This is the example in which I will not be using the params keyword. I return
the sum. And I’m calling the GetSum() method
in Main. We’re going to pass in 1, 2, and 3. And it’s going to sum them up. So the sum is going to be 6. This will return 6. However, this is limited to only 3 numbers. What if we want to pass in 5 numbers or even
10 numbers? What if we want to pass in only 2 numbers? In this case, we would have to pass in 0 as
the third number. And we can’t even do 5 numbers. Let’s try to use params. static int GetSumImproved(params int[] numbersToSum)
{ int sum=0; foreach (var number in numbersToSum) sum=sum
+ number; return sum;
} static void Main(string[] args)
{ Console.WriteLine(GetSum(1,2,3));
Console.WriteLine(GetSumImproved(1,2,3,4,5,6,7,8,9)); Console.WriteLine(GetSumImproved(1,2));
Console.WriteLine(GetSumImproved(1,2,3)); }
params is the special reserved keyword for the method. int is the type. numbersToSum is the array. You already know how to deal with the arrays. Now all we need to do is go from the beginning
all the way to the end. Let’s have a variable here called sum. And we will return that sum. Now we will loop around our numbersToSum. We have number as the name of the element. It will start from the beginning and go all
the way to the end. Now
we come back to Main and say Console.WriteLine(GetSumImproved(1,2,3,4,5,6,7,8,9)). We can do up to 9. We can even do 1 and 2. That will also work. We can do the same thing that we did with
the GetSum(), the legacy method. Now let’s run the program. The output of the program is:
6 45
3 6
The first one was 6, second one was 45, third one was 3, and the fourth one is the same
as the first one, which is 6. That’s how you use params. I hope you liked this video. If you did, please give it a thumbs up. And if you haven’t subscribed yet, please
do so so that I can keep updating you with the latest content.

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

8 Comments

  • bobyy31 says:

    Thank you!This is by far the best c# tutorial that I found,keep up the good work!

  • Thomas Alsteens says:

    A bit late to the party, but hey, at least I'm there! 😉
    Quick question : you said "you can do up to 9" when using param. Is it a built in stuff, the array in param is just an array of 9 elements? Or was it just for your example?
    I suppose it's the latter, but one never know 😉

    Keep up the good work.

  • Jiwon Kang says:

    I LOVE YOUR TUTORIALS, AVETIS!!! My C# savior!! You saved my time and energy!! Thank you so much for all these tutorials! BY FAR THE BEST C# TUTORIALS I'VE SEEN OVER THE YEARS!!

  • TACV - The Amazing Code-Verse says:

    i started c# with you , got far its been almost an year but your tutorial still helps.. #respect

  • MD Gosoddin says:

    Excellent I Really appreciate ur talent

  • Mike V says:

    Good video

  • Carlgamer Z says:

    The only thing that is lacking to your tutorials is giving a problem based on the topic for us to practice and solve.

  • Igor Go says:

    Спасибо Аветис, хорошо объяснил тему.

Leave a Reply

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