Posted by: Zeeshan Amjad | April 11, 2011

Using Extension Method in VC++.Net and F#

Extension method is a nice feature in C# and and most of the time we are using it without even realizing it. Unfortunately it is not supported natively in VC++ and F#. But it doesn’t mean that we can’t use that feature. We can use it but indirectly. Internally extension methods are static method and we are using it in similar way. We are calling that method just like static method of their appropriate class and pass the instance of our class as a parameter to it. The only thing we have to do it use the System.Linq namespace.

Let’s take a look at simple example of ElementAt extension method of List class. When working with C# or VB.Net we even don’t know that it is extension method. Here is our C# code of using this extension method.

Code Snippet
IList<String> list = new List<String>();




In VC++.Net and F# we are going to call ElementAt as a static method of Enumerable class. Here is complete VC++.Net program to do the same thing.

Code Snippet
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Linq;

int main(array<System::String ^> ^args)
    IList<String^>^ list = gcnew List<String^>();


    String^ str = Enumerable::ElementAt(list, 0);

    return 0;


Here is F# version of the same program.

Code Snippet
// Learn more about F# at
open System
open System.Collections.Generic
open System.Linq

let list = List<String>()


let str = Enumerable.ElementAt(list, 0)



  1. Hi,

    whats the reason for this, as such Collection classes could be accessed by indexes anyway.


    • This is just an example of using extension method in a language that doesn’t support it directly. I chose the collection classes, but it is true for any other classes that uses the extension method. Internally it is working just like a static method. CLR doesn’t know anything about it, it is just a syntaxtical suger in C# and VB.Net languages.

      Zeeshan Amjad

  2. Nice post.Visit WPF Extension Methods or visit Extension Method Kit for full set.

    • Thanks to like it. I visited your library and you have some cool methods. Thanks for creating this library.

      Zeeshan Amjad

Leave a Reply

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

You are commenting using your 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


%d bloggers like this: