Welcome to the 67th edition of The Catch Block!
In this edition: let's check out a bunch of official and semi-official features coming in C# 10!
Plus: a new Visual Studio 2022 preview; a new .NET 6 preview; musings on GitHub Copilot; and improving our Blazor UI responsiveness.
Check Out Upcoming Features of C# 10!
C# 10 is coming out soon, and included with it are a few changes that I thought my dear readers should know about. A few of these are officially included (that is, included on the Microsoft docs for C# 10), and some of them are not official yet but are pretty much guaranteed to make the cut; we'll call them "semi-official".
Here's five of the coolest upcoming changes:
Official - Const Interpolated Strings
The coolest "official" change is the ability to make const string
objects using interpolation (the $
operator) if all the interpolated strings are const
themselves.
public const string Name = "John Smith";
public const string Title = "Dr.";
public const string Greeting = $"Hello {Title} {Name}!";
Semi-Official - Null Parameter Checking
C# 10 will probably include the ability to check if a parameter to a method is null, using the !!
operator:
public void MyMethod(string param1, SomeClass myClass!!)
{
//...
}
Any time this method is called, if the value of myClass
is null, C# will automatically through an instance of ArgumentNullException
.
Semi-Official - field
Keyword
In C# right now, we often need to declare properties in a class with a backing private
field. Let's pretend we have a User
class with a Name
field, and there's a restriction on the Name
so that it cannot be blank or only whitespace:
public class User
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if(string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Name cannot be blank.");
_name = value;
}
}
}
C# 10 introduces the new field
keyword, which allows us to simplify this code to the following:
public class User
{
private string _name;
public string Name
{
get;
set
{
if(string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Name cannot be blank.");
field = value;
}
}
}
We don't need the get
accessor code anymore, because the C# compiler automatically includes it when we use the field
keyword.