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.
using System;
namespace TestParam
{
class Program
{
static void Main(string[] args)
{
TestCase _testCase = new TestCase();
_testCase.TestMethod(1, 2, 3);
_testCase.TestMethod(1, 2, 3,4,5);
_testCase.TestMethod("a", "abc", "abcd");
_testCase.TestMethod('a', 'b', 'c');
_testCase.TestMethod(1,'a', "abc", "abcd");
Console.Read();
}
}
class TestCase
{
public void TestMethod(params object[] input)
{
foreach (var item in input)
{
Console.WriteLine(item);
}
Console.WriteLine(Environment.NewLine);
}
}
}
No comments:
Post a Comment