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.

Allow ref struct types as arguments for type parameters in generics

Allow ref struct types as arguments for type parameters in generics

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

Allow ref struct types as arguments for type parameters in generics

- [Instructor] In the last video, we learned how ref structures never move to the heap in memory and always remain in the stack. We also learned how they can now implement interfaces. In this video, we will expand on this and see how implementing an interface on a ref struct can now be used to allow it to be used as a type in a generic. The importance of having the ref struct implement the interface is so we have some type that we can then use to call methods and properties on. One thing to be aware of, since a ref struct has special handling of memory, it cannot be retained in an instance of a variable of a reference type. So even if a generic class allows a ref struct, it cannot retain a permanent reference to it because it can move out of the stack and into the memory heap, and this isn't allowed for a ref struct. However, a generic ref struct could retrain a permanent reverence to a generic type that allows a ref struct because both would be restricted to the stack. In the start…

Contents