.NET 7 Release Candidate 2

C# Programming Guide

Microsoft’s Release Candidate 2 (the final release candidate) for .NET 7 was released on October 11, 2022. In this programming tutorial, we look at some of the features and updates developers can expect.

Read: Best Online Courses for .NET Developers

C# 11 Updates in .NET 7

New features and updates in C# 11 include the following.

Generic Attributes in C#

C# 11 introduces a new generic class, which encapsulates operations that are specific to a particular data type. A small example (from Microsoft) follows:

class BaseNode { }
class BaseNodeGeneric { }
// concrete type
class NodeConcrete : BaseNode { }
//closed constructed type
class NodeClosed : BaseNodeGeneric { }
//open constructed type
class NodeOpen : BaseNodeGeneric { }

An example of a generic class with the base class of System.Attribute in .NET follows:

public class GenericAttribute : Attribute { }

Required Members in C#

A Class and Struct type can make use of the required keyword to ensure that properties and fields must be properly initialized. Required members have a few rules:

  • The required modifier can be applied to fields and properties in Struct and Class types
  • Required members cannot be applied to members of an interface
  • Required members must be initialized
  • Required members must be visible as their containing type
  • Derived classes cannot hide required members that are declared in the base class

Here is a code example showing how to use required members in C#:

public class Course
{
    public required string CourseName { get; init; }
    public required int CourseDuration {get; init; }

    [SetsRequiredMembers]
    public Course(string courseName, int courseDuration)
    {
        this.CourseName = coursetName;
        this.CourseDuration = courseDuration;
    }
    public Course() {}
}

For a more detailed look at some of the new and updated features of C#, please refer to the following link.

Looking to learn C# programming in an online course environment? We have a list of the Best Online Courses for Developers.

.NET Library Updates

.NET libraries keep on improving, and there are always new features being added. In the most recent release, these include:

  • Generic Math
  • Regular Expression improvements

Generic Math Library in .NET

Generic Math gives .NET developers the ability to use operators on generic types. An example from Microsoft follows:

public static TResult Sum<T, TResult>(IEnumerable values)
    where T : INumber
    where TResult : INumber
{
    TResult result = TResult.Zero;

    foreach (var value in values)
    {
        result += TResult.Create(value);
    }

    return result;
}

public static TResult Average<T, TResult>(IEnumerable values)
    where T : INumber
    where TResult : INumber
{
    TResult sum = Sum<T, TResult>(values);
    return TResult.Create(sum) / TResult.Create(values.Count());
}

public static TResult StandardDeviation<T, TResult>(IEnumerable values)
    where T : INumber
    where TResult : IFloatingPoint
{
    TResult standardDeviation = TResult.Zero;

    if (values.Any())
    {
        TResult average = Average<T, TResult>(values);
        TResult sum = Sum<TResult, TResult>(values.Select((value) => {
            var deviation = TResult.Create(value) - average;
            return deviation * deviation;
        }));
        standardDeviation = TResult.Sqrt(sum / TResult.Create(values.Count() - 1));
    }

    return standardDeviation;
}

More information on Generic Math can be found here and Regular Expression improvements from the latest release can be seen here.

For a complete list of improvements and changes in .NET 7 Candidate Release 2, have a look here.

Read more C# programming tutorials and software development tips.

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