Extension methods make C# programming code readable and developer-friendly. UI components generally tend to expose events and common routines which are at times repeatitive and time consuming. This article touches base on one such UI component from Infragistics and looks at how extension methods can help us out.

As MSDN points out : "Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. ".

It's also clearly defined by Scott Hanselman in one of his blog post on Extension methods: "Extension methods are a new feature in .NET framework 3.5 (C#3/VB9) that let you appear to "spot weld" new methods on to existing classes. If you think that the "string" object needs a new method, you can just add it and call it on instance variables."

Extension methods also let you have control over error handling. It's not easy to wrap every piece of code with condition checks such as if some object > 0...etc. These can be very well written at a single place. Statements like this:

Can throw an error if there are no fieldlayouts defined at all. The extension methods also reduce development time once developed and make the code easy to read.

Let's first consider the need for each method and then see its underlying code snippet in extension methods format:

If you want to make a Record Active and Selected for the user, you would use the "IsActive" and the "IsSelected" properties. You would toggle the values for each of the above said properties. This can be easily accomodated in the extension method shown below:

If you want to want to hide certain columns in a XamDataGrid, you would use the Visibility property of the field. This needs to be done after two conditions are checked (i.e., the Field layout existence and the field existence). This can be easily accommodated in the extension method shown below:

Similarly, if you want to show a certain field in a XamDataGrid, you would use the visibility property of the field but this too needs to be done after two conditions are checked (i.e. the field layout existence and the field existence). Look below:

Top White Papers and Webcasts

Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT
The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility.
Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

According to technology research firm Gartner, cloud computing will become the bulk of new IT spend by 20161. By the end of 2017, Gartner predicts that nearly half of large enterprises will have hybrid cloud deployments1. Learn how you can use these trends to your advantage by offering cloud and hybrid data solutions to your customers.