Modern C# – Part 1

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.

Unknown's avatar

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in C#, C# 12, Essentials. Bookmark the permalink.

654 Responses to Modern C# – Part 1

  1. In the great scheme of things you receive a B- for effort. Where exactly you lost us was on all the facts. You know, as the maxim goes, the devil is in the details… And it could not be more accurate right here. Having said that, permit me inform you precisely what did do the job. Your authoring is certainly extremely persuasive and this is possibly the reason why I am taking an effort to opine. I do not make it a regular habit of doing that. Secondly, whilst I can certainly notice the leaps in reasoning you come up with, I am definitely not sure of just how you appear to connect your details which in turn produce the conclusion. For now I shall yield to your issue however hope in the future you connect your facts much better.

  2. 67bet1's avatar 67bet1 says:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something informative to read?

  3. Thank you for every other magnificent article. Where else could anyone get that kind of information in such a perfect approach of writing? I’ve a presentation subsequent week, and I am at the search for such information.

  4. I am continuously browsing online for posts that can aid me. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *