creativity in code.

creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.

Programming - C# Extension Methods


Extension methods are a new feature added to the C# specifiation. They allow you to dynamically add methods to classes from the context of your own application. It is especially usefull for code re-usability and keeps messy static and instance oriented functions to their variable scope.

An example would be to add a regular expresion extension directly to the string class.

You could do this within an extension implementation in the following way:

static class Program

{

static void Main(string[] args)

{

string s = "Hello, world";

s.Strip(@"<(.|\n)*?>");

}

/// <summary>

/// Strips text according to the regular expression passed in

/// </summary>

/// <param name="s"></param>

/// <param name="expresssion"></param>

/// <returns></returns>

static string Strip(this string s, string expresssion)

{

Regex.Replace(s, expresssion, string.Empty);

}

}


As you can see, we have added an extension method, which is a string utility dirctly to the class instance. Because it is defined in the scope of our application it is only available to ourselves.

and


Other pages of interest:
[+] Posix software
[+] Programming - ASP.NET MVC Example
[+] Programming - C# Anonymous methods and Lambda
[+] Protocol Buffers - Google's Data Interchange Format
[+] Consume .NET services without Silverlight
[+] Human Computer Interaction
[+] Java (J2EE) provides a scalable and robust framework for Enterprise application development Java ..
[+] .NET Enterprise provides a truly robust and scalable platform to build business applications on I..
[+] AJAX Alliance upset at lack of basic support for featureset required to push AJAX to the next level
[+] Java JSP and Sever Pages
[+] Java EE and Java SE
[+] Programming - Design patterns in Software


Comments(0)

add comment Add Comment

Add your comment

Email address:
Your name:
Do you want your email to be visible?: