Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

params: practical reference

params: practical reference: params lets the final collection parameter accept a variable number of arguments.

Version: C# language; verified against C# 14 reference

Syntax

params /* grammar-specific form */

params lets the final collection parameter accept a variable number of arguments.

Use it in convenient APIs with a natural list of values.

A call may allocate an array, and passing null differs from passing no arguments.

Only one params parameter is allowed and it must be last; overload resolution may become ambiguous.

The complete program actually uses params; its exact expected stdout is 6.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

Console.WriteLine(Sum(1, 2, 3)); static int Sum(params int[] values) => values.Sum();

Result

6

The complete program actually uses `params`; its exact expected stdout is `6`.

Sources