C# Extension Methods & Cleaner Code

My goal in this post is to explain & show you what Extension Methods are and why they are important. I also hope that this will allow you to write cleaner more understandable code for you & others.

So what are Extension Methods? Extension Methods "simply" allow you to tack on methods to .Net types such as strings or integers to act on them. Now when I say "tack on" I mean you can use the "." access modifier to access common functionality that you or someone else has written. If you want the Microsoft MSDN definition see this link. So what does an Extension Method look like? Take a look below:

The Extension Method in the above example is RemoveNumbers();. I'd like to make a point that Extension Methods should easily identify what is going to be done with the type it is being used with. In this case we can come to the conclusion that numbers will be removed from the string testString. Great, we now know what Extension Methods look like when being used but how to you create one. Let's take a look at the code below:

An extension method is defined just like any other method. As you can see this method is public & static. It also returns a string. The method overload is what defines what type the Extension Method will work with. The this keyword before String tells .Net that this method is to be used with the String type. So any variable that is of type String can use this extension method. Nice! Now if you read the above Extension Method you will see that it is simply taking a string & removing numbers...plain & simple. Now remember Extension Methods can do what ever you want them to....with in the confines of .Net & C# of course. 

Now lets say your are developing a library to work with Twitter and you are going to make it public by pushing it to Github. Making your library easy to use is going to help make your library successful. 

A user's goal is to get handles from a list of users on Twitter. Normally you would instantiate a class, call a method pass in values and then get your results. The code is going to look a little ugly and not feel very intuitive. With extension methods a user could simply do myUsers.GetHandle();. The user can now in one line of code do work on their list of users and quickly get the results they want. All the messiness of the logic is hidden from the user. 

As I said before naming is also key to extension methods. You want the name to quickly identify what is going to happen. When a user hits the "." after an typing in an object Intellisense is going to give them a long list of options. Extension method names will play a key part in how fast a user can find and identify the correct method they need. 

I hope this helps you in your development career and also shows you that Extension Methods allow you and others to quickly act on objects in .Net. One more tool in the toolbox. Happy coding!


So who has heard of the saying "You should program yourself out of a job"? Well that may be a good saying, I would say you should program yourself INTO another job. You may ask what exactly does that mean. I will give you a for instance.

My current job has me working on the Integration Team using BizTalk 2010. So where could I go if I only know BizTalk and nothing else with no knowledge of SQL Server, PowerShell, TFS, Windows Server or any other IT related knowledge? Probably not very far. If I can make a SQL query better, automate a build process using PowerShell and TFS, or create a Dev environment by myself I am showing upper management and fellow team members that I have skills and am worth keeping around. So now you ask how do you obtain quality skills and knowledge. Great question! 

My first recommendation is books. Yes the good old paperback or e-book in you like. Not only do they give you a good starting point but they are great reference when you forget something. You set it down beside you and code away. I personally love to use Amazon to get books. I actually have all my books in electronic format so I can carry them with me everywhere. 

My favorite is videos series from websites and this is the one I will detail the most. The first website is Channel9.

Channel9: this website is nothing but Microsoft goodness from webisodes to full series on technologies and methodologies. Yes Channel9 is proprietary but it proves my point. Channel9 reaches out to other people that are talented at teaching and getting topics out to the public. For instance there is a gentleman from LearnVisualStudio named Bob Tabor. He was contacted by Microsoft to create beginner series for programming topics. He has created outstanding C#, Java, HTML5, Windows Phone 8 and Windows Store App series. Channel9 also covers conferences and events held around the world. Some of those videos can give you insight and an advantage on a project.


Microsoft Virtual Academy: this is another Microsoft driven sight but yet another outstanding one. Hear you can enroll and earn points for modules that are completed. If you don't want to keep track that's great just use the content. There is a lot of Jump Start and Deep Dive content for everything from Visual Studio to SQL Server to Azure to Windows Server and more. 


PluralSight: this is another awesome training sight. It is a paid site but provides a very advanced learning area. Another nice thing is that it is not solely devoted to Microsoft technologies. I have used this sight to brush up on Agile programming methods and SCRUM. You do have to pay for this sight at $29/mo for just the content and $49/mo for the content and content files. But if your serious about learning and advancing this is a great tool. If the money sounds a little intimidating they offer a trial period and it gives you 200 minutes of free content. That is certainly enough to go through a good bit of one series or try out a few. 


Go to events! What could be more exciting than being in a room full of nerds or "IT" people talking about nerd stuff. TechEd, VisualStudio Live, Build. These are great events to go to and have your brain pumped with knowledge. Plus you can travel and see the world. 

This one may not be appealing to everyone but it worked for me. Go back to school. I worked as an electrician for four years out of high school before I became a programmer. I finally decided I needed a change and I just did it. If you have an Associates, go get your B.S. If you have your B.S, go get your Masters. If you have your Masters, get your Doctorate. If you have your Doctorate, holy cow that's amazing. Check and see if your company will pay for the schooling or at least help pay for it. Never hurts to ask or try.

My last recommendation is ask your fellow peers. Get the knowledge from other sources but then ask intelligent questions to your peers. Discussion is a great way to solve problems and come up with new good problems. Someone with 10 years of experience can tell you that yes the idea in the book is great BUT when doing that way in this situation this will happen. 

I hope these ideas give you a door way into a new job or at least better your current position. Remember that if you stay still you will fall behind.