DEV Community

Albert Bennett
Albert Bennett

Posted on • Updated on

C# 10.0 - Useful features to know

If you liked what you read feel free to connect with me on linkedin or follow me on dev.to :)

Ho, ho, ho merry... something ๐ŸŽ…

It is that time of year again MS ignite has past by and few new features\ updates have been announced for our favorite programing language C#. Today I'd like to show you how you can use a few of the new features that I have found very useful over the past month or so.

Field Initializers in Structs
I am really happy about this update to struct instantiation.
In the past if you wanted to set a default value for a property in a struct you might need to do something like this. See example below:

    public class TestClass
    {
        public void CreateThing()
        {
            var thing = new Thing("Name");
        }
    }

    public struct Thing
    {
        public Thing(string name)
        {
            Name = name;
            Id = 5;
        }

        public string Name { get; set; }
        public int Id { get; set; }
    }
Enter fullscreen mode Exit fullscreen mode

Notice how we can't set the value of a property at property declaration nor can we set the value inside of a parameterless constructor as both would result in an error. This is why we needed to create a constructor with a parameter to set the default value for the 'Id' property.
Now in C# 10 you can do the same in fewer lines of code and no need for a new constructor.

    public class TestClass
    {
        public void CreateThing()
        {
            var thing = new Thing();
        }
    }

    public struct Thing
    {
        public string Name { get; init; } = "name";
        public int Id { get; init; } = 5;
    }
Enter fullscreen mode Exit fullscreen mode

Record Structs
So, records have been around since C# 9. A record is essentially an immutable reference type that is used when your data needs to be both immutable and encapsulate some kind of complex data value. It has all of the properties and behaviors of a class. See example of how to define a record below:

public record UserId(string Id);
Enter fullscreen mode Exit fullscreen mode

The change here for C# 10 is that a record can now be declared as a struct as well. See example below:

public record struct UserId(string Id);
Enter fullscreen mode Exit fullscreen mode

The advantage here is that the record can now contain all of the same properties and behaviors that a struct has all in one line of code.


Namespace declaration
This has been simplified in C# 10.
Normally this is how namespaces were defined in previous versions of C#.

namespace testLib
{
    //some class
}
Enter fullscreen mode Exit fullscreen mode

In C# 10 it has been simplified to:

namespace testLib;

//some data types
Enter fullscreen mode Exit fullscreen mode

In my opinion it really makes code files look a lot cleaner by removing a set of braces and a level of indentation. It's not something that comes up so often but... it's nice when it does.

Thanks for reading my post on C# 10.0 features that I think are useful.
I wrote this post as part of the C# Advent Calendar event for day 5. Feel free to check out the other blog post already revealed during this event: https://csadvent.christmas/


The entire list of updates for C# 10 can be found here:

Discussion (0)