CSTrüter HomeArticlesDownloadsAbout meContact me
How to enable the filestream feature in SQL 2008 - Alternative way to store blobs(files) via SQL 2010-08-21 19:31:56
How to create a Singleton Pattern in C# 2010-08-10 22:52:52
How to prevent that threads access shared resources concurrently via Monitor. 2010-08-06 15:31:15
A quick review of the book PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide written by Larry Ullman 2010-08-04 21:48:58
How to prevent that threads access shared resources concurrently via Mutex. 2010-08-03 14:42:36
How to stop propagation of javascript events 2010-07-25 21:59:29
Post about how Pete the web developer fixed his sitemap 2010-07-17 15:12:02
How to setup an out of process session service 2010-07-08 17:51:46
How to display/add images from/to a SQL Database 2010-07-04 23:15:15
How to register a custom URL protocol handler 2010-06-28 20:34:01
Creating a WYSIWYG textbox for your website is actually quite simple. 2007-02-01 12:00:00
Move items between two listboxes in ASP.net(C#, VB.NET) and PHP 2008-06-12 17:07:43
Firefox word wrapping issues 2008-06-09 09:51:21
Populate a TreeView control in a windows application. 2009-08-27 16:01:03
2007-02-22 12:00:00
C# 3.0 introduced interesting new functionality, one of them being extension methods, which enables developers to add methods to existing types, without needing to create a new derived type (interfaces, classes, structs) Extension methods behave much like instance methods, but have a number of limitations they can't for example access private nor protected members of the type they're extending. (I can imagine the logic behind this) Note the following code example and how its being defined, you need a static class (all members must be static) and the first parameter of the extension method contains the "this" keyword, informing the compiler which type we're extending. (you may add numerous parameters you wish to pass to this method, but the first parameter must always define the type you want to extend) public static class ExtensionMethods { public static string Reverse(this string value) { char[] charArray = value.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } } Typically (if you did everything right), your extension method should immediately reflect in your IDE (if you're using one), with a little blue arrow. One can also write overloads to existing methods using extension methods, but cant override existing ones - remember that whenever you're planning to use these methods, the static class containing your extensions, obviously need to be brought in scope with whatever code you're planning to use it with (namespaces etc) My feelings on this feature, is that its a tool that must only be used if its REALLY needed. (You can open a tin of tuna with a hammer, but there is better ways to do it) Also if you have access to the source code to a class you need to extend, consider simply adding the method to the class, dont extend it - unless you've got very good reasons. Imagine the impact of misusing this feature (which is true for most others), having your business logic for example, all over the show extended by extension methods, where you potentially come to the point where functionality gets decentralized, it can most definitely lead to some ugly unmanageable code. I've noticed an interesting way Microsoft use extension methods in context with Linq - as soon as you include System.Linq (among others) as a namespace, a number of extension methods gets added, which enables developers to query those types with linq.
public static class ExtensionMethods { public static string Reverse(this string value) { char[] charArray = value.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } }
The company I am currently working for as software developer.
Collection of C# snippets 2010-05-22 01:06:19
Collection of MS SQL snippets 2010-05-22 00:55:15
Collection of JavaScript snippets 2010-05-22 00:37:57
Collection of ASP.net snippets 2010-05-22 00:29:56
Collection of PHP snippets 2010-05-22 00:06:45
a Parallel reference of programming languages 2009-09-10 12:48:23
a tutorial explaining how to develop a simple login using PHP and MySQL 2009-09-05 18:26:47
An article looking at adding some kind of event driven model to PHP 5 2008-07-28 12:48:09
It is very simple creating your own rss reader, the following article looks at a few methods of doing this. 2008-06-23 13:18:25
A quick reference about working with dropdown boxes (select element) in javascript. 2007-02-17 16:36:41
Collection of funny programming articles 2006-10-08 14:23:43