From the course: .NET 9 Maui: Enhanced Features for Cross-Platform Development

Unlock this course with a free trial

Join today to access over 24,400 courses taught by industry experts.

New lock type and semantics

New lock type and semantics

- [Instructor] C# 13 changed how locking can work with a new type, System.Threading.Lock. Even so, the current lock syntax will work as it did before C# 13, so any of your current lock code will still work. Instead, the motivation for the new type was to give us more control over the existing monitor-based lock keyword and slightly better performance. If you get the start code for this video, there is a new view model called LockingViewModel that uses the old lock syntax. So we're going to open this up. And look at the variable called _Lock. As you can see, locks have been done against simple object types before. And if we're going to scroll down here to the CountLocked method, here we can see using that _Lock keyword. The syntax for monitor-based locks were pretty simple. It will lock out a section of code. The same lock could even be used for multiple sections of code, keeping competing threads out of different sections of code that all use the same resources. So let's see how this…

Contents