Handling Null Reference in C# 10 with Record Types and Optional Properties
I'm writing unit tests and I'm working with C# 10 and using record types to represent some domain models. However, I'm running into a `NullReferenceException` when trying to create instances of these records, especially when some properties are optional. I'm defining a record `User` like this: ```csharp public record User(int Id, string Name, string? Email = null); ``` When I attempt to create a new `User` instance without providing an email: ```csharp var user = new User(1, "John Doe"); ``` I expect the `Email` property to be null. However, in some parts of my code, I access `Email` without checking if it is null, which leads to `NullReferenceException` when I try to perform string operations on it. For example: ```csharp if (user.Email.Length > 0) { Console.WriteLine(user.Email); } ``` This results in an exception if `Email` is null. I've considered using the null-conditional operator, but I want to ensure that I handle these optional properties correctly and avoid such runtime errors. Whatβs the best practice to handle optional properties in record types to prevent these kinds of exceptions? Should I always use null checks, or is there a more elegant way to manage this? Any help would be greatly appreciated! I'd be grateful for any help.