How to search for YouTube videos in a channel with .NET Core 3

Read Time:3 Minute, 15 Second

Probably you have already heard of extension methods: those are C# methods used to add new functionalities to an existing class.

This functionality is available since C# 3.0, so it’s largely used and well documented.

The basics of extension method

Let’s say that you have a non-static class that you cannot modify, maybe because it comes from an external library.

For the Person class we have only the GetFullName method.

So we can use the Person class this way:

Nothing difficult, right?

Now we want to get the full name with the birthdate. We have 2 ways to achieve this: using a subclass – terrible idea – or creating an extension method. Let’s go with the second approach!

First of all, we need a static class that contains our method:

Now we can create the new method. We must remember 2 things:

  • it must be a static method
  • the first parameter must be of the same type we want to extend and must be preceded by the this keyword

Now we can use it with person.GetFullNameWithBirthDate(). Easy-peasy.

If you use Visual Studio and you have the Intellisense enabled, you will see those hints, both on the icon and on the description of the method.

Behind the scenes, we are calling the method on the MyExtensions class, not on the person object. Extension methods act just as shortcuts.

Ok, we have seen an overview of this functionality. Let’s go more in details.

Visibility matters

As you may imagine, since extension methods are meant to extend the public behavior of a class, you cannot use a private field (or method) of that class.

So, if we have a private string ID to our Person class, it won’t be accessible from the extension method.

Of course, you can use both public properties and public methods. In fact, we can do refactor GetFullNameWithBirthDate to

You can extend subclasses

Say that you have a Student class that extends Person:

Now, among our extension methods, we can add functionalities to Student, and reference the Person class:

Finally, we can create extension methods that will override the ones created for the base class:

So, now

will print out

A real life example: LINQ

Since C# 3.5 we have a new query language: LINQ.

LINQ stands for Language-Integrated Query, and it is often used when you are working on collections and you need to do some operations on them, like get all the values that match a particular condition or take only the first element of the collection.

An example can be

By default, List<T> does not include a method that returns the first element of the collection. To use it, we need to include LINQ by putting using System.Linq among the imports.

So yes, we can say that First is a method that extends the List class: LINQ is a set of extension methods!

In fact, we can read its source code and notice this:

So, you can extend this library if you want. Let’s say that you need to get a random element from the list; you can create a method like this:

And call it this way:

Wrapping up

Have you ever used extension methods for creating a library?

Anyway, here’s a short recap of the main points:

  • all in a static class
  • each method must be static
  • the extended class must be non-static
  • the first parameter must be preceded by the this keyword
  • you can use only public fields and methods
  • you can extend classes, but also subclasses and structs
  • the greatest example of library based on extension methods is LINQ

You can see the full example here.


WP Ad Inserter plugin for WordPress