.NET HELP

C# Extension Methods (How It Works For Developers)

Published August 15, 2024
Share:

Extension methods are a powerful feature in C# that allow you to add new functionality to existing types without modifying their source code. They can be extremely useful in making your code more readable and maintainable. In this guide, we'll explore the basics of extension methods and how to implement them.

What are Extension Methods?

Extension methods are special static methods that can be called as if they were instance methods of an existing type. They are a convenient way to add new methods to an existing class without changing the original source code or inheriting from the class.

To create an extension method, you need to define a static method inside a static class. The first parameter of the method should be the type you want to extend, prefixed with this keyword. This special keyword tells the C# compiler that this is an extension method.

Implementing Extension Methods in C#

Now that we know what extension methods are, let's implement one. Imagine you have a string that you want to reverse. Instead of writing a separate function to do this, you can create an extension method for the string class.

First, let's create a new static class called StringExtensions. The class name is not important, but it's a common convention to use the name of the type being extended followed by "Extensions". Inside this class, we'll define a static method called Reverse:

public static class StringExtensions
{
   public static string Reverse(this string input)
   {
       char [] chars = input.ToCharArray();
       Array.Reverse(chars);
       return new string(chars);
   }
}
public static class StringExtensions
{
   public static string Reverse(this string input)
   {
       char [] chars = input.ToCharArray();
       Array.Reverse(chars);
       return new string(chars);
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

In this example, we created a public static string method called Reverse with a single parameter. The this keyword before the string type indicates that this is an extension method for the string class.

Now, let's see how to use this new extension method in our Program class:

class Program
{
   static void Main(string [] args)
   {
       string example = "Hello, World!";
       string reversed = example.Reverse();
       Console.WriteLine(reversed); // !dlroW ,olleH
   }
}
class Program
{
   static void Main(string [] args)
   {
       string example = "Hello, World!";
       string reversed = example.Reverse();
       Console.WriteLine(reversed); // !dlroW ,olleH
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Notice that we didn't have to create an instance of the StringExtensions class. Instead, we used the Reverse method directly on the string instance as if it were an instance method.

Extension Method Syntax

Extension methods look and behave like instance methods, but there are a few important differences to keep in mind:

  • Extension methods cannot access private members of the extended type
  • They also do not participate in inheritance or polymorphism
  • You cannot override an existing method with an extension method

If the extended type has a method with the same signature as an extension method, the instance method will always take precedence. Extension methods are only called when there is no matching instance method.

Real-Life Examples of Extension Methods

Now that we understand the basics of extension methods in C#, let's look at some real-life examples.

String Extension Method Word Count

Imagine you want to count the number of words in a string. You can create a WordCount extension method for the string class:

public static class StringExtensions
{
   public static int WordCount(this string input)
   {
       return input.Split(new [] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Length;
   }
}
public static class StringExtensions
{
   public static int WordCount(this string input)
   {
       return input.Split(new [] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Length;
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Now, you can easily count the number of words in a string like this:

string text = "Extension methods are awesome!";
int wordCount = text.WordCount();
Console.WriteLine($"The text has {wordCount} words."); // The text has 4 words.
string text = "Extension methods are awesome!";
int wordCount = text.WordCount();
Console.WriteLine($"The text has {wordCount} words."); // The text has 4 words.
Dim text As String = "Extension methods are awesome!"
Dim wordCount As Integer = text.WordCount()
Console.WriteLine($"The text has {wordCount} words.") ' The text has 4 words.
VB   C#

IEnumerable Extension Method Median

Suppose you have a collection of numbers, and you want to calculate the median value. You can create an extension method for IEnumerable:

public static class EnumerableExtensions
{
   public static double Median(this IEnumerable source)
   {
       int [] sorted = source.OrderBy(x => x).ToArray();
       int count = sorted.Length;

       if (count == 0)
       {
           throw new InvalidOperationException("The collection is empty.");
       }

       if (count % 2 == 0)
       {
           return (sorted [count / 2 - 1] + sorted [count / 2]) / 2.0;
       }
       else
       {
           return sorted [count / 2];
       }
   }
}
public static class EnumerableExtensions
{
   public static double Median(this IEnumerable source)
   {
       int [] sorted = source.OrderBy(x => x).ToArray();
       int count = sorted.Length;

       if (count == 0)
       {
           throw new InvalidOperationException("The collection is empty.");
       }

       if (count % 2 == 0)
       {
           return (sorted [count / 2 - 1] + sorted [count / 2]) / 2.0;
       }
       else
       {
           return sorted [count / 2];
       }
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

With this extension method, you can easily find the median value of a collection:

int [] numbers = { 5, 3, 9, 1, 4 };
double median = numbers.Median();
Console.WriteLine($"The median value is {median}."); // The median value is 4.
int [] numbers = { 5, 3, 9, 1, 4 };
double median = numbers.Median();
Console.WriteLine($"The median value is {median}."); // The median value is 4.
Dim numbers() As Integer = { 5, 3, 9, 1, 4 }
Dim median As Double = numbers.Median()
Console.WriteLine($"The median value is {median}.") ' The median value is 4.
VB   C#

DateTime Extension Method StartOfWeek

Let's say you want to find the start of the week for a given date. You can create an extension method for the DateTime struct:

public static class DateTimeExtensions
{
   public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek = DayOfWeek.Monday)
   {
       int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
       return dt.AddDays(-1 * diff).Date;
   }
}
public static class DateTimeExtensions
{
   public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek = DayOfWeek.Monday)
   {
       int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
       return dt.AddDays(-1 * diff).Date;
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Now, you can easily find the start of the week for any date:

DateTime today = DateTime.Today;
DateTime startOfWeek = today.StartOfWeek();
Console.WriteLine($"The start of the week is {startOfWeek.ToShortDateString()}."); // The start of the week is 17/06/2024.
DateTime today = DateTime.Today;
DateTime startOfWeek = today.StartOfWeek();
Console.WriteLine($"The start of the week is {startOfWeek.ToShortDateString()}."); // The start of the week is 17/06/2024.
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Generating PDFs with IronPDF and Extension Methods

In this section, we'll introduce IronPDF, our industry-leading library for generating and working with PDF files in C#. We'll also see how we can leverage extension methods to create a more seamless and intuitive experience when working with this library.

IronPDF converts HTML to PDF in a way that preserves the layout and style of content as it would appear in a web browser. The library can work with raw HTML from files, URLs, and strings. Here’s a quick overview:

using IronPdf;

class Program
{
   static void Main(string [] args)
   {
       var renderer = new ChromePdfRenderer();

       // 1. Convert HTML String to PDF
       var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
       var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
       pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

       // 2. Convert HTML File to PDF
       var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
       var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
       pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

       // 3. Convert URL to PDF
       var url = "http://ironpdf.com"; // Specify the URL
       var pdfFromUrl = renderer.RenderUrlAsPdf(url);
       pdfFromUrl.SaveAs("URLToPDF.pdf");
   }
}
using IronPdf;

class Program
{
   static void Main(string [] args)
   {
       var renderer = new ChromePdfRenderer();

       // 1. Convert HTML String to PDF
       var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
       var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
       pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

       // 2. Convert HTML File to PDF
       var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
       var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
       pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

       // 3. Convert URL to PDF
       var url = "http://ironpdf.com"; // Specify the URL
       var pdfFromUrl = renderer.RenderUrlAsPdf(url);
       pdfFromUrl.SaveAs("URLToPDF.pdf");
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Creating a Simple PDF

Before we dive into extension methods, let's see how to create a simple PDF from HTML using IronPDF:

using IronPdf;

class Program
{
   static void Main(string [] args)
   {
       var renderer = new ChromePdfRenderer();;
       var PDF = renderer.RenderHtmlAsPdf("Hello, World!");
       PDF.SaveAs("HelloWorld.PDF");
   }
}
using IronPdf;

class Program
{
   static void Main(string [] args)
   {
       var renderer = new ChromePdfRenderer();;
       var PDF = renderer.RenderHtmlAsPdf("Hello, World!");
       PDF.SaveAs("HelloWorld.PDF");
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

This code snippet creates a PDF with the text "Hello, World!" and saves it to a file named "HelloWorld.PDF".

Extension Methods for IronPDF

Now, let's explore how we can use extension methods to enhance the functionality of IronPDF and make it easier to work with. For instance, we can create an extension method that takes an instance of the string class and generates a PDF directly from it.

public static class StringExtensions
{
   public static void SaveAsPdf(this string htmlContent, string filePath)
   {
       var renderer = new ChromePdfRenderer();;
       var PDF = renderer.RenderHtmlAsPdf(htmlContent);
       PDF.SaveAs(filePath);
   }
}
public static class StringExtensions
{
   public static void SaveAsPdf(this string htmlContent, string filePath)
   {
       var renderer = new ChromePdfRenderer();;
       var PDF = renderer.RenderHtmlAsPdf(htmlContent);
       PDF.SaveAs(filePath);
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

With this extension method, we can now generate a PDF directly from a string:

string html = "Extension Methods and IronPDFGenerating PDFs has never been easier!";
html.SaveAsPdf("ExtensionMethodsAndIronPDF.PDF");
string html = "Extension Methods and IronPDFGenerating PDFs has never been easier!";
html.SaveAsPdf("ExtensionMethodsAndIronPDF.PDF");
Dim html As String = "Extension Methods and IronPDFGenerating PDFs has never been easier!"
html.SaveAsPdf("ExtensionMethodsAndIronPDF.PDF")
VB   C#

Generating PDFs from URLs

Another useful extension method we can create is one that generates a PDF from a URL. We can extend the Uri class to achieve this:

public static class UriExtensions
{
   public static void SaveAsPdf(this Uri url, string filePath)
   {
       var renderer = new ChromePdfRenderer();;
       var PDF = renderer.RenderUrlAsPdf(url.AbsoluteUri);
       PDF.SaveAs(filePath);
   }
}
public static class UriExtensions
{
   public static void SaveAsPdf(this Uri url, string filePath)
   {
       var renderer = new ChromePdfRenderer();;
       var PDF = renderer.RenderUrlAsPdf(url.AbsoluteUri);
       PDF.SaveAs(filePath);
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Now, we can easily generate a PDF from a URL like this:

Uri url = new Uri("https://www.ironpdf.com/");
url.SaveAsPdf("UrlToPdf.PDF");
Uri url = new Uri("https://www.ironpdf.com/");
url.SaveAsPdf("UrlToPdf.PDF");
Dim url As New Uri("https://www.ironpdf.com/")
url.SaveAsPdf("UrlToPdf.PDF")
VB   C#

Conclusion

And voila - we explored the concept of extension methods in C#, learned how to implement them using static methods and static classes, and used real-life examples for various types. Furthermore, we introduced IronPDF, a library for generating and working with PDF files in C#. As you start using extension methods and IronPDF together, you'll see how much cleaner, more readable, and more efficient your code can become.

Ready to get your hands on IronPDF? You can start with our 30-day free trial. It’s also completely free to use for development purposes so you can really get to see what it’s made of. And if you like what you see, IronPDF starts as low as $749. For even bigger savings, check out the Iron Suite where you can get all nine Iron Software tools for the price of two. Happy coding!

< PREVIOUS
Try/Catch in C# (How It Works For Developers)
NEXT >
C# Using (How it Works for Developers)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >