Book Image

Instant .NET 4.5 Extension Methods How-to

By : Shawn Ricardo Mclean
Book Image

Instant .NET 4.5 Extension Methods How-to

By: Shawn Ricardo Mclean

Overview of this book

.NET extension methods is an essential feature to know and understand for all .NET developers. Usage of extension methods is found in applications ranging from small to large scale enterprise systems built using the .NET framework. Create and use extension methods the correct way to save your development time and maintainability costs. Instant .NET 4.5 Extension Methods How-to is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises that will help you take advantage of the real power that is behind extension methods and gives you good knowledge of how to use them in your .NET applications. This book covers how to create, write, and use different types of extension methods. It will take you through a number of clear, practical recipes that will help you take advantage of the power of extension methods in the quickest possible way. You will also learn exactly how to create extension methods on strings, interfaces, classes such as IQueryable and IEnumerable, and so on. You will write them from scratch and then use them practically in your application. You will also learn the most suitable scenarios for using these extension methods.You will learn everything you need to know about creating your own extension methods and using them and other external extension methods.
Table of Contents (6 chapters)

Preface

Instant .NET 4.5 Extension Methods How-to seeks to discover the creation and usage of extension methods from the most basic to veteran coder level of experience. It seeks to improve code maintainability and the structure of your .NET applications. It will take you through a number of clear, practical recipes that will help you take advantage of the power extension methods has to provide in a quick and seamless manner.

What this book covers

Your first extension method (Must know) starts off by creating the simplest form of extension methods and comparing it to the previous way of accomplishing this goal. We go into the technicalities of how the compiler handles extension methods and what they really are.

Extension methods on string data types (Must know) teaches how to extend the string data type for validation and modifications. The various caveats when working with strings are also explained.

Extension methods on classes (Should know) talks about extending your own classes or classes whose source you have no access to.

Chaining extension methods (Should know) talks about writing extension methods in a manner that allows chaining calls when using it.

Overloading extension methods (Should know) shows how to overload your own and existing extension methods.

Overriding extension methods (Should know) aims at teaching the tricks needed to override extension methods and explains its technicalities.

Extension methods on interfaces (Should know) gives examples of creating and using extension methods on interfaces with a twist to mixing it with overloading class extension methods.

Extension methods on enumerables (Should know) aims at showing many tricks of extending enumerables with modifying, filtering, and chaining.

Extension methods on IQueryable (Become an expert) aims at showing performance optimizations and tricks between IEnumerable and IQueryable.

Extension methods with generics (Become an expert) seeks to explore the caveats of extending generics. Extending generics should be done with care, and if not done properly will cause ambiguity when using them.

Extension methods with lambda expressions (Become an expert) talks about extending lambda expressions, that are somewhat of a niche feature and rarely done, and can be very useful for those familiar with the functional programming paradigm.

Structuring your project and best practices (Should know) helps the developer structure their project so it can be easily maintained.

Appendix – List of well-known extension libraries contains a list of useful extension method libraries that developers can include in their projects and get start using them immediately.

What you need for this book

To run the code in this book you need the following:

  1. Visual Studio 2008 or higher

  2. .NET 3.5 or higher

Who this book is for

This book is great for both new and veteran developers in C# and the .NET framework. The readers are assumed to have basic understanding of the language syntax, types, and OOP concepts. Readers are also expected to have some experience in finding their way around Visual Studio.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The only difference between the two code snippets is the keyword this in the first parameter."

A block of code is set as follows:

//----We use the method like this
int num = 5;
bool isNumEven = false;
//calling the extension method
isNumEven = num.IsEven();
//calling the extension method on raw values
bool isSixEven = 6.IsEven();

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.