Introduction to .NET 7 Preview 4

.Net Tutorials

With the latest preview of .NET 7 (preview 4 released in May 2022), comes a lot of exciting new features and changes. We will be discussing some of these important changes and new features in today’s tutorial and .NET preview coverage.

Let us get right into it.

Read: Project Management Software for .NET Developers

What is New in .NET 7?

Below we will discuss some of the new changes that have been announced for .NET 7 in Preview 4. This will include additions to time-based structures, feature enhancements, and a new TAR assembly and API.

Microseconds and Nanoseconds in .NET

Microseconds and nanoseconds have been added to the DateTime, TimeStamp, TimeOnly, and DateTimeOffset structures.

By adding these to the above-mentioned structures, .NET developers can save a lot of time by not having to perform calculations and computations via the Ticks property of the DateTime, TimeStamp, TimeOnly, and DateTimeOffset structures, as a single Tick (with the Ticks property) is only 100 nano seconds. Here is a short code example of how microseconds and nanoseconds work in .NET 7:

namespace System {
    public struct DateTime {
        public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);
        public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.DateTimeKind kind);
        public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar);
        public int Microsecond { get; }
        public int Nanosecond { get; }
        public DateTime AddMicroseconds(double value);
    }
    public struct DateTimeOffset {
        public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.TimeSpan offset);
        public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.TimeSpan offset, System.Globalization.Calendar calendar);
        public int Microsecond { get; }
        public int Nanosecond { get; }
        public DateTimeOffset AddMicroseconds(double microseconds);
    }
    public struct TimeSpan {
        public const long TicksPerMicrosecond = 10L;
        public const long NanosecondsPerTick = 100L;
        public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds, int microseconds);
        public int Microseconds { get; }
        public int Nanoseconds { get; }
        public double TotalMicroseconds { get; }
        public double TotalNanoseconds { get; }
        public static TimeSpan FromMicroseconds(double microseconds);
    }
    public struct TimeOnly {
        public TimeOnly(int hour, int minute, int second, int millisecond, int microsecond);
        public int Microsecond { get; }
        public int Nanosecond { get; }
    }
}

Read: 7 Best C# IDEs

System.Text.RegularExpressions Enhancements in .NET

Some notable enhancements included in System.Text.RegularExpressions include:

  • Regex.Count(ReadOnlySpan input): Regex.Count searches the provided string for all occurrences of a given regular expression and returns the number of matches in the supplied string.
  • Regex.IsMatch(ReadOnlySpan input): Returns True if the regular expression finds a match in the input span, and False when it does not.
  • Regex.EnumerateMatches(ReadOnlySpan input): Searches for all occurrences of the provided regular expression and then returns a ValueMatchEnumerator that allows you to iterate through the matches.

Tar APIs in .NET

.NET 7 includes a new Cross-Platform System.Formats.Tar assembly that allows developers to read, write, archive, and extract Tar archives. A short example on how to add a file to an existing Tar archive in .NET follows:

using MemoryStream memStream = new();
using (TarWriter tWriter = new TarWriter(memStream, TarFormat.Pax, leaveOpen: true))
{
    tWriter.WriteEntry(fileName: @"C:Source.txt", entryName: "file.txt");
}

using FileStream stOutput = File.Create(@"C:Destination.tar.gz");
using GZipStream zpOu
tput = new(stOutput, CompressionMode.Compress);
memStream.Seek(0, SeekOrigin.Begin);
memStream.CopyTo(zpOutput);

You can read more about .NET 7 Preview 4 by visiting Microsoft’s official .NET 7 page.

Read: C# Tools for Code Quality

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read