Extension Methods

I’ve just started looking at C#, and having gone through some example code, noticed something unusual in a method definition. There was a method which included the keyword ‘this’ in the parameters. Here’s what it looked like:

 static bool Contains(this string keyString, char c)
 {
     return keyString.IndexOf(c) >= 0;
 }

After doing some looking around, I found out this is what is called an extension method. The difference between an extension method and a normal static one is that it allows you to use the method in a way that makes it look like it’s part of the type (that it’s extending).

Formally, per the Microsoft Documentation;

“Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.”

References:

https://developer.xamarin.com/guides/android/getting_started/hello,android/hello,android_quickstart
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

Advertisements

Greenshot

Greenshot is a free (under GNU General Purpose Licence) tool, which offers more than just the standard Print Screen button would do on a PC.

If you’ve ever wanted to capture just a small part (region) of the screen, then this is a great tool for doing that. It allows you to map itself to your Print Screen button, meaning you can still have the convenience of pressing that button. If you do, after pressing it you’ll be presented with a cross-hair to select what you want to capture on the screen. After letting go, it will then let you decide whether to save it straight away (as a jpg for example), or whether to launch it in MSPaint.

You can find more information, and a free download, here: http://getgreenshot.org