Free Trial

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

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. OOA&D > Extending Closed Classes

Extending Closed Classes

There’s one last twist to working with classes in .NET: extension methods. EXTENSION METHODS behave as though they were standard instance methods, but they’re defined in a separate class (and usually a separate namespace). They allow you to add new methods to a class without re-compiling or inheriting from it, so you can extend the functionality of sealed classes and classes, like those in the BCL, that you can’t or shouldn’t change directly. Let’s look at how they work:

0 It isn’t required, but you’ll usually define your extension methods in a new namespace in order to simplify referencing them in your code. You’ll usually call the namespace something like <Project>Extensions.

1 Declare a public static class to contain the methods.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint