3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#で可変長引数を使うには params

Last updated at Posted at 2012-08-30

よく忘れるのでメモ。

可変長引数 を使用するには、params キーワードを使います。
LinqのSum()メソッドを使っているので、サンプルにあまり意味ありませんが。

SampleParams.cs
using System;
using System.Linq;

namespace SampleParams
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Sum={0}",Sum(1,2,3,4,5));
			Console.ReadKey(true);
		}
		
		public static int Sum(params int [] values)
		{
			return values.Sum();
		}
	}
} 
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?