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.
Writing an extension method - C# Tutorial
From the course: C# Essential Training 2: Generics, Collections, and LINQ
Writing an extension method
- [Instructor] Now I want to go change that method, in my string extensions, to be an extension method, and it couldn't be easier. I'm going to go to the method, I'm going to type this in front of the type, string, for that input, and now this is an extension method. And it extends whatever that first type is, so it extends string, in this case, 'cause that's the first parameter. Importantly, when I'm in here, notice that, as I get the input, I don't have access to any additional functionality of the string class than I would in my normal method or operation that I had here. I have all of the same access, and I have all the same methods and properties available to me. So I'm not getting internal access, I'm not changing the way that I can work with string, I'm simply changing how this method is going to be accessed on the class. So, now, same idea, we're going to get that input, we're going to check if its empty,…
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.