C# 10.0 - Useful features to know
Tags: csharp, beginners, programming, tutorial
Published: 2021-12-03 21:49:28

If you liked what you read feel free to connect with me on [linkedin](https://www.linkedin.com/in/albert-bennett/) or follow me on dev.to :)

Ho, ho, ho merry... something :santa:

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.
<br/>

**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:
```csharp
    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; }
    }
```
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.
```csharp
    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;
    }
```
<br/>

**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:
```csharp
public record UserId(string Id);
```
The change here for C# 10 is that a record can now be declared as a struct as well. See example below:
```csharp
public record struct UserId(string Id);
```
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. 
<br/> 
**Namespace declaration**
This has been simplified in C# 10. 
Normally this is how namespaces were defined in previous versions of C#.
```csharp
namespace testLib
{
    //some class
}
```
In C# 10 it has been simplified to:
```csharp
namespace testLib;

//some data types
```
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.
<br/>

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/
<br/>
The entire list of updates for C# 10 can be found here:
- https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10