SHARE:

Introducing C# 10: Seal overriden ToString() method on records

Introduction

Microsoft has introduced last year records. Which a new way to declare object. Records were, and still are a great feature of C# 9. This year Microsoft is releasing C# 10 which is part of .NET 6 and improves records: The possibility (like a regular class) to seal the overidden ToString() method. Let’s see in this post an example of this.

Example

The following example shows a record named Person which implements an override of the ToString() method. This override is sealed as well with the seal keyword. The record named Student, inherits from Person and tries to override the ToString() method which gives a compilation error:

The given error is the same as a class: Compiler Error CS239

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.