Summary

Extension methods give us an easy way to add a new method to an existing class or type without having to modify the original class or type. Besides, we don't need to recompile the code since just after we create the extension method it will be recognized by the code in the project. An extension method has to be declared as a static method inside a static class. As there is no apparent difference compared to the existing methods in a class or type, the method will also appear in the IntelliSense.

Extension methods can also be declared in another assembly, and we have to refer to the namespace of the static class defined the method stored in the other assemblies. However, we can use the piggyback namespace technique, which uses the existing namespace so that we don't need to refer to any other namespace anymore. Not only can we extend the class and type functionality, but we can also extend the interface, collection, and any objects in the framework.

Like other C# technologies, extension methods also have their advantages and limitations. One of the advantages related to functional programming is that the extension method will make our code apply method chaining so that it can apply the functional approach. However, we cannot extend a static and we cannot modify the method implementation in an existing class or type, which is a limitation of the extension method.

In the next chapter, we will dig into LINQ techniques since we already have enough understanding of delegates, lambda expressions, and extension methods. We will also discuss the convenient way to code a functional program provided by LINQ.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset