Check out a free preview of the full C# and .NET Basics course
The "Extension Methods in CSharp" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:
Spencer introduces the concept of extension methods which allow developers to add behavior to existing classes without actually extending those classes. He demonstrates how to define and use extension methods using examples with the string class.
Transcript from the "Extension Methods in CSharp" Lesson
[00:00:00]
>> Spencer Schneidenbach: Let's move on to talking about extension methods, we're just gonna touch on this real briefly, because they are important to know. Especially as we come up on talking about link, which is the next section, which I'm really excited to talk about. But sometimes there are situations where you have classes that maybe they're in a separate library.
[00:00:19]
Maybe they're in a NuGet package and you don't wanna decompile the source code or build it yourself. You just wanna use the package as it was intended, and maybe add a few extra behaviors or things to it that you just wanna do. Maybe it's a method that doesn't quite fit inside of another class, right?
[00:00:39]
So, you wanna add that method, but it really kind of doesn't fit in with the rest of the class. It's kind of just stands alone, especially in the context of maybe it's useful in this library, but it's not useful. It's useful in this ASP Net core application, but it's not useful in this Winforms app.
[00:00:55]
So, string is a good example of that, string is a sealed class means you cannot inherit from it, thank goodness. And it's fundamental to the language, but you can't extend it very much, which is fine, but maybe not ideal for a given use case. You may find that you wanna do things with the string that are maybe specific to your use case but do it in a way that's convenient for developers.
[00:01:17]
So, extension methods are kind of the magic that kind of makes that possible. So, an extension method allows you to essentially add behavior to an existing thing without actually extending that thing, let me show you what that looks like. Let's take, for example, the string classes we already have and maybe we want to define a method that basically says is palindrome.
[00:01:43]
Maybe we're doing some kind of riddle application or some kind of puzzle application, and knowing if a string is a palindrome is very useful in that context, but not really in the context of the real world. You can have this string extensions class that essentially defines a static, so this is a static class.
[00:02:00]
It has only behaviors, it does not have properties, and it only has behaviors that can affect other things, right? There's no instances of this class, you just use it to hold methods essentially or properties that other things can use. And then you have this static method called is palindrome and you want it to operate on strings.
[00:02:20]
Copy this over to our code, remove this and then paste this in, perfect, okay. We could very easily go up to this and say var ma'am. And then we could easily say String Extensions, because that's the name of the class that we've defined our method in, is Palindrome, and say maam, right?
[00:02:56]
>> Spencer Schneidenbach: And then we can put it out to console and see the value, boom, open our terminal.net run. And see that it is indeed a palindrome, in other words, a word that is spelled the same way forwards as backwards. But we do this a lot, we don't wanna call string extensions everywhere, and we want developers to be happy with us.
[00:03:21]
We're gonna extend this thing, we just want this to kind of behave as it's, if an extension of the string class, even though it's there, check this out. If I go ma'am dot is palindrome, I now have access to that method as if it was on the class.
[00:03:39]
So, you notice, if I hover over it, it says extension, and that's because it is still a static method. It is not it does not belong to the string class in any way, but it behaves as though it is inside of the editor, and that's because we've defined it as an extension method.
[00:03:55]
What's the magic that makes that happen? It's this keyword right here, this keyword, literally this keyword, that this keyword is the thing that defines this, the things that makes this an extension method. So, this is just basically a marker to say hey, language, I want you to interpret uses of this palindrome method, as if it were on the actual instance of a string.
[00:04:22]
The moment we delete this, we come down here, and we can see that there's no doesn't have a definition for is palindrome. And no extension method except the first argument of type string could be found, are you missing a using or a directive or an assembly reference? So as soon as we took that out, we no longer have as an extension method, now, again, when we put it back, we don't get the compiler error.
[00:04:46]
Again, we're not actually extending the string class, it's kind of a pattern called a mixing, and this is common in JavaScript. Where you just add kind of behaviors, but it's a lot easier to add behaviors in JavaScript to existing things that it is and c sharp. So, in that we can define additional behaviors, we can even treat them like a little property, it's a method, but it's open parens, close parens, it kind of behaves the same way.
[00:05:12]
So, it's our way of extending the string class and not actually extending the string class. So, this is really useful, especially as we get to to talking about some of the features that are really cool. But here's a real example from the dot NET Core runtime code. So, they have an interface called I Logger, and that logger essentially has a one method that looks kind of like this, where you give it a log level, and a string of message.
[00:05:41]
It also takes in additional stuff, like exceptions and other things that you may want to log additional properties, but it's got one defined interface, and it's got one method on that right? And we want to be able to, very simply, make it so that when our developer goes to an instance of I Logger and says dot log.
[00:06:01]
Maybe they don't wanna type out log level dot information as the first argument every time. Maybe they just wanna type out log information, right? So we can provide extension methods that allow the developer to kind of extend that class in a way that feels natural, but also keeps the surface of the ilogger interface very small.
[00:06:24]
Imagine the converse to this, which is that you have a interface where you have log information, log warning, log error, and all of those things exist on this logger class. Not only are you adding a bunch of behavior where now you have to implement that behavior and maybe you make a mistake, but.
[00:06:40]
A lot of extra typing that simply isn't necessary, because we know our log levels ahead of time, we know that they're information, warning, error, debug, trace, those kinds of things. We can just define extension methods to just make it so that we add, we can just go logger dot log information and then just type in the message.
[00:06:59]
It's important to note, that your extension methods are methods so they can have as many arguments as you want. So, another example might be public static bool are words, let's see, public static string to concert and to upper. So, we can call our string, our original string, and then call concert, then have another string that we wanna concatenate with this.
[00:07:37]
So, there's no this keyword after any other argument, it only operates on the first one. One, but we can have second string, and then we could say return, string dot, concat. So, string, dot, second string, and then to upper, okay? And the way that looks then is that down here, concat and to upper.
[00:08:03]
>> Spencer Schneidenbach: So, we'll concatenate those things and then call to upper on them, so, you can provide additional arguments, but this essentially becomes the first argument in your extension method. The class that you're operating on, is the or the instance of the class that you're operating on is the thing that the becomes the first argument to concaten to upper.
[00:08:29]
So that's extension methods, you'll see them a lot, you'll see them a lot in the runtime. It will tell you if something is an extension method, usually denoted by just another symbol, so concat and to upper. Let's see, does it do that? It does tell at least it tells us it's an extension, in some IDEs, like writer, it will actually have a slightly different symbol for these.
[00:08:54]
This is an actual method on the string class, this is a our extension method, so.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops