Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

Chapter 3. Defining Extension and Partia... > Defining Extension Methods - Pg. 64

64 CHAPTER 3 Defining Extension and Partial Methods . Extension methods can be used on sealed classes and intrinsic types like int because extension methods automatically support boxing and unboxing; that is, when value types are used like objects, the .NET Framework wraps a class around the value type. . Extension methods are not really members, so you can only access public members of the object being extended. . An extension method implicitly uses the ExtensionAttribute ; the ExtensionAttribute is used explicitly in VB .NET. The term you will hear associated with extension methods is duct typing. Pun intended. Defining Extension Methods Extension methods follow a consistent pattern. Define a public static class. In that class, define a public static method. The first argument of the method must use the this modi- fier. The first argument, modified by this , represents the class of the object being extended. After the first argument, add additional arguments. Static methods can be over- loaded by parameters and can be generic methods. This section explores extension methods, overloaded extension methods, and generic extension methods.