Naveen's Weblog

Bridge to future

Extension methods

Posted by codingsense on December 24, 2008


Here is a small example of Extension methods using C# 3.5

In this example we will put our own function in Frameworks string data type.

This will help user to print the the content of the stirng to console.

using System;
namespace ConsoleApplication1
{
public static class ExtendString
{
public static void PrintToConsole(this string str)
{
Console.WriteLine(str);
}
}
class Program
{
static void Main(string[] args)
{
string s = "Naveen.Prabhu";
s.PrintToConsole();
Console.Read();
}
}
}

Hope this will help you to understand the use of extension methods.

Create your own samples and take maximum use of this feature.

Happy Learning 🙂

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

 
%d bloggers like this: