Variable parameters (Varargs) in C#

WBOY
Release: 2023-09-09 12:57:02
forward
1170 people have browsed it

C# 中的变量参数 (Varargs)

Use the param keyword in C# to get variable parameters.

Let’s look at an example of multiplying integers. We use the params keyword to accept any number of int values -

static int Multiply(params int[] b)
Copy after login

The above code allows us to find the numerical multiplication of one or two int values. The following calls the same function with multiple values -

int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10);
Copy after login

Let’s see the complete code to understand how variable parameters 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; } }
Copy after login

The above is the detailed content of Variable parameters (Varargs) in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!