Sunday, December 28, 2008

Extension Methods in C# 3.0


An extension method is a new language feature of C#. This methods provides a ability to "ADD" methods to an existing types without creating a new derived type.

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

In my current project I had Business Object called LOT  this is C# class with the following properties .

public class Lot
       public string lotNumber {get;set;}
       public string lotDescription { get; set; }
       public string customerName { get; set; }
       public string metalId { get; set; };

when I pass this Lot Information to Back end ERP System I need to format with padding Zero ( total length of the lotNumber field is 14 in DB).

Here comes new C# 3.0  Extension Methods for handy .

Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

Here is my Extension methods.




for more Information about  C# Extension Methods POINT your IE to EXTENSION METHODS



No comments: