Join Reynald Adolphe for an in-depth discussion in this video Using extension add methods, part of C# 6.0 First Look.
- Another thing to note about Collection initializers in C# 6.0 is that they now observe Extension Add Methods. I've gone ahead and created a custom object. In Visual Studio, you'll find an initialized Collection inside the method Test _ExtensionAddMethods. So let's go ahead and click on this call to the method, and Go To Definition, and then from here, let's click on ArtistCollection, navigate to the Definition, and let's break down what's going on with this type.
We have an Artist class implementing IEnumerable of Artist, which means it needs a strongly typed version of GetEnumerator, and this implementation of GetEnumerator will return us an IEnumerator Collection of Artist. If you recall, IEnumerator is a pretty significant interface in C#. It is what allows us to iterate through a Collection. It is what makes using the foreach statement even possible.
Well, this interface also requires us to implement IEnumerable.GetEnumerator which, as the name implies, returns an IEnumerator. And lastly, we have a method for adding an Artist which returns the current Collection of Artist in addition to the newly added one. Now let's navigate back to our Program file. In this case right now, we have access to ArtistCollection, but what if we didn't? What if we didn't have access to this Collection, this type, and for whatever reason, it didn't use the Add Method, but used another term.
We'd have to find some workaround in order to deal with it because if we changed this Add to, let's say, Put, the compiler will complain because it doesn't recognize Put by default but it does for Add. However, in C# 6.0, because we are now able to use Extension Methods for cases such as this, this gives us a neat little workaround in order to address this issue.
So all we need to do is come down here to create an Extension Method. Want to make sure we put it in the right location. Let's start off with a static class ArtistStorage Extension public static, let's give it a name of Artist, and then, we'll use the term Add which we know will work for us, use the keyword for when creating Extension Method, use the keyword this.
We want this instance of ArtistCollection, let's go ahead and give it a name of collection, also pass in an Artist and give it a variable name of anotherArtist, and now, let's just take that collection and call its Put method, pass in anotherArtist, and then return that other Artist.
And now, our compiler no longer complains, and if we were to try to debug this, click Start, and step through, we can go ahead and see that we have four Artists, two of them were the default Artists that were already in there. In case you didn't notice it before, in our ArtistCollection, we had two, Jay Z and Julio Iglesias as default Artist, but then, in our program method, we added two more, so in our Collection, we have four.
And that's it for Extension Add Methods.
- Introducing the new IDE in Visual Studio 2015
- Leveraging nameof expressions
- Using index initializers
- Using await in catch and finally blocks
- Using static
- Resolving conflict instance methods