{"id":12308,"date":"2026-03-19T10:44:13","date_gmt":"2026-03-19T10:44:13","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=12308"},"modified":"2026-03-20T09:24:44","modified_gmt":"2026-03-20T09:24:44","slug":"inlining-methods-in-c","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/inlining-methods-in-c\/","title":{"rendered":"Inlining methods in C#"},"content":{"rendered":"<p>Inlining is a simple process of, essentially replacing a method call with the contents of the call, it&#8217;s main benefits are around performance in that it removed the method call\/stack frame overhead and is especially useful in tight looks or hot path scenarios.<\/p>\n<p>An example in C++ we might have something like<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nclass Math {\r\npublic:\r\n    inline int square(int x);\r\n};\r\n\r\ninline int Math::square(int x) {\r\n    return x * x;\r\n}\r\n\r\nMath m;\r\nint y = m.square(5);\r\n<\/pre>\n<p>where the <em>square<\/em> method might be compiled down to <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nint y = 5 * 5;\r\n<\/pre>\n<p>This post isn&#8217;t about C++ inlining but there are similarities with C# in that a method may be implicitly inline, i.e. no need for the <em>inline<\/em> keyword and where we might request a method to be inlined &#8211; the compiler in this case may or may not fulfil the request.<\/p>\n<p>In C# the same implicit inlining can be seen with code such as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static int Square(int x) =&gt; x * x;\r\n<\/pre>\n<p>and again we can request or hint to the JIT to inline &#8211; this is the key thing to take away from this post, we can request inlining but essentially the compiler will make the decision on whether it should inline our code.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\npublic static int Square(int x) =&gt; x * x;\r\n<\/pre>\n<p><strong>What cannot be inlined?<\/strong><\/p>\n<ul>\n<li>Methods which are too large, i.e. a method body which exceeds JITA internal size threshold.<\/li>\n<li>Virtual\/interfaces calls cannot be inlined unless the JIT can de-virtualize them.<\/li>\n<li>Generic methods with unresolved types.<\/li>\n<li>try\/catch\/finally blocks are no eligible for inlining<\/li>\n<li>async\/iterator methods i.e. async\/await and yield return both compile to a state machine which cannot be inlined.<\/li>\n<li>Methods with lock keyword, this essentially compiles to Monitor.Enter\/Monitor.Exit with try\/finally and hence cannot be inlined.<\/li>\n<li>Methods with stackalloc cannot be inlined.<\/li>\n<li>Methods with unsafe and some other pointer operations cannot be inlined.<\/li>\n<li>Might seem obvious but marking your methods as MethodImplOptions.NoInlining will not allow this method to be inlined.<\/li>\n<li>P\/Invoke and extern methods.<\/li>\n<li>Inlining may be disabled for debug builds, and other compiler optimizations<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Inlining is a simple process of, essentially replacing a method call with the contents of the call, it&#8217;s main benefits are around performance in that it removed the method call\/stack frame overhead and is especially useful in tight looks or hot path scenarios. An example in C++ we might have something like class Math { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-12308","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/12308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=12308"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/12308\/revisions"}],"predecessor-version":[{"id":12325,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/12308\/revisions\/12325"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=12308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=12308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=12308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}