.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Extension Methods In C# 3.0 Framework 3.5

Posted By:Hefin Dsouza       Posted Date: August 11, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Extension Methods In C# 3.0 Framework 3.5

Extension methods a feature of C# 3.0 which gives us the capability of adding our own methods to existing Types without creating a new derived class.

Extension Method's Are Special Static Method's which act like instance methods on extended types.
This Example will create a Extension Method on an String Type to Convert a String to an Integer.
Example :

using System;
namespace ExtensionDemo
{
static class StringExtension
{
public static int ToInteger(this string val)
{
return Convert.ToInt32(val);
}
}
}

Using This Extension in Any Classes

using System;
using ExtensionDemo;
namespace ExtensionDemo
{
class Class1
{
static void Main(string[] args)
{
string dem = "100";
int a = dem.ToInteger();
Console.WriteLine(a);
}
}
}

This is How it Works.
It only Works in Visual Studio 2008 and Framework 3.5
Regards Hefin Dsouza

 Subscribe to Articles

     

Further Readings:

Responses
Author: Ramesh         Company URL: http://www.dotnetspark.com
Posted Date: August 11, 2009

Good one sir..Short and precise..Thanks...

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend