C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Extension Methods In C# 3.0 Framework 3.5


Posted Date: 04 Jan 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Hefin DsouzaMember Level: Diamond    
Rating: 1 out of 5Points: 10



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




Attachments

  • Extension Methods In C# 3.0 Framework 3.5 (24765-4157-ExtensionDemo.rar)

    For more details, visit http://msdn.microsoft.com/en-us/library/bb383977.aspx




  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Extension Methods  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Function For Password-Checking
    Previous Resource: Parse the Enum value
    Return to Discussion Resource Index
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use