{"id":305,"date":"2020-10-14T15:50:52","date_gmt":"2020-10-14T10:20:52","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=305"},"modified":"2020-10-14T15:50:52","modified_gmt":"2020-10-14T10:20:52","slug":"extension-methods","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2020\/10\/14\/extension-methods\/","title":{"rendered":"Extension Methods"},"content":{"rendered":"\n<p>Extension methods enable you to &#8220;add&#8221; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they&#8217;re called as if they were instance methods on the extended type. For client code written in C#, F# and Visual Basic, there&#8217;s no apparent difference between calling an extension method and the methods defined in a type.<\/p>\n\n\n\n<p>The most common extension methods are the LINQ standard query operators that add query functionality to the existing&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.collections.ienumerable\">System.Collections.IEnumerable<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.ienumerable-1\">System.Collections.Generic.IEnumerable&lt;T&gt;<\/a>&nbsp;types. To use the standard query operators, first bring them into scope with a&nbsp;<code>using System.Linq<\/code>&nbsp;directive. Then any type that implements&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.ienumerable-1\">IEnumerable&lt;T&gt;<\/a>&nbsp;appears to have instance methods such as&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.linq.enumerable.groupby\">GroupBy<\/a>,&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.linq.enumerable.orderby\">OrderBy<\/a>,&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.linq.enumerable.average\">Average<\/a>, and so on. You can see these additional methods in IntelliSense statement completion when you type &#8220;dot&#8221; after an instance of an&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.ienumerable-1\">IEnumerable&lt;T&gt;<\/a>&nbsp;type such as&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.list-1\">List&lt;T&gt;<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.array\">Array<\/a>.<\/p>\n\n\n\n<h3 id=\"orderby-example\">OrderBy Example<\/h3>\n\n\n\n<p>The following example shows how to call the standard query operator\u00a0<code>OrderBy<\/code>\u00a0method on an array of integers. The expression in parentheses is a lambda expression. Many standard query operators take lambda expressions as parameters, but this isn&#8217;t a requirement for extension methods. For more information, see\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/lambda-expressions\">Lambda Expressions<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> static void Main()\r\n    {\r\n        int&#91;] ints = { 10, 45, 15, 39, 21, 26 };\r\n        var result = ints.OrderBy(g => g);\r\n        foreach (var i in result)\r\n        {\r\n            System.Console.Write(i + \" \");\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<h2>Benefits of extension methods<\/h2>\n\n\n\n<ul><li>Extension methods allow existing classes to be extended without relying on inheritance or having to change the class&#8217;s source code.<\/li><li>If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced, in other words extension methods.<\/li><li>This feature is important for all developers, especially if you would like to use the dynamism of the C# enhancements in your class&#8217;s design.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Extension methods enable you to &#8220;add&#8221; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they&#8217;re called as if they were&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4,2],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/305"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":306,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions\/306"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}