How C# Extension Methods Work

limbioliong

1. Introduction.

1.1 Extension methods were introduced in C# 3.0.

1.2 The main purpose of extension methods is to provide for seamless integration of new functionalities to existing classes.

1.3 I have personally used extension methods to great effect in my projects and I intend to share some of my extension methods with readers in the future.

1.4 In this blog, I wish to explore how extension methods work under the hood.

1.5 We will explore low-level IL code generated for extension methods as well as their client code.

1.6 For a little bit of fun, I will also show how, with a little bit of IL code tweaking, we can turn a normal static method into an extension method.

2. A Sample Extension Method.

2.1 First let’s create a small C# class library with the following namespace and class :

using System; using System.Collections.Generic; using System.Linq; using System.Text…

View original post 2,962 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s