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.

Enable ref locals and unsafe contexts in iterators and async methods

Enable ref locals and unsafe contexts in iterators and async methods

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

Enable ref locals and unsafe contexts in iterators and async methods

- [Instructor] The ref keyword allows us to access a value or reference type and change its value, or to protect the reference type from being pointed at by a different instance of an object. This is a reasonable usage in C#, but with the addition of the async and await keywords, they were limited in the use in those functions. This happened because C# did not know if the value or reference type that the ref keyword was pointed to would still exist after the asynchronous operation. This has been improved somewhat in C# 13. Now the ref keyword can be used in an async method with some limitations. For example, async method still cannot return or receive items using the ref keyword, nor can a ref local variable in the async method be retrieved from a ref returning function to be used after the await keyword. So let's take a look at an example. So on the start code for this chapter, we've got a new view model called RefLocalsViewModel. So we're going to open that up and we're going to…

Contents