Extension Methods Are Easy with C#
Tags: programming, codetips, extendingmethods
Published: 2019-05-29 07:00:56

---
title: Extension Methods Are Easy with C#
published: true
tags: Programming,C#,code tips,Extending Methods
canonical_url: https://dirkstrauss.com/extension-methods-are-easy/
---

I have said this many times before, I love using extension methods. They are probably the easiest methods to write and can power up your code like never before. If you what to find out more, read on.

## LOOKING FOR SOMETHING ELSE? TRY THESE LINKS INSTEAD:

- [C# 7 and .NET Core Cookbook Published](https://dirkstrauss.com/c-7-net-core-cookbook-published/) 
- [The Unbelievably Powerful Multi Select. Increase Productivity Now](https://dirkstrauss.com/the-unbelievably-powerful-multi-select/) 
- [Cool Life Hacks using Simple Technology Hacks](https://dirkstrauss.com/cool-life-hacks-using-simple-technology/)
- [Sassy Css – The Valuable Benefits You Need To Know](https://dev.to/dirkstrauss/sassy-css--the-valuable-benefits-you-need-to-know-5447-temp-slug-6301632)
- [Struggling With Imposter Syndrome? How To Think And Overcome](https://dirkstrauss.com/struggling-with-imposter-syndrome-think-overcome/)

If you are a bit short on time, go and check out the video explainer on this topic.

{% youtube BKJGnmWcCVw %}

This should get you up and running quickly with using extension methods in your own applications to enhance your code. If you want a little more, keep on reading.

## Extension Methods Explained

If you want to add methods to existing types without having to recompile your code or without creating a new derived type, then extension methods are the way to go.

Extension methods are a special type of static method. These are then called on the type you are extending as if they were instance methods.

![Extension Method displayed in IntelliSence](https://i2.wp.com/dirkstrauss.com/wp-content/uploads/2019/05/IntelliSence.png?w=640&ssl=1)

Have a look at the [Microsoft document explaining extension methods](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) in depth. Consider the following code listing:

```
public static bool ToInt(this string value)
{
    return int.TryParse(value, out var _);
}
```

This is a typical extension method that acts on a string type and checks to see if the string can be parsed to a valid Integer. Note that the TryParse uses an out var using a discards variable.

**TIP:** Introduced in C# 7.0, discards are temporary, dummy variables are intentionally unused. A discard is indicated by using the underscore (\_) for its variable name.

## Using Extension Methods

In order to use your extension method, you need to just call it as you would any method of that type. In our case, we are extending the String type. This is seen by the extension method signature in the previous code listing. It enables us to do the following:

```
var sValue = "15";
if (sValue.ToInt())
{
    // Do something
}
```

If the String variable can successfully be parsed to an Integer, then the extension method will return True. You can even modify the method to return the integer it parsed, instead of a Boolean. You would, of course, need to gracefully handle cases where the String value could not be parsed to an Integer.

## Get the Source Code

If you want to grab a working example of the code, head on over to the following GitHub repo and clone the repo to your local machine.

[dirkstrauss/ExtensionMethodsDemo](https://github.com/dirkstrauss/ExtensionMethodsDemo)

If you have any comments or suggestions, please feel free to put those in the comments section below.

The post [Extension Methods Are Easy with C#](https://dirkstrauss.com/extension-methods-are-easy/) appeared first on [Programming and Tech Blog](https://dirkstrauss.com).