You are reading the article Examples Of C# With And Without Using Params Keyword updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Examples Of C# With And Without Using Params Keyword
Introduction to C# ParamsParams are a very important keyword in the C#. We used param when we wanted to give the number of arguments as the variable. So it is used when the developer does not know the number of parameters that will be used. After the C# Params keyword, no additional param will be allowed in the function. If we do not pass any arguments, then the length of params will remain zero. We can send comma-separated values or arrays.
Start Your Free Software Development Course
Keyword: params
using System; namespace Examples { class Test { public static int Addittion(params int[] ListNumbers) { int total = 0; foreach(int i in ListNumbers) { total += i; } return total; } static void Main(string[] args) { int y = Addittion (12,13,10,15,56); Console.WriteLine(y); } } }Output:
With params Keyword:
static public int add(params int[] args) { int add1 = 0; foreach (var item in args) add1= add1+item + 2; return add1; }Without params Keyword:
static public int add(int[] args) { int add = 0; foreach (var item in args) add1 = add1+item + 2; return add1; }With param, we can call the method like add(1,4,5), but without param, we cant.
Examples of C# ParamsThe following examples show how we can implement params in C#.
Example #1 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Params { class Demo { public void Show(params int[] num) { for (int a = 0; a < num.Length; a++) { Console.WriteLine(num[a]); } } static void Main(string[] args) // main function { Demo program = new Demo(); // Creating Object program.Show(20, 4, 64, 3, 20, 2, 14); // Passing arguments of variable length Console.ReadLine(); } } }In the above example, a param keyword is used, allowing any type and number of types. Then, after creating the object, we pass several arguments to display.
Output:
Example #2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Params { class Demo { public void Show(params object[] val) { for (int a = 0; a < val.Length; a++) { Console.WriteLine(val[a]); } } static void Main(string[] args) { Demo program = new Demo(); program.Show("Javascript", "Dotnet", 11, 10.50, "Param", 'h',"Example"); Console.ReadLine(); } } }Output:
Example #3 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Params { class Demo { public static int Addition(params int[] num) { int add = 0; foreach (int a in num) { add += a; } return add; } static void Main(string[] args) { int x = Addition(23, 45, 2, 36, 76); Console.WriteLine(x); Console.ReadLine(); } } }In the above example, an array is used, and there is no need to mention the size of an array as a param keyword is used, which will allow any type and number of the argument. The numbers will be in the following format.
[4] 76
[0] 25[1] 45[2] 2[3] 36[4] 76
Output:
Example #4 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Params { class Demo { static void Main() { int cal1 = calParameters(7); int cal2 = calParameters(11, 23); int cal3 = calParameters(46, 8, 45); int cal4 = calParameters(23, 31, 21, 45); int cal5 = calParameters(12, 12, 54, 76); Console.WriteLine(cal1); Console.WriteLine(cal2); Console.WriteLine(cal3); Console.WriteLine(cal4); Console.WriteLine(cal5); Console.ReadLine(); } static int calParameters(params int[] values) { int sum = 0; foreach (int value in values) { sum += value; } return sum; } } }Output:
How is it Different From an Array? public void test(params int[] args) { } test();But if You Use an Array:
public void test(int[] args) { } test();So, If we do not pass any arguments, then the length of params will remain zero. Param keyword must be the last in the parameter list; otherwise, it will give an error.
Example:
public void test(params int[] args,int value) { }This declaration is not allowed.
Recommended ArticlesThis is a guide to C# Params. Here we discuss the introduction to C# Params and code implementation with and without using the params keyword. You may also look at the following articles to learn more-
You're reading Examples Of C# With And Without Using Params Keyword
Update the detailed information about Examples Of C# With And Without Using Params Keyword on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!