C# 9 - Record Types Introduction & Deep-Dive

preview_player
Показать описание
Record types are immutable reference types that provide value semantics for equality. Don't worry - record types aren't as complicated as they sound. Let's break it all down and see what record types are, how to implement them in C# 9, and why we should care about them.

Record types are introduced in C# 9 to allow developers to write immutable reference types. It means that the properties of an instance of a reference type cannot change after its initialization.

If we want to change the value of an existing object's property, we need to create a new instance. It is possible to inherit from a record type.

Record Types offer the following features:

- Immutability through read-only properties
- A constructor with properties as arguments
- Value-based equality checks
- A textual representation (ToString)
- A Deconstruct method

Record types are essentially a compiler feature. The developer writes a record type definition, and during compilation, the compiler generates a class based on a template.

Every situation where we could take advantage of one of the features record types provide is a potential use case for using record types instead of writing a class.

00:00 Introduction
00:33 Record Types Introduction
01:47 Cloning a record type
02:11 Record Types Inheritance
02:59 Value-based Equality
04:26 ToString Implementation
05:28 Record Types Internals - Generated Code (Advanced)
06:53 Record Types Features
08:12 Alternative Syntax
10:32 Alternative Syntax: Properties without init-setters
11:00 Alternative Syntax: Properties with default-setters
11:30 Alternative Syntax: Summary
12:05 Record Types: Add custom methods
12:33 Record Types: Use cases
13:49 Record Types: Conclusion

🔥 C#/.NET Bootcamp: The Fundamentals & more (Code FRIENDS10 for 10% OFF)*

📌 .NET Developer Roadmap

📌 YouTube Playlists:

📌 SharpLab:

✅ SUBSCRIBE FOR MORE VIDEOS

🚀 CONNECT & SUPPORT

CREDITS

* Affiliate Link: I earn a small commission at no additional cost
Рекомендации по теме
Комментарии
Автор

What is your take on C# 9 Record Types? Will you use them? Why, or why not?

ClaudioBernasconi
Автор

Thank you for sharing this great knowledge

parthlearning
Автор

If the data does not change, we don't need to synchronise the access to it ?

انامسلموكفى
Автор

Hello. If I wanna to full clone a record. For example: var rec2=rec1 with { }; Is it the same as: var rec2=rec1; // is the 'with' necessary?

msnotif
Автор

Which tool are you using to convert into IL?

ajeetmalviya
Автор

"var" is completely dead now (it has been before, too, because it is, by definition, less readable).
instead of...:
var person1 = new Person("John", "Adams");
write...:
Person person1 = new("John", "Adams");

it is dead now, since the alternative is more readable (Datatype, variable name, assignment) and shorter than the var syntax. From now on, don't use a language feature unless you need it - so for var it would be limited to very few cases such as ArrayLists or whatever.

Mortuus
join shbcf.ru