

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to use parameter/param arrays in C#?
While declaring a method, you are not sure of the number of arguments passed as a parameter. C# param arrays can let you know about this.
This is how you can use the param −
public int AddElements(params int[] arr) { }
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(); } } }
Output
The sum is: 1620
- Related Questions & Answers
- What are parameter/param arrays in C#?
- How to define param arrays in C#?
- How to use the ErrorAction parameter in PowerShell?
- How to use an alias() for the parameter in PowerShell?
- How to use -For parameter in Restart-Computer cmdlet in PowerShell?
- jQuery Misc param() Method
- How to use the -recursive parameter in Get-ChildItem using PowerShell?
- How to use –Wait parameter in Restart-Computer cmdlet in PowerShell?
- How to use –Timeout parameter in Restart-Computer cmdlet in PowerShell?
- How to pass parameters using param array in a C# method?
- How do I use arrays in C++?
- How to use arrays as filters by querying subdocuments in MongoDB?
- How do we use multi-dimensional arrays in C#?
- Use of parameter basepath_logvolumes_es in global configuration file in SAP HANA
- How to use an OUT parameter / read data with SELECT from table in a MySQL procedure?
Advertisements