This short post will kick off a series covering some of the new features in C#.
One small but much requested feature is the ability to use any kind of collection for params. Previously you had to pass in an array:
public void MyMethod(int firstParam, params string[] otherParams)
{
foreach (var param in otherParams)
{
Console.WriteLine(param);
}
}
But now you can pass in any kind of collection,
public void MyMethod(int firstParam, params List<string> otherParams)
{
foreach (var param in otherParams)
{
Console.WriteLine(param);
}
}
This can save a great deal of fussing, converting your collection to an array and back.
Please note that this is a preview feature. Fortunately, Intellisense will convert your project for you. When you put this in you’ll get the dreaded red squiggly. Click on the light bulb and let it convert your project. Hey! Presto! it works.
We’ll look at a few bigger features in coming blog posts.