What are parameter/param arrays in C#?

CsharpProgrammingServer Side Programming

<p>C# param arrays help if you are not sure of the number of arguments passed as a parameter, while declaring a method.</p><p>The following is the syntax through which you can use the params keyword &minus;</p><pre class="result notranslate">public int function_name(params int[] variable_name) {}</pre><p>Here us an example to learn about param arrays in C# &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/WMbLGM" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">using System; namespace Program { &nbsp; &nbsp;class ParamArray { &nbsp; &nbsp; &nbsp; public int AddElements(params int[] arr) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int sum = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach (int i in arr) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += i; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return sum; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;class Demo { &nbsp; &nbsp; &nbsp; static void Main(string[] args) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ParamArray app = new ParamArray(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int sum = app.AddElements(300, 250, 350, 600, 120); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(&quot;The sum is: {0}&quot;, sum); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.ReadKey(); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">The sum is: 1620</pre>
Updated on 20-Jun-2020 14:46:23