DEV Community

Brandon Weaver
Brandon Weaver

Posted on

C# 9 - New Features

C# 9 is here, and there are quite a few interesting changes worth talking about.

I'm going to go over a few of the new features which I find most useful, however, there are many other changes which you may want to look into here.

1. The init only setter

One of the first new features which caught my attention was the init keyword. The init keyword can be used in place of the set keyword to prevent the manipulation of a property post-instantiation. Of course, we we're always able to use the readonly keyword in conjunction with a constructor to implement this behavior, but I genuinely believe that this is a better approach. This way, we have the option to use the default constructor, and set the property using initializer syntax.

For example...

class Account
{
    public int Id { get; init; }

    public Account(int id) => this.Id = id;
}

...

Account acc1 = new Account(1); // Works!
Account acc2 = new Account() { Id = 2 }; // Works!

acc1.Id = 2 // Error!
Enter fullscreen mode Exit fullscreen mode

2. Object instantiation

Another awesome new feature introduced in C# 9 is shorthand instantiation.

For example, we can now instantiate objects using the following syntax.

Account acc3 = new (3);
Account acc4 = new () { Id = 4 };
Enter fullscreen mode Exit fullscreen mode

As someone who was never a big fan of instantiating objects with the var keyword, but also disliked the redundancy of traditional instatiation, this is great. I'm not sure I'll instantly switch to this syntax, but it's nice to have the option.

3. Pattern matching enhancements

Finally, I want to talk a little about the pattern matching enhancements. I'll try to provide a few examples and explain what I understand along the way.

First, we have a few new keywords...

if (someInt is not 0 and < 10 or > 20) DoSomething();
Enter fullscreen mode Exit fullscreen mode

I'm not really sure how to feel about this. Again, it's nice to have options, but this feels strange for some reason.

The equivalent being as follows...

if (someInt != 0 && someInt < 10 || someInt > 20) DoSomething();
Enter fullscreen mode Exit fullscreen mode

I personally prefer the traditional syntax here, but for simple expressions, the new syntax might be preferable; that's just my opinion.

Now, an aspect of these enhancements which I am very intrigued with is the ability to use >, <, >=, and <= in switch statements. I generally avoid using switch statements, as there are typically better solutions, but these improvements may convince me to revisit them.

int input = int.Parse(Console.ReadLine());

string result = input switch
{
    > 0 => "greater than 0.",
    0 => "0.",
    _ => "less than 0."
};

Console.WriteLine($"Your input is {result}");
Enter fullscreen mode Exit fullscreen mode

Again, I'm not sure that I'll instantly begin utilizing this approach, but I do think it is an overall improvement to the traditional method.

These were some of the new features introduced in C# 9 which captivated me, and I look forward to learning more about the changes over the ensuing months.

Top comments (0)