<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Peter Ritchie's MVP Blog : DevCenterPost</title><link>http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx</link><description>Tags: DevCenterPost</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Becoming a Visual Studio Jedi Part 1</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/09/02/becoming-a-visual-studio-jedi.aspx</link><pubDate>Tue, 02 Sep 2008 18:49:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1640948</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1640948</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/09/02/becoming-a-visual-studio-jedi.aspx#comments</comments><description>&lt;p&gt;&lt;span style="font-size:large;"&gt;Becoming a Visual Studio 2008 (and often Visual Studio 2005) Jedi&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In much the same grain as James&amp;#39; &lt;a href="http://www.jameskovacs.com/blog/BecomingAJediPart1OfN.aspx"&gt;Resharper Jedi&lt;/a&gt; posts, I&amp;#39;m beginning a series of posts on becoming a Visual Studio Jedi.&amp;nbsp; It involves getting the most out of Visual Studio off-the-shelf, doing things as quickly as possible and with as little friction as possible.&amp;nbsp; I think it&amp;#39;s useful for all users; but especially useful for those who are in situations where they can&amp;#39;t install refactoring tools like Refactor Pro! or Resharper.&lt;/p&gt;
&lt;p&gt;First, familiarize yourself with &lt;a href="http://blogs.msdn.com/saraford/archive/tags/Visual+Studio+2008+Tip+of+the+Day/default.aspx"&gt;Sara&amp;#39;s Visual Studio Tips&lt;/a&gt; blog; then subscribe to her blog.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll attempt to provide detail at a less granular level than Sara&amp;#39;s blog (i.e. using a series of commands to perform a specific task); but I may overlap here and there&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:large;"&gt;Take advantage of Auto-Hide &lt;br /&gt;&lt;/span&gt;Like Jimmy Bogard and Jeffery Palermo, I have my Visual Studio UI very lean. 99% of the time, I&amp;#39;m working in code.&amp;nbsp; The Solution Explorer (SE), Properties, Output, etc are auto-hide panes.&amp;nbsp; When I need to use them I hover the mouse over the tab to make them visible, do what I need to do with them, then get back to the code.&amp;nbsp; The Code Editor is the only window that isn&amp;#39;t auto-hide or floating. &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peterritchie/lean-VS2k8.JPG" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:large;"&gt;Navigate find results via keyboard&lt;/span&gt; &lt;br /&gt;Whenever anything is displayed in a find results window, you can iterate each item in the list via a keystroke.&amp;nbsp; The default C# keyboard map had F8 and Shift+F8 as the shortcuts for next and previous.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Press Ctrl+Shift+F to bring up the Find in Files form. &lt;/li&gt;
&lt;li&gt;Enter &amp;quot;TODO\:.*refactor&amp;quot; in the &amp;quot;Find what&amp;quot; text box. &lt;/li&gt;
&lt;li&gt;Ensure Match case, Match whole word are unchecked. &lt;/li&gt;
&lt;li&gt;Ensure Use is checked and has Regular expressions selected. &lt;/li&gt;
&lt;li&gt;Press Alt+F to search. &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peterritchie/Find-in-Files.JPG" alt="" /&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A Find Results window is displayed that shows the results of the search. &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/peterritchie/Find-Results.JPG" alt="" /&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;F8&lt;/strong&gt; goes to the first result.&lt;/li&gt;
&lt;li&gt;Pressing &lt;strong&gt;F8&lt;/strong&gt; again goes to the next. &lt;/li&gt;
&lt;li&gt;Pressing &lt;strong&gt;Shift+F8&lt;/strong&gt; goes to the previous result.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;See also: &lt;br /&gt;&lt;a href="http://blogs.msdn.com/saraford/archive/2008/04/18/did-you-know-you-can-use-f8-and-shift-f8-to-navigate-among-errors-in-the-output-window.aspx"&gt;http://blogs.msdn.com/saraford/archive/2008/04/18/did-you-know-you-can-use-f8-and-shift-f8-to-navigate-among-errors-in-the-output-window.aspx&lt;/a&gt; &lt;br /&gt;&lt;a href="http://blogs.msdn.com/saraford/archive/2007/11/08/did-you-know-how-to-use-f8-to-navigate-the-find-results-window.aspx"&gt;http://blogs.msdn.com/saraford/archive/2007/11/08/did-you-know-how-to-use-f8-to-navigate-the-find-results-window.aspx&lt;/a&gt; &lt;br /&gt;&lt;a href="http://blogs.msdn.com/saraford/archive/2005/03/30/403887.aspx"&gt;http://blogs.msdn.com/saraford/archive/2005/03/30/403887.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;F8 and Shift+F8 work for most lists like Find Results 1, Find Results 2, Error List, Output:Build, etc.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:large;"&gt;File name extensions when adding classes&lt;/span&gt; &lt;br /&gt;Do you find your self selecting text in the file name when you use the Add Class wizard?&amp;nbsp; Or, do you always type &amp;quot;.cs&amp;quot; at the end of your file name?&amp;nbsp; You may be happy to know you don&amp;#39;t have to do that.&amp;nbsp; Simply invoke the Add Class wizard and type the name of the class.&amp;nbsp; The wizard adds the missing .cs for you.&amp;nbsp; For example: &lt;br /&gt;Press Alt+P, C &lt;br /&gt;Enter &amp;quot;MyNewClass&amp;quot; &lt;br /&gt;Press Enter &lt;br /&gt;A file MyNewClass.cs is added to your project and it contains class named &amp;quot;MyNewClass&amp;quot;.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:large;"&gt;Consider a Custom toolbar&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;There&amp;#39;s generally only handful of toolbar buttons that you might need, especially if you&amp;#39;re a keyboard user like me.&amp;nbsp; There&amp;#39;s some things that simply don&amp;#39;t have a default keyboard mapping.&amp;nbsp; Another good reason for having a custom toolbar item with only the buttons you use is if you often change the size of your Visual Studio window.&amp;nbsp; The default layout has two or more toolbars (depending on the edition and any add-ins you have installed).&amp;nbsp; You can carefully position those toolbars so they may take up one or two lines; but when you then shrink the size of your window the get wrapped and they won&amp;#39;t restore if you expand the size of your window.&amp;nbsp; Having a single toolbar means this wrapping of toolbars can never happen.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:large;"&gt;Export Settings&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you get your UI the way you want it, you can actually save the layout.&amp;nbsp; &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Click on Tools\Import and Export Settings.&lt;/li&gt;
&lt;li&gt;Select Export selected environment settings.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is really handy if you get into low-resource situations (like the application you&amp;#39;re developing or its framework uses up too many GDI handles and Visual Studio can&amp;#39;t allocate a handle to display a toolbar or a frame.&amp;nbsp; When this happens Visual Studio actually turns off those GUI elements; when you close and restart those panes/frames are no longer displayed by default.)&lt;/p&gt;
&lt;div style="text-align:left;margin:0px;padding:4px 4px 4px 4px;" class="wlWriterHeaderFooter"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://msmvps.com/blogs/peterritchie/archive/2008/07/15/becoming-a-visual-studio-jedi.aspx&amp;amp;title=Becoming%20a%20Visual%20Studio%20Jedi%20Part%201"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://msmvps.com/blogs/peterritchie/archive/2008/07/15/becoming-a-visual-studio-jedi.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1640948" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Design_2F00_Coding+Guidance/default.aspx">Design/Coding Guidance</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Visual+Studio+2008/default.aspx">Visual Studio 2008</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>Fundamentals of OOD Part 3: Method Cohesion</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/06/10/fundamentals-of-ood-part-3-method-cohesion.aspx</link><pubDate>Tue, 10 Jun 2008 19:26:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1633691</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1633691</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/06/10/fundamentals-of-ood-part-3-method-cohesion.aspx#comments</comments><description>&lt;p&gt;Single Responsibility Principle (SRP) helps us write more cohesive types and methods.&amp;nbsp; Cohesion is the relatedness of the members of a type to each other and the relatedness parts of a method&amp;#39;s code to other parts.&lt;/p&gt;
&lt;p&gt;&lt;font size="3"&gt;&lt;strong&gt;Method cohesion&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;Often times a method is not very cohesive, meaning the code that it executes relates to more than one thing.&amp;nbsp; This can often be seen with a method that contains a large switch statement.&amp;nbsp; For any invocation of the method only one case statement may be executed; meaning that blocks of code within the method don&amp;#39;t relate to all the other blocks.&amp;nbsp; Switch statements are often an indication that the design should be changed to be more polymorphic or introduce a pattern like the Strategy or Template Method patterns.&amp;nbsp; Likely a concept of the design is implicit instead of explicit (at least no more explicit than an enum).&amp;nbsp; For example:&lt;br /&gt;&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Set&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;enum&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Unknown,&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Union,&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Intersection,&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; RelativeComplement,&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; And,&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Conjunction = And&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;static&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; PerformOperation(&lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; one, &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; two, &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; operation) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; result = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;switch&lt;/span&gt; (operation) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;case&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt;.Union:&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.AddRange(one);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.AddRange(two);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;case&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt;.Intersection:&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; x &lt;span style="COLOR:blue;"&gt;in&lt;/span&gt; one) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; (two.Contains(x)) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.Add(x);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;case&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt;.RelativeComplement:&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; x &lt;span style="COLOR:blue;"&gt;in&lt;/span&gt; one) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; (!two.Contains(x)) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.Add(x);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;case&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt;.And:&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; x &lt;span style="COLOR:blue;"&gt;in&lt;/span&gt; one) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; (two.Contains(x)) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.Add(x);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;default&lt;/span&gt;:&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;throw&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;operation&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;This code works; but it&amp;#39;s far from cohesive.&amp;nbsp; There are many combinations of execution paths this method can take, and each path is unrelated to the other paths.&amp;nbsp; For example, the Union case has no relation to any of the other cases.&amp;nbsp; Methods like this are also hard to maintain and prone to errors.&amp;nbsp; Obviously if another member were added to Operation PerformOperation would have to change--making PerformOperation tightly coupled to Operation--not very object oriented.&lt;/p&gt;
&lt;p&gt;This can be made more object-oriented by through Dependency Inversion and the Strategy Pattern:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Set2&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;abstract&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;abstract&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; Execute(&lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; left, &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; right);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;UnionOperation&lt;/span&gt; : &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;override&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; Execute(&lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; left, &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; right) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; result = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.AddRange(left);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.AddRange(right);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IntersectionOperation&lt;/span&gt; : &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;override&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; Execute(&lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; left, &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; right) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; result = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; x &lt;span style="COLOR:blue;"&gt;in&lt;/span&gt; left) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; (right.Contains(x)) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.Add(x);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;RelativeComplementOperation&lt;/span&gt; : &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;override&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; Execute(&lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; left, &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; right) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; result = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; x &lt;span style="COLOR:blue;"&gt;in&lt;/span&gt; left) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; (!right.Contains(x)) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.Add(x);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;AndOperation&lt;/span&gt; : &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;override&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; Execute(&lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; left, &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; right) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; result = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; x &lt;span style="COLOR:blue;"&gt;in&lt;/span&gt; left) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; (right.Contains(x)) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.Add(x);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;static&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; PerformOperation(&lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; left, &lt;span style="COLOR:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; right, &lt;span style="COLOR:#2b91af;"&gt;Operation&lt;/span&gt; operation) {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;return&lt;/span&gt; operation.Execute(left, right);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Now each&amp;nbsp;operation is encapsulated, explicit, PerformOperation&amp;nbsp;need&amp;nbsp;not change as new strategies are added, and we&amp;#39;ve completely avoided the InvalidOperationException.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f06%2f10%2ffundamentals-of-ood-part-3-method-cohesion.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f06%2f10%2ffundamentals-of-ood-part-3-method-cohesion.aspx" border="0" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1633691" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Design_2F00_Coding+Guidance/default.aspx">Design/Coding Guidance</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/OOD/default.aspx">OOD</category></item><item><title>Fundamentals of OOD, Part 2 - Encapsulation Scope</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/05/08/fundamentals-of-ood-part-2-encapsulation-scope.aspx</link><pubDate>Thu, 08 May 2008 20:01:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1617693</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1617693</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/05/08/fundamentals-of-ood-part-2-encapsulation-scope.aspx#comments</comments><description>&lt;p&gt;Let&amp;#39;s look at the ubiquitous Person concept.&amp;nbsp; It might seem logical that an application that deals with people should have a Person interface for classes to implement.&amp;nbsp; For example:&lt;/p&gt;
&lt;p&gt;public interface IPerson&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String GivenName { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String SurName { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IAddress Address { get; set; }&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;At first glance this seems fine.&amp;nbsp; The IPerson interface defines attributes that the application uses with most scenarios dealing with types of IPerson, it&amp;#39;s &amp;quot;well encapsulated&amp;quot;.&amp;nbsp; But, the person concept is much more broad than what IPerson is modeling.&amp;nbsp; IPerson hasn&amp;#39;t fully encapsulated the person concept.&amp;nbsp; A person could have parents, age, weight, height, etc.&amp;nbsp; The application doesn&amp;#39;t need this information so it&amp;#39;s narrowed the concept of person to fit its needs. This is an incomplete abstraction.&amp;nbsp; The type that the application needs (that is currently IPerson) should be fully abstract.&amp;nbsp; Based on the IPerson interface, a better abstraction would be ILocatableIndividual.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s look at the opposite of not fully encapsulating a concept.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Let&amp;#39;s look at another common concept, the Invoice:&lt;/p&gt;
&lt;p&gt;public class Invoice&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Customer BillToCustomer { get;set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Customer ShipToCustomer { get;set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Datetime InvoiceDate;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ICollection&amp;lt;InvoiceItem&amp;gt; InvoiceItems { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Single ShippingAndHandlingPrice { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Single CalculateSubTotal();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Single CalculateTotal();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Single CalculateGrandTotal();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PurchaseOrder PurchaseOrder { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void Print();&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Again, seems like a reasonable encapsulation; but it has an issue.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;There&amp;#39;s a fundamental principle of OOD called the Single Responsibility Principle.&amp;nbsp; Robert Martin interprets the principle as &amp;quot;there should never be more than one reason for a class to change.&amp;quot;.&amp;nbsp; A class should model a single abstraction.&amp;nbsp; For that abstraction to remain abstract the modeled class should have a single responsibility.&amp;nbsp; In the case of Invoice, the invoice concept should only model behaviours and attributes of an invoice.&amp;nbsp; An invoice does not print, something or someone else prints it.&amp;nbsp; With the above Invoice definition should printing need to change Invoice must also change, event though what the invoice *is* doesn&amp;#39;t change.&amp;nbsp; Invoice is now coupled to how printing occurs.&amp;nbsp; In Martin&amp;#39;s terms, Invoice now has two reasons for it to change: when invoice attributes change, and when how printing occurs changes.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The Invoice class should be refactored by moving the Print method to another class.&amp;nbsp; Likely this would involve MVC, or MVP where a view would be responsible for the &amp;quot;printed view&amp;quot; and a controller or presenter would be responsible for communicating with the model (Invoice).&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f05%2f08%2ffundamentals-of-ood-part-2-encapsulation-scope.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f05%2f08%2ffundamentals-of-ood-part-2-encapsulation-scope.aspx" border="0" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1617693" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Design_2F00_Coding+Guidance/default.aspx">Design/Coding Guidance</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>Fundamentals of Object-Oriented Design (OOD) Part 1</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/05/01/fundamentals-of-object-oriented-design-ood-part-1.aspx</link><pubDate>Fri, 02 May 2008 03:50:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1611300</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1611300</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/05/01/fundamentals-of-object-oriented-design-ood-part-1.aspx#comments</comments><description>&lt;p&gt;With increased usage of patterns and situationally specific strategies, people sometimes lose sight of the concepts and principles behind these patterns and strategies and fail to follow them when they&amp;#39;re not using patterns or strategies.&amp;nbsp; I feel it&amp;#39;s good to periodically review the fundamental concepts and first principles.&lt;/p&gt;
&lt;p&gt;Object Oriented Design (OOD) attempts to help with the complexity of designing, writing, and maintaining software.&amp;nbsp; It attempts to allow building of software by modeling real-world objects.&amp;nbsp; As with any tool, it can be used improperly, but OOD attempts to facilitate simplicity, robustness, flexibility, etc..&amp;nbsp; OOD has many fundamental concepts.&amp;nbsp; Some of these concepts include modularity, encapsulation, and abstraction.&amp;nbsp; OOD deals with modeling behaviour and attributes of real-world objects.&lt;/p&gt;
&lt;p&gt;Modularity is a technique of composing software from separate parts.&amp;nbsp; At the lowest level of an Object-Oriented Programming Language (OOPL), this is the definition of a type (a class or a struct in C#/C++).&amp;nbsp; Depending on the platform there may be various other levels of modularity.&amp;nbsp; In C#, for example, modularity can occur at other levels like module (source file), namespace, netmodule, assembly, etc.&amp;nbsp; Modularity is a form of encapsulation.&lt;/p&gt;
&lt;p&gt;Encapsulation is a technique of hiding implementation details, grouping them together.&amp;nbsp; In OOPL, the lowest level of encapsulation is the type level (again, class/struct in C#/C++).&amp;nbsp; Implementation details (data) is separated from behaviour of a type.&amp;nbsp; In some OOPLs both behaviour and attributes (properties, for example in C#) are separated from behaviour.&amp;nbsp; This allows clients to decouple or to be not dependant on those implementation details. Encapsulation is a form of abstraction.&lt;/p&gt;
&lt;p&gt;The ability to encapsulate related behaviour, attributes, and implementation allows programmers to utilize abstraction.&amp;nbsp; Abstraction facilitates separation.&amp;nbsp; OOPLs allows programmers to keep concepts separate by abstracting them from one another.&amp;nbsp; Keeping abstract concepts separate allows these concepts to evolve and be used independently.&amp;nbsp; Properly designed types allow abstraction; a File class can abstract the file system away from a particular file, for example.&amp;nbsp; The file system is part-and-parcel when dealing with files; but while dealing with a File object, it is abstracted away.&lt;/p&gt;
&lt;p&gt;Good OOD has as little dependencies between parts as possible.&amp;nbsp; This is called lack of coupling.&amp;nbsp; One part that uses another part means the part depends on that other part.&amp;nbsp; Changes to the second affect the first.&amp;nbsp; Good OOD also has parts that contain related behaviour and attributes.&amp;nbsp; This is called cohesion.&amp;nbsp; If all the behaviour and attributes are generally used together in each scenario, the part has high cohesion.&amp;nbsp; If only some behaviour or attributes are used in each scenario, the part has low cohesion and likely should be split up.&amp;nbsp; Some people only view cohesion and coupling at the class level, I&amp;#39;ve purposely said &amp;quot;part&amp;quot; because I believe these concepts need to live throughout the modeled system, from class-level details, to modules, to namespace, to assemblies, to layers, to components, to systems, etc.&lt;/p&gt;
&lt;p&gt;I will continue&amp;nbsp;this series with the fundamental OOD principles that all good patterns should enforce.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f05%2f01%2ffundamentals-of-object-oriented-design-ood-part-1.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f05%2f01%2ffundamentals-of-object-oriented-design-ood-part-1.aspx" border="0" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1611300" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Design_2F00_Coding+Guidance/default.aspx">Design/Coding Guidance</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>Overcoming problems with MethodInfo.Invoke of methods with by-reference value type arguments</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/04/29/overcoming-problems-with-methodinfo-invoke-of-methods-with-by-reference-value-type-arguments.aspx</link><pubDate>Tue, 29 Apr 2008 14:08:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1608438</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1608438</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/04/29/overcoming-problems-with-methodinfo-invoke-of-methods-with-by-reference-value-type-arguments.aspx#comments</comments><description>&lt;p&gt;I ran into an interesting problem on the Forums recently.&amp;nbsp; Basically, when&amp;nbsp;you use MethodInfo.Invoke to invoke a method with by-reference value type arguments you can&amp;#39;t have the invoked method update a variable/argument.&amp;nbsp; The problem is, when you invoke the method the parameter is passed to the MethodInfo.Invoke via an object array.&amp;nbsp; Since we&amp;#39;re dealing with a value type, the original&amp;nbsp;value type&amp;nbsp;is boxed and the invoked method actually updates the array element, not the original object (as it would with reference types).&amp;nbsp; For example:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System.Reflection.Emit;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System.Reflection;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System.Diagnostics;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;namespace&lt;/span&gt; InvokeTesting&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Program&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;private&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;const&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; testValue = 10;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;static&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; TestMethod(&lt;span style="COLOR:blue;"&gt;ref&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; i)&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; i = testValue;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;static&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR:blue;"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;MethodInfo&lt;/span&gt; methodInfo = &lt;span style="COLOR:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="COLOR:#2b91af;"&gt;Program&lt;/span&gt;).GetMethod(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;TestMethod&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Static | &lt;span style="COLOR:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Public);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; i = 0;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;object&lt;/span&gt;[] parameters = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;object&lt;/span&gt;[] {i};&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; methodInfo.Invoke(&lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;, parameters);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// original variable isn&amp;#39;t updated&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;Debug&lt;/span&gt;.Assert(i == 0);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// array element is updated:&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;Debug&lt;/span&gt;.Assert((&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;)parameters[0] == testValue);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// copy updated value to original variable&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; i = (&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;)parameters[0];&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;Debug&lt;/span&gt;.Assert(i == testValue);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Of course, the only &amp;quot;workaround&amp;quot; is to get the new value out of the array.&lt;/p&gt;
&lt;p&gt;Since essentially we&amp;#39;re implementing a run-time method invocation, a &amp;quot;better&amp;quot; way would be to create a dynamic method to make the call.&amp;nbsp; A dynamic method is a method created at run-time (whose body is generated through ILGenerator.Emit et al) and invoked via a delegate.&amp;nbsp; For example:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System.Reflection.Emit;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System.Reflection;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;using&lt;/span&gt; System.Diagnostics;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;namespace&lt;/span&gt; EmitTesting&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// delegate for void method that takes one reference parameter&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;delegate&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;OneRefParameterCallback&lt;/span&gt;&amp;lt;T&amp;gt;(&lt;span style="COLOR:blue;"&gt;ref&lt;/span&gt; T value);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Program&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;private&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;const&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; testValue = 10;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;static&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; TestMethod(&lt;span style="COLOR:blue;"&gt;ref&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; i)&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; i = testValue;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;static&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR:blue;"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// create a dynamic method object with arbitrary name &amp;quot;Caller&amp;quot;, void return (null), and one parameter &amp;quot;ref int&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;DynamicMethod&lt;/span&gt; caller = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;DynamicMethod&lt;/span&gt;(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;Caller&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;, &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Type&lt;/span&gt;[] { &lt;span style="COLOR:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;).MakeByRefType() }, &lt;span style="COLOR:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;Program&lt;/span&gt;).Module);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;ILGenerator&lt;/span&gt; ilGenerator = caller.GetILGenerator();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// emit ldarg.0&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ilGenerator.Emit(&lt;span style="COLOR:#2b91af;"&gt;OpCodes&lt;/span&gt;.Ldarg_0);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// emit call void EmitTesting.Program::TestMethod(int32&amp;amp;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;MethodInfo&lt;/span&gt; mi = &lt;span style="COLOR:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;Program&lt;/span&gt;).GetMethod(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;TestMethod&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Static | &lt;span style="COLOR:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Public, &lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;,&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Type&lt;/span&gt;[] { &lt;span style="COLOR:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;).MakeByRefType() }, &lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ilGenerator.Emit(&lt;span style="COLOR:#2b91af;"&gt;OpCodes&lt;/span&gt;.Call, mi);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// emit ret&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ilGenerator.Emit(&lt;span style="COLOR:#2b91af;"&gt;OpCodes&lt;/span&gt;.Ret);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;OneRefParameterCallback&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt; callback =&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="COLOR:#2b91af;"&gt;OneRefParameterCallback&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;)caller.CreateDelegate(&lt;span style="COLOR:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;OneRefParameterCallback&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;int&lt;/span&gt;&amp;gt;));&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// call our emitted code with a reference parameter&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; i = 0;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; callback(&lt;span style="COLOR:blue;"&gt;ref&lt;/span&gt; i);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;Debug&lt;/span&gt;.Assert(i == testValue);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;In this&amp;nbsp;example,&amp;nbsp;the method invocation directly updates a value type variable.&amp;nbsp; This could be made more re-usable to refactoring the dynamic method creation code into it&amp;#39;s own method.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f04%2f29%2fovercoming-problems-with-methodinfo-invoke-of-methods-with-by-reference-value-type-arguments.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f04%2f29%2fovercoming-problems-with-methodinfo-invoke-of-methods-with-by-reference-value-type-arguments.aspx" border="0" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1608438" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>Upcoming C# 3 Guidance From Microsoft</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/03/13/upcoming-c-3-guidance-from-microsoft.aspx</link><pubDate>Thu, 13 Mar 2008 15:46:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1542087</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1542087</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/03/13/upcoming-c-3-guidance-from-microsoft.aspx#comments</comments><description>&lt;p&gt;Mircea Trofin has some &lt;a class="" href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx"&gt;design guidelines with regard to some C# 3 language additions&lt;/a&gt; (that I assume will make it into a&amp;nbsp;revised Framework Design Guidelines of some sort).&amp;nbsp; They more less agree with the &lt;a class="" href="http://www.code-magazine.com/Article.aspx?quickid=0801061"&gt;guidelines I published&lt;/a&gt; in Code Magazine a while ago.&amp;nbsp; There are some slight differences:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Consider &lt;/strong&gt;using extension methods in any of the following scenarios: to provide helper functionally relevant to every implementation of an interface&lt;/em&gt;... and,&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Do&lt;/strong&gt; define extension methods in the same namespace as the extended type, if the type is an interface, and if the extension methods are meant to be used in most or all cases.&lt;/em&gt;&amp;nbsp; This applies to framework designers that are publishing interfaces but also want to publish callable methods that apply to all implementation of those interfaces.&amp;nbsp; My article approaches the guidelines more from a non-framework designer.&amp;nbsp; I do agree that extension methods to extend interfaces is very useful and is probably one of the most adept use of extension methods.&amp;nbsp; Although, I think the wording of this guideline could use improvement.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Avoid&lt;/strong&gt; defining extension methods on System.Object, unless absolutely necessary&lt;/em&gt;.&amp;nbsp; Good advice.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Do not&lt;/strong&gt; define extension methods pertaining to a feature in namespaces normally associated with other features.&amp;nbsp; Instead define them in the namespace associated with the feature they belong to, or a namespace of it.&lt;/em&gt;&amp;nbsp; This is really unclear (and seems to suggest contradicting the first guideline:&amp;nbsp;&lt;em&gt;Avoid frivolous use of the extension methods feature when defining methods on a new type.&amp;nbsp; Use the canonical, language-specific means for defining type members&lt;/em&gt;).&amp;nbsp; I&amp;#39;m assuming the jist of this is, as a framework designer, don&amp;#39;t arbitrarily put extension methods in the namespace of the type the method applies to, consider putting the extension method in a more applicable namespace, if possible.&amp;nbsp; For example, if you want to declare an extension method &amp;quot;Forward&amp;quot; for Telecom.INode implementations, putting the method in a &amp;quot;Routing&amp;quot; namespace&amp;nbsp;would be better than arbitrarily putting it in the &amp;quot;Telecom&amp;quot; namespace.&amp;nbsp; I&amp;#39;ve changed the Mircea&amp;#39;s guidance slightly to use a interface in the example--which I think makes it more clear.&lt;/p&gt;
&lt;p&gt;Mircea also includes &lt;em&gt;&lt;strong&gt;Consider&lt;/strong&gt; using extension methods in any of the following scenarios:... when object model considerations would dictate taking a dependency on some assuming but taking such a dependency would break dependency management rules&lt;/em&gt;.&amp;nbsp; This means, should you need to add a method to a class but adding that method would create cyclic dependency or would cause a lower level assembly/class to be dependant on a higher level class, break the method out into another assembly as an extension method.&amp;nbsp; Use this advice with caution; I would argue that if you think you need a method like this at all (even if implemented as an extension method), you likely have some design problems and should only be considered when revising a published framework, and not when creating a new framework.&lt;/p&gt;
&lt;p&gt;Another tidbit of guidance that came about after I wrote the article and Mircea doesn&amp;#39;t mention is that &lt;a class="" href="http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx"&gt;extension methods can make writing fluent interfaces much cleaner&lt;/a&gt; by separating the state management concern of supporting a fluent interface&amp;nbsp;from the class that it applies to.&lt;/p&gt;
&lt;p&gt;Thoughts?&amp;nbsp; Any additional guidance you feel has been overlooked (with regard to extension methods and LINQ)?&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f03%2f13%2fupcoming-c-3-guidance-from-microsoft.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2008%2f03%2f13%2fupcoming-c-3-guidance-from-microsoft.aspx" border="0" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1542087" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Design_2F00_Coding+Guidance/default.aspx">Design/Coding Guidance</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>"Object is currently in use elsewhere" error.</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/01/28/quot-object-is-currently-in-use-elsewhere-quot-error.aspx</link><pubDate>Mon, 28 Jan 2008 17:04:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1484207</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1484207</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/01/28/quot-object-is-currently-in-use-elsewhere-quot-error.aspx#comments</comments><description>&lt;p&gt;I was debugging what I thought was a strange exception the other day.&amp;nbsp; The exception was an &lt;font face="courier new,courier"&gt;InvalidOperationException&lt;/font&gt; and the message was &amp;quot;Object is currently in use elsewhere&amp;quot;.&amp;nbsp; Unless you&amp;#39;re familiar with this exception, it really doesn&amp;#39;t offer much as to why the exception is occurring.&amp;nbsp; There seems to be several stale threads on the Web about this issue, so I&amp;#39;d thought I&amp;#39;d post about it.&lt;/p&gt;
&lt;p&gt;As it turns out it had to do with some code that was PInvoking some native graphics functions and the interaction with the WinForm that was hosting the drawing surface was to blame.&lt;/p&gt;
&lt;p&gt;What&amp;#39;s really happening with &amp;quot;Object is currently in use elsewhere&amp;quot; is that GDI+ is complaining that the device context (DC)&amp;nbsp;that it is trying to use is already &amp;quot;in use&amp;quot;.&amp;nbsp; With WinForms, this generally means there is a recursive &lt;font face="courier new,courier"&gt;Graphics.GetHdc&lt;/font&gt; occurring.&amp;nbsp; &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt; must match a &lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt; before any other &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;.&amp;nbsp; Recursive means you have something like &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;-&amp;gt;ReleaseHdc, instead of &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;.&amp;nbsp; Another possibility is that there is a missing call to&amp;nbsp;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;. (i.e. &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;)&lt;/p&gt;
&lt;p&gt;Now, in my case there was some seemingly innocuous code like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;SafeNativeMethods&lt;/span&gt;.DrawSomeStuff(e.Graphics.GetHdc(), parameters);&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Graphics.DrawString(text, &lt;span style="COLOR:blue;"&gt;this&lt;/span&gt;.Font, &lt;span style="COLOR:#2b91af;"&gt;Brushes&lt;/span&gt;.Black, point);&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;...no matching &lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;(), before the &lt;font face="courier new,courier"&gt;DrawString&lt;/font&gt; call.&lt;/p&gt;
&lt;p&gt;The fix turned out to be really simple:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;try&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;SafeNativeMethods&lt;/span&gt;.DrawSomeStuff(e.Graphics.GetHdc(), parameters);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;finally&lt;/span&gt; &lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Graphics.ReleaseHdc();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;/div&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Graphics.DrawString(text, &lt;span style="COLOR:blue;"&gt;this&lt;/span&gt;.Font, &lt;span style="COLOR:#2b91af;"&gt;Brushes&lt;/span&gt;.Black, point);&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;You can also encounter this exception if you&amp;#39;re drawing to a form from multiple threads.&amp;nbsp; You&amp;#39;ll likely also be encountering a cross-threading exception as well.&amp;nbsp; The solution in this case is to not use multiple threads when accessing a form, including drawing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1484207" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>Formatting/parsing for a specific culture redux</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/01/22/formatting-parsing-for-a-specific-culture-redux.aspx</link><pubDate>Tue, 22 Jan 2008 23:35:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1442456</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1442456</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/01/22/formatting-parsing-for-a-specific-culture-redux.aspx#comments</comments><description>&lt;p&gt;In &lt;a class="" href="http://msmvps.com/blogs/peterritchie/archive/2007/12/27/formatting-parsing-for-a-specific-culture.aspx"&gt;recent blog post&lt;/a&gt; I detailed how creating a culture via the CultureInfo constructor could actually create a user-overridden culture--which could be completely different than the culture that you&amp;#39;ve requested by name.&amp;nbsp; Fortunately there&amp;#39;s a way of overriding the user override (apologies for&amp;nbsp;overloading &amp;quot;override&amp;quot;) by supplying&amp;nbsp;the boolean value &amp;quot;false&amp;quot; in&amp;nbsp;a CultureInfo overload.&lt;/p&gt;
&lt;p&gt;As &lt;a class="" href="http://gregbeech.com/blogs/tech/"&gt;Greg Beech&lt;/a&gt; &lt;a class="" href="http://msmvps.com/blogs/peterritchie/archive/2007/12/27/formatting-parsing-for-a-specific-culture.aspx#1442156"&gt;commented&lt;/a&gt;, there&amp;#39;s another method to create a culture--&lt;a class="" href="http://msdn2.microsoft.com/en-us/library/system.globalization.cultureinfo.createspecificculture(VS.80).aspx"&gt;System.Globalization.CultureInfo.CreateSpecificCulture&lt;/a&gt;.&amp;nbsp; This sounds like it does exactly what you might expect and creates a &amp;quot;specific&amp;quot; culture.&amp;nbsp; Unfortunately, this method too violates the &lt;a class="" href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment"&gt;principle of least astonishment&lt;/a&gt; and creates a culture that uses the user-overridden values when the culture name matches that of the user&amp;#39;s current culture.&lt;/p&gt;
&lt;p&gt;CreateSpecificCulture does not, as far as I can tell, have an overload/alternative to you allow to to force a &amp;quot;specific&amp;quot; culture, so the problem is much worse with CreateSpecificCulture.&amp;nbsp; I&amp;#39;ve gone ahead and logged a bug for it on Connect: &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=321241"&gt;https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=321241&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In case you&amp;#39;re wondering why this is more serious, consider the following following block of code that formats a date value as text:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt; ci;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; text;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ci = System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;en-us&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; text = &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt;.Format(ci, &lt;span style="COLOR:#a31515;"&gt;&amp;quot;{0:d}&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;DateTime&lt;/span&gt;.Now);&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And that text&amp;nbsp;is transmitted to another application (or another session of the same application, e.g. serialization), potentially in another locale, to be&amp;nbsp;parsed with the following&amp;nbsp;block of code:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt; ci;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ci = System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;en-us&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; text = ReadRecord();&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;DateTime&lt;/span&gt; now = &lt;span style="COLOR:#2b91af;"&gt;DateTime&lt;/span&gt;.Parse(text, ci);&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If either (or) the user&amp;#39;s current culture is set to &amp;quot;English (United States)&amp;quot;&amp;nbsp;and they&amp;#39;ve overridden the currency format the short date (say from &amp;quot;M/d/yyyy&amp;quot; to &amp;quot;d/M/yyyy&amp;quot;) will randomly result in the wrong date.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1442456" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+2.0/default.aspx">.NET 2.0</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Framework+Bugs/default.aspx">Framework Bugs</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>Formatting/parsing for a specific culture</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/12/27/formatting-parsing-for-a-specific-culture.aspx</link><pubDate>Thu, 27 Dec 2007 15:47:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1422142</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1422142</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2007/12/27/formatting-parsing-for-a-specific-culture.aspx#comments</comments><description>&lt;p&gt;Sometimes you may want to use a specific format for formatting and parsing of textual data.&amp;nbsp; The easiest way to do this is to select a specific culture and use that with formatting and parsing methods.&amp;nbsp; Unfortunately, the CultureInfo constructor that just takes the name of the culture defaults to accepting any overrides the current user has set in Regional and Language Options.&lt;/p&gt;
&lt;p&gt;For example, if in Regional and Language Options you have English (United States) selected and you&amp;#39;ve changed the currency symbol to be something other than the dollar sign ($), the following won&amp;#39;t use the dollar sign:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;Decimal&lt;/span&gt; d = 2123.5M;&lt;/pre&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; text = &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt;.Format(&lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt;(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;en-us&amp;quot;&lt;/span&gt;), &lt;span style="COLOR:#a31515;"&gt;&amp;quot;{0:c}&amp;quot;&lt;/span&gt;, d);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you want to use a specific culture, you must&amp;nbsp;use the CultureInfo&amp;nbsp;constructor&amp;nbsp;overload that accepts a&amp;nbsp;Boolean so you&amp;nbsp;can tell it not to use the user overrides.&amp;nbsp; For example, the following &lt;em&gt;will&lt;/em&gt;&amp;nbsp;result in a dollar sign, despite what the user has done in Regional and Language Options:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;Decimal&lt;/span&gt; d = 2123.5M;&lt;/pre&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; text = &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt;.Format(&lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt;(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;en-us&amp;quot;&lt;/span&gt;&lt;strong&gt;&lt;font style="BACKGROUND-COLOR:#ffff00;"&gt;, &lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;&lt;/font&gt;&lt;/strong&gt;), &lt;span style="COLOR:#a31515;"&gt;&amp;quot;{0:c}&amp;quot;&lt;/span&gt;, d);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1422142" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item></channel></rss>