From the course: C# Essential Training 2: Generics, Collections, and LINQ
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Solution: Extension method - C# Tutorial
From the course: C# Essential Training 2: Generics, Collections, and LINQ
Solution: Extension method
- [Narrator] In this challenge, you're supposed to create an extension method that's going to allow you on an array of integers to call median, as you see in the test code, and get the median value or the middle number. So in order to do that, one of the things we know we've got to do is have a static method and use the this keyword on the target type, in this case, an int array. But we also have to make the class static in order for this to be an extension method. And of course we don't have to worry about it here, but it also has to be in scope in terms of the namespace. That is really the key to making this an extension method. You can see the compiler is happy down there in the test code. I'm going to go ahead and just paste an implementation in here. Not a particularly great implementation. It's not optimized for performance. This is just a little fun aside from the extension methods. I'm going to clone the array,…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.