What's new in .NET 7

The tech giant in Redmond just released the last preview of .NET 7 before it is officially released at the end of this year.
You can download .NET 7 Preview 7 for Windows, Linux, and macOS.
Unlike .NET 6, .NET 7 is a Short Term Support (STS) release, which means it will go along with 18 months of free support & patch from the release date. For more information about the .NET support policy, check out this page: https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core.

Let's quickly review what's new in the latest version of the .NET framework.
Simplified ordering with System.LINQ
System.Linq
now has the methods Order
and OrderDescending
, which are there to order an IEnumerable
according to T
.
Before:
var data = new[] { 2, 1, 3 };
var sorted = data.OrderBy(e => e);
var sortedDesc = data.OrderByDescending(e => e);
After:
var data = new[] { 2, 1, 3 };
var sorted = data.Order();
var sortedDesc = data.OrderByDescending();
Support for Unix file modes
Previously, .NET had no built-in support for getting and setting Unix file permissions, which control which users can read, write, and execute files and directories. P/Invoking manually to syscalls isn’t always easy because some are exposed differently on different distros. For example, on Ubuntu, you may have to pinvoke to __xstat
, on RedHat to stat
, and so on. This makes a first-class .NET API important.
In Preview 7, we introduced a new enum:
public enum UnixFileMode
{
None,
OtherExecute, OtherWrite, OtherRead,
GroupExecute, GroupWrite, GroupRead,
UserExecute, UserWrite, UserRead,
...
}
Sample codes:
// Create a new directory with specific permissions
Directory.CreateDirectory("myDirectory", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
// Create a new file with specific permissions
FileStreamOptions options = new()
{
Access = FileAccess.Write,
Mode = FileMode.Create,
UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite,
};
using FileStream myFile = new FileStream("myFile", options);
// Get the mode of an existing file
UnixFileMode mode = File.GetUnixFileMode("myFile");
// Set the mode of an existing file
File.SetUnixFileMode("myFile", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
ClientWebSocket upgrade response details
ClientWebSocket
previously did not provide any details about upgrade response. However, the information about response headers and status codes might be important in both failure and success scenarios.
In case of failure, the status code can help to distinguish between retriable and non-retriable errors (server doesn’t support web sockets at all vs. just a tiny transient error). Headers might also contain additional information on how to handle the situation.
The headers are also helpful even in case of a successful web socket connect, e.g., they can contain a token tied to a session, some info related to the subprotocol version, or the server can go down soon, etc.
ClientWebSocket ws = new();
ws.Options.CollectHttpResponseDetails = true;
try
{
await ws.ConnectAsync(uri, default);
// success scenario
ProcessSuccess(ws.HttpResponseHeaders);
ws.HttpResponseHeaders = null; // clean up (if needed)
}
catch (WebSocketException)
{
// failure scenario
if (ws.HttpStatusCode != null)
{
ProcessFailure(ws.HttpStatusCode, ws.HttpResponseHeaders);
}
}
To access the full list of .NET 7 breaking changes, you can access this document from Microsoft: https://docs.microsoft.com/en-us/dotnet/core/compatibility/7.0.
Integrated Development Environment
Currently, .NET 7 preview 7 is shipped with Visual Studio 17.4 Preview 1, if you want to have early access to this framework, do upgrade your IDE and give it a try.
Reference: https://docs.microsoft.com/en-us/visualstudio/releases/2022/release-notes-preview