<p>I have read about partial methods in the latest <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx" rel="noreferrer">C# language specification</a>, so I understand the principles, but I'm wondering how people are actually using them.  Is there a particular design pattern that benefits from partial methods?</p>

[{'answer_id': 42190, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 2, 'selected': False, 'text': '<p><strong><a href="http://msdn.microsoft.com/en-us/library/bb531348.aspx" rel="nofollow noreferrer">Code generation</a></strong> is one of main reasons they exist and one of the main reasons to use them.</p>\n\n<hr>\n\n<p>EDIT: Even though that link is to information specific to Visual Basic, the same basic principles are relevant to C#.</p>\n'}, {'answer_id': 42227, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': "<p>I see them as lightweight events. You can have a reusable code file (usually autogenerated but not necessarily) and for each implementation, just handle the events you care about in your partial class. In fact, this is how it's used in LINQ to SQL (and why the language feature was invented).</p>\n"}, {'answer_id': 42243, 'author': 'Jonathan Webb', 'author_id': 1518, 'author_profile': 'https://Stackoverflow.com/users/1518', 'pm_score': 3, 'selected': False, 'text': '<p>Partial methods are very similar in concept to the GoF <a href="http://www.dofactory.com/Patterns/PatternTemplate.aspx" rel="noreferrer">Template Method</a> behavioural pattern (<a href="http://books.google.com/books?id=aQ1RAAAAMAAJ&amp;q=design+patterns&amp;dq=design+patterns&amp;ei=pNO-SJqWEY_-jgH1pvHpDA&amp;pgis=1" rel="noreferrer">Design Patterns</a>, p325).  </p>\n\n<p>They allow the behaviour of an algorithm or operation to be defined in one place and implemented or changed elsewhere enabling extensibility and customisation. I\'ve started to use partial methods in C# 3.0 instead of template methods because the I think the code is cleaner.</p>\n\n<p>One nice feature is that unimplemented partial methods incur no runtime overhead as they\'re compiled away.</p>\n'}, {'answer_id': 42341, 'author': 'Ed Schwehm', 'author_id': 1206, 'author_profile': 'https://Stackoverflow.com/users/1206', 'pm_score': 0, 'selected': False, 'text': '<p>Here is the best resource for partial classes in C#.NET 3.0: <a href="http://msdn.microsoft.com/en-us/library/wa80x488(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/wa80x488(VS.85).aspx</a></p>\n\n<p>I try to avoid using partial classes (with the exception of partials created by Visual Studio for designer files; those are great). To me, it\'s more important to have all of the code for a class in one place. If your class is well designed and represents one thing (<a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow noreferrer">single responsibility principle</a>), then all of the code for that one thing should be in one place.</p>\n'}, {'answer_id': 43557, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 6, 'selected': True, 'text': '<p>Partial methods have been introduced for similar reasons to why partial classes were in .Net 2.</p>\n\n<p>A partial class is one that can be split across multiple files - the compiler builds them all into one file as it runs.</p>\n\n<p>The advantage for this is that Visual Studio can provide a graphical designer for part of the class while coders work on the other.</p>\n\n<p>The most common example is the Form designer. Developers don\'t want to be positioning  buttons, input boxes, etc by hand most of the time.</p>\n\n<ul>\n<li>In .Net 1 it was auto-generated code in a <code>#region</code> block</li>\n<li>In .Net 2 these became separate designer classes - the form is still one class, it\'s just split into one file edited by the developers and one by the form designer</li>\n</ul>\n\n<p>This makes maintaining both much easier.  Merges are simpler and there\'s less risk of the VS form designer accidentally undoing coders\' manual changes.</p>\n\n<p>In .Net 3.5 Linq has been introduced.  Linq has a DBML designer for building your data structures, and that generates auto-code.</p>\n\n<p>The extra bit here is that code needed to provide methods that developers might want to fill in.</p>\n\n<p>As developers will extend these classes (with extra partial files) they couldn\'t use abstract methods here.</p>\n\n<p>The other issue is that most of the time these methods wont be called, and calling empty methods is a waste of time.</p>\n\n<p>Empty methods <a href="https://stackoverflow.com/questions/11783/in-net-will-empty-method-calls-be-optimized-out">are not optimised out</a>.</p>\n\n<p>So Linq generates empty partial methods. If you don\'t create your own partial to complete them the C# compiler will just optimise them out.</p>\n\n<p>So that it can do this partial methods always return void.</p>\n\n<p>If you create a new Linq DBML file it will auto-generate a partial class, something like</p>\n\n<pre><code>[System.Data.Linq.Mapping.DatabaseAttribute(Name="MyDB")]\npublic partial class MyDataContext : System.Data.Linq.DataContext\n{\n    ...\n\n    partial void OnCreated();\n    partial void InsertMyTable(MyTable instance);\n    partial void UpdateMyTable(MyTable instance);\n    partial void DeleteMyTable(MyTable instance);\n\n    ...\n</code></pre>\n\n<p>Then in your own partial file you can extend this:</p>\n\n<pre><code>public partial class MyDataContext\n{\n    partial void OnCreated() {\n        //do something on data context creation\n    }\n}\n</code></pre>\n\n<p>If you don\'t extend these methods they get optimised right out.</p>\n\n<p>Partial methods can\'t be public - as then they\'d have to be there for other classes to call. If you write your own code generators I can see them being useful, but otherwise they\'re only really useful for the VS designer.</p>\n\n<p>The example I mentioned before is one possibility:</p>\n\n<pre><code>//this code will get optimised out if no body is implemented\npartial void DoSomethingIfCompFlag();\n\n#if COMPILER_FLAG\n//this code won\'t exist if the flag is off\npartial void DoSomethingIfCompFlag() {\n    //your code\n}\n#endif\n</code></pre>\n\n<p>Another potential use is if you had a large and complex class spilt across multiple files you might want partial references in the calling file. However I think in that case you should consider simplifying the class first.</p>\n'}]
['https://Stackoverflow.com/questions/42187', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1219/']