Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why do we use the params keyword in C#?
While declaring a method, if you are not sure of the number of arguments passed as a parameter, then use the C# param arrays.
The following is the complete example to learn how to implement param in C# −
Example
using System;
namespace Program {
class ParamArray {
public int AddElements(params int[] arr) {
int sum = 0;
foreach (int i in arr) {
sum += i;
}
return sum;
}
}
class Demo {
static void Main(string[] args) {
ParamArray app = new ParamArray();
int sum = app.AddElements(300, 250, 350, 600, 120);
Console.WriteLine("The sum is: {0}", sum);
Console.ReadKey();
}
}
}Advertisements