params Keyword in C#

Blog - Programming

User Rating: / 2
PoorBest 

The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

// cs_params.cs
using System;
public class MyClass
{

public static void UseParams(params int[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list);
}
Console.WriteLine();
}

public static void UseParams2(params object[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list);
}
Console.WriteLine();
}

static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test");

// An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}

Output


1
2
3

1
a
test

10
11
12

(Source: MSDN)



Add this page to your favorite Social Bookmarking websites
Tags:

Add comment


Security code
Refresh

Idioms

  • Every man has his faults (Nhân vô thập toàn)
  • A friend in need is a friend indeed. (Gian nan mới hiểu bạn bè)

Who's online

We have 9 guests online

Location

 38.107.179.219
 38.107.179.219
Search Bot
 unknown unkno

Relax

Site Ranking


Increase your website traffic with Attracta.com

Quick Search

This Day in History

Poll

What is your current operating system?
 

Weather

Newsletter

Copyright © 2012 Thong D. Nguyen. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.