
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to pass parameters using param array in a C# method?
While declaring a method, you are not sure of the number of arguments passed as a parameter. C# param arrays (or parameter arrays) come into help at such times.
This is how you can use the param −
public int AddElements(params int[] arr) { }
The following is the complete example −
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 Articles
- How to pass parameters to a method in C#?
- How do we pass parameters by reference in a C# method?
- How do we pass parameters by value in a C# method?
- How do we pass parameters by value in a Java method?
- How to pass pointers as parameters to methods in C#?
- How do we pass an array in a method in C#?
- How to pass reference parameters PHP?
- How to pass optional parameters to a function in Python?
- How to pass keyword parameters to a function in Python?
- How to define param arrays in C#?
- How to pass the parameters in the PowerShell function?
- jQuery Misc param() Method
- Method Parameters in C#
- How to use parameter/param arrays in C#?
- How to pass a 2D array as a parameter in C?

Advertisements