
- 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
Variable Arguments (Varargs) in C#
Use the param keyword to get the variable arguments in C#.
Let us see an example to multiply integers. We have used params keyword to accept any number of int values −
static int Multiply(params int[] b)
The above allows us to find multiplication of numbers with one as well as two int values. The fllowing calls the same function with multiple values −
int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10);
Let us see the complete code to understand how variable arguments work in C# −
Example
using System; class Program { static void Main() { int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10); Console.WriteLine(mulVal1); Console.WriteLine(mulVal2); } static int Multiply(params int[] b) { int mul =1; foreach (int a in b) { mul = mul*a; } return mul; } }
- Related Articles
- Using Varargs with standard arguments in Java
- Variable-length arguments in Python
- Variable number of arguments in C++
- What are variable arguments in java?
- Demonstrating variable-length arguments in Java
- Command Line and Variable Arguments in Python?
- Variable length arguments for Macros in C
- Variable number of arguments in Lua Programming
- How to Count Variable Numbers of Arguments in C?
- How to use variable-length arguments in a function in Python?
- Overloading Varargs Methods in Java
- Explain about Varargs in java?
- How to use variable number of arguments to function in JavaScript?
- What are the arguments to Tkinter variable trace method callbacks?
- Method Overloading and Ambiguity in Varargs in Java

Advertisements