PIN Extension Method Using C#

An extension method is a special kind of static method that allows you to add new methods to existing types without creating derived types. PIN stands for the three extension methods PreviousItem, IndexOfCurrentItem and NextItem.


Prologue:

You may get confused about this topic PIN Extension Method. PIN is a collection of three IEnumerable extension methods.

PIN stands for

  • PreviousItem
  • IndexOfCurrentItem
  • NextItem

These three extensions methods can be accessed on any IEnumerable type of collections.

Short introduction of Extension Methods:

An extension method is a special kind of static method that allows you to add new methods to existing types without creating derived types. The extension methods are called as if they were instance methods from the extended type. For example: x is an object from int class and we called it as an instance method in int class.

Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code. This means that if you want to add some methods into the existing String class you can do it quite easily.
 
Considerations while creating Extension methods:
  • Extension methods cannot be used to override existing methods
  • An extension method with the same name and signature as an instance method will not be called
  • The concept of extension methods cannot be applied to fields, properties or events
  • Use extension methods sparingly...overuse can be a bad thing!

Why PIN:

You could have asked these questions in your past or you may have now...
  • How to get the Previous item in a collection in a foreach loop
  • How to get the Index of the current item of a collection in a foreach loop
  • How to get the Next item in a collection in a foreach loop

For these three questions PIN extension methods are the answer for you.

Consider the situation where you are processing thousands of items in a collection. At that time you need to get the previous item or next item or index of the current item. For this situation the PIN extension methods will help you.

Lets us discuss each extension method one by one.

PreviousItem in PIN

Anyhow, to get the previous item in a collection we need a for loop in the extension method.

PExten1.gif

In this method we are just looping through the collection to check whether the given current item is available. If so then we will return the previous item.

Input: Item of type T which may/may not be in the collection

Returns: Previous item from of type T collection if the current item exists else current item.

The (i > 0) condition is to ensure that the given current item is not the first item because if it is the first item then there won't be a previous item.

IndexOfCurrentItem in PIN

PExten2.gif

The IndexOfCurrentItem is used to return the index of the current item given. In this we just compare the current item; if it is exists in the collection then it will return the index.

Input: Item of type T which may/may not be in the collection.

Returns: Index of the item from of type T collection if the current item exists else current item.

NextItem in PIN

PExten3.gif

The NextItem method will return the Next item following the given current item if the current item is exists, else it will return the current item.

Input: Item of type T which may/may not be in the collection

Returns: Next item from of type T collection if the current item exists else current item.

The rounded condition is to ensure the given current item is not the last item.

Advantage of PIN
  • As it is a Generic type, we can use on all types of IEnumerable collection.

Usage of PIN

To demonstrate the PIN extension methods we have a Test project. To make PIN our Test project we need to add a Reference of the PIN assembly.

PExten4.gif

Example I:

PExten5.gif

In this example screen we are getting the previous item from the name collection.

PExten6.gif

In this example screen we are getting the next item of the current item.

PExten7.gif

In this example we are getting the index of the current item.

PExten8.gif

This picture shows you that we can also use the User defined types.

In one of my projects I worked on, I had the problem of how to get the previous item and index of the current item. So I tried to write an extension method to implement on IEnumerable collection.

Finally it's done!

Summary:

We have seen about the PIN extension methods for the IEnumerable collection.

Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which enable me to give a better article the next time.

Please rate this Article.

Thanks.