<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CE8HRnk8fSp7ImA9WhBXGUo.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388</id><updated>2013-04-03T12:37:17.775+05:30</updated><category term="FCL" /><category term="CLR" /><category term="WMI" /><category term="WinForms" /><category term="OOPS" /><category term="C#.NET" /><category term="BCL" /><title>Ideas on Hand</title><subtitle type="html">Knowledge sharing on .NET</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://adavesh.blogspot.com/" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/IdeasOnHand" /><feedburner:info uri="ideasonhand" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>IdeasOnHand</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><entry gd:etag="W/&quot;CE8EQHk5fSp7ImA9WhBXGUo.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-9159235328031775302</id><published>2013-04-03T12:36:00.002+05:30</published><updated>2013-04-03T12:36:41.725+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-03T12:36:41.725+05:30</app:edited><title>MVP of the year</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
On&amp;nbsp; Thursday, January 24, 2013, I was a surprised to see my inbox that I am awarded as MVP of the year 2012. I am one among the 32 people awarded as MVP of the year and only person from India. It feels great to receive such a great award. Here is a official site that tells the story,&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://mvp.microsoft.com/en-us/story/Developer%20MVPs%20of%20the%20Year-20130221205159" target="_blank"&gt;Developer MVPs of the Year&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Thank you Microsoft for such a great honor. I also thank my MVP lead Tanmay Kapoor and Biplab Paul for their support.&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/R2dCa-SZ6CU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/9159235328031775302/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2013/04/mvp-of-year.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/9159235328031775302?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/9159235328031775302?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/R2dCa-SZ6CU/mvp-of-year.html" title="MVP of the year" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2013/04/mvp-of-year.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEUCSHY5fyp7ImA9WhJaEEo.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-8045612209030553549</id><published>2012-09-30T14:46:00.004+05:30</published><updated>2012-10-01T12:54:29.827+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-01T12:54:29.827+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="CLR" /><title>Creating RCW Interops from ActiveX (OCX) Controls</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
Many times we need to use ActiveX controls (ocx) developed  in VB6 or C++ in our .NET applications. As you might aware of that, we can't directly use ActiveX components in .NET application. So, we need to create wrapper assemblies around the ActiveX components which we call Runtime Callable Wrappers as I have already talked about that in my previous posts. I am not going to discuss details of RCW, but I will show how to generate the RCWs.&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
You can generate RCW in two ways&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
1. Using Visual Studio&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
2. Using Tools TlbImp.exe and AxImp.exe.&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;Using Visual Studio to generate RCW: &lt;/strong&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;&lt;/strong&gt; &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
This is the simplest way of generating RCWs. Then in your project, open the toolbar, right click on it and select 'Choose controls'. Browse the .ocx file and click OK. &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-4NRo27WNqVY/UGgM4mhr2PI/AAAAAAAAASI/LckZ2Vvt1TQ/s1600/Command+Prompt.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="395" src="http://4.bp.blogspot.com/-4NRo27WNqVY/UGgM4mhr2PI/AAAAAAAAASI/LckZ2Vvt1TQ/s640/Command+Prompt.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Now open the 'obj' folder present inside the project folder and you will see two interop dlls are generated namely AxInterop.&amp;lt;FileName&amp;gt;.dll and &amp;lt;FileName&amp;gt;.dll. For example, if the control name is MyControl.OCX then generated assemblies will be AxMyControls.dll and MyControls.dll. The prior one contains the visible components of the user control that can be placed on the form. And later one contains the types.&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-bfxPvXWp2pE/UGgNiSivoOI/AAAAAAAAASQ/AzpB020rzxQ/s1600/Command+Prompt.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="514" src="http://1.bp.blogspot.com/-bfxPvXWp2pE/UGgNiSivoOI/AAAAAAAAASQ/AzpB020rzxQ/s640/Command+Prompt.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;Using Tools to generate RCW:&lt;/strong&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Above method of generating RCW fulfils most of the times. However, if you want to have some control over how the interops are generated like default namespace, strong naming etc., you can't do much with Visual Studio. However, for that you can use framework tools to generate RCW yourself.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
AxImp.exe and TlbImp.exe are two such tools that ship with NET Framework SDK. In fact AxImp.exe alone is sufficient to generate required interops. We will how that works now. Suppose you have an ActiveX control called MyControl.OCX. Open the visual studio command prompt, change the directory to the folder where MyControl.ocx resides and run the below command.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;span style="background-color: #eeeeee;"&gt;AXIMP MyControl.OCX&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;&lt;span style="font-family: Courier New;"&gt;&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
This will generate two interops AxMyControl.dll and MyControl.dll. The default namespace in the prior one will be AxMyControl and default namespace in MyControl.dll will be MyControl. If you want to change the out putfile name to AxInterop.MyControl.dll, you can run the below command,&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee;"&gt;AXIMP MyControl.OCX /OUT:AxInterop.MyControl.dll&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
This will generate AxInterop.MyControl.dll and MyControl.dll. You can see that when using AXIMP tool, you do not have any control over other interop dll i.e. MyControl.dll. You also do not have control over default namespace name. So, if I want to give your own names say AxInterop.MyControl.dll and Interop.MyControl.dll with custom namespaces say LegacyControls, then I will do it as below:&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
First generate  the interop assembly containign type definitions using TLBIMP tool as below,&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;TLBIMP MyControl.OCX /OUT:Interop.MyControl.dll /namespace:LegacyControls&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
If you want the generated assembly to be strong named, you can supply the strong name key file to above command (as below)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee;"&gt;TLBIMP MyControl.OCX ... /KEYFILE:MyCompanyKey.snk&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Now generate the ActiveX interop using AXIMP tool using the above generated RCW,&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee;"&gt;AXIMP MyControl.OCX /OUT:AxInterop.MyControl.dll /RCW:Interop.MyControl.dll&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
and of course you can sign this assembly just like as explained before.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-v8Xp1mbg3Ck/UGgMBxNfflI/AAAAAAAAASA/ifqe1nPq-js/s1600/Command+Prompt.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="272" src="http://4.bp.blogspot.com/-v8Xp1mbg3Ck/UGgMBxNfflI/AAAAAAAAASA/ifqe1nPq-js/s640/Command+Prompt.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Now you have generated the two interops AxInterop.MyControl.dll and Interop.MyControl.dll with having namespace AxLegacyControls and LegacyControls  respectively.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Note: You must register the OCX before you can use the generated interops.&lt;/div&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/wDMAAJxulMI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/8045612209030553549/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2012/09/generating-rcw-interop-assemblies-from.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/8045612209030553549?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/8045612209030553549?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/wDMAAJxulMI/generating-rcw-interop-assemblies-from.html" title="Creating RCW Interops from ActiveX (OCX) Controls" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-4NRo27WNqVY/UGgM4mhr2PI/AAAAAAAAASI/LckZ2Vvt1TQ/s72-c/Command+Prompt.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2012/09/generating-rcw-interop-assemblies-from.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUcMQXwyfyp7ImA9WhJaFU0.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-7633704706254884012</id><published>2012-06-15T13:33:00.003+05:30</published><updated>2012-10-06T11:28:00.297+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-06T11:28:00.297+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="OOPS" /><title>Interface Vs. Abstract Class, Why and When</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Recently many people asked me this question - what is the difference between an abstract class and an interface? - This is a typical interview question and you can easily find answers like below in any .NET book.&lt;/div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
abstract classes can contain pure abstract methods as well as concrete methods. But all methods in an interface are implicitly abstract&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
Methods in an abstract class can be protected, private, static etc. But, all items in an interface are implicitly public&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
A class can inherit multiple interfaces, but it can inherit only one abstract class.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
etc. :)&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div style="text-align: justify;"&gt;
But, in my view, one shouldn't compare interfaces and abstract classes because each of them have their own strength and weaknesses. I have also seen many people who&amp;nbsp;go dumbfounded if someone asks them&amp;nbsp;- Why do you use an interface?&amp;nbsp;Or Why do you use an abstract class? I suggest you to go through this &lt;a href="http://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx" target="_blank"&gt;MSDN&amp;nbsp;&amp;nbsp;page&lt;/a&gt;&amp;nbsp;which gives you an idea of when to use what. In this blog, I will try to explore my knowledge on these two concepts.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style="color: orange; font-size: large;"&gt;Abstract Class&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
An abstract class is just another class with abstract keyword. But, the keyword 'abstract' adds some additional features to the class like - the class can contain just method declarations, an instance of the class cannot be created. So, below one is an abstract class.&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&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;Animal&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp; public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; 
Walk()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;       &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"Walking"&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="WordSection1" style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; 
Sleep()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"Sleeping"&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="WordSection1" style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;abstract&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; 
Eat();&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
This class defines the basic characteristics of an animal like every animal sleeps, Moves, eats etc. But look at the Eat function. I have made it abstract in the sense - every animal can eat but what it eats depends upon the kind of animal.&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;Cow&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;Animal&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; 
Eat()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;       &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"Eating 
grass"&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="WordSection1" style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;Tiger&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;Animal&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; 
Eat()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;       &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"Eating 
meat"&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;   &amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
I hope above example doesn't need any explanation. You can see that abstract class&amp;nbsp;defines an &lt;strong&gt;IS-A &lt;/strong&gt;relationship. Which means Cow is an Animal, so Cow has all the basic characters of an Animal. e.g. Move and Sleep functions need not be re-defined in Cow class. &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Also note that, If I add any new behaviour to base class, child classes need not be changed at all and they simply inherit the new behaviour. For example, if I define a new behaviour 'Drink' in animal class, both the child classes simply adapt the new behaviour immediately. &lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;br /&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&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;Animal&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp; Drink()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;     &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"Drinking water"&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;abstract&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt;  Eat();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
So, visioning is not a problem when you use abstract class.&amp;nbsp;By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. Hence, If you want to create multiple versions of your component, create an abstract class. &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
One more thing to be noted that all these&amp;nbsp;2 classes&amp;nbsp;Tiger, Cow are closely related i.e. both are Animals and share common behaviour of an animal. So, when the classes are closely related and share common functionality - Use Abstract classes.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style="color: orange; font-size: large;"&gt;Interfaces&amp;nbsp;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
In contrast to abstract class, an Interface defines a &lt;strong&gt;CAN-DO &lt;/strong&gt;relationship rather than &lt;strong&gt;IS-A &lt;/strong&gt;relationship. For example, the definition of&amp;nbsp;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt;&amp;nbsp;class looks like below,&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IComparable&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;ICloneable&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;IConvertible&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/span&gt;&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
..&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
This definition tells you that - you &lt;u&gt;can&lt;/u&gt; compare string objects, you &lt;u&gt;can&lt;/u&gt; clone string objects, you &lt;u&gt;can&lt;/u&gt; convert string objects into other type.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Next thing is, when the classes are not closes related&amp;nbsp;and you still want&amp;nbsp;to have a common functionality among them - Use Interfaces.&amp;nbsp;Let's consider an example,&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IEquatable&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt; 
Equals(T other);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;: 
&lt;span style="color: #2b91af;"&gt;IEquatable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 
&lt;span style="color: blue;"&gt;string&lt;/span&gt; Name;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 
&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt; DateOfBirth;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; 
&lt;span style="color: blue;"&gt;bool&lt;/span&gt; Equals(&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt; other)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="font-family: Times New Roman; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;return&lt;/span&gt; 
other.Equals(other.Name) &amp;amp;&amp;amp; other.DateOfBirth == &lt;span style="color: blue;"&gt;this&lt;/span&gt;.DateOfBirth;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Home&lt;/span&gt; : 
&lt;span style="color: #2b91af;"&gt;IEquatable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Home&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 
&lt;span style="color: blue;"&gt;int&lt;/span&gt; HouseNumber;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 
&lt;span style="color: blue;"&gt;int&lt;/span&gt; AreaCode;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; 
&lt;span style="color: blue;"&gt;bool&lt;/span&gt; Equals(&lt;span style="color: #2b91af;"&gt;Home&lt;/span&gt; other)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt; 
other.AreaCode == &lt;span style="color: blue;"&gt;this&lt;/span&gt;.AreaCode &amp;amp;&amp;amp; 
other.HouseNumber == &lt;span style="color: blue;"&gt;this&lt;/span&gt;.HouseNumber;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Here 2 non-related classes Home and Person want to have Equality function. See that functionality 'Equals' is common between the 2 classes but the implementation is completely different. Hence, both Home &amp;amp; Person implement IEquatable interface but implement Equals method independently.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Now you can ask, if the implementations are different, what is the need of using interface? Of course, just remove the IEquatable interface and the program still works. So, why interface? Let me try to make it clear step by step.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
&lt;strong&gt;Firstly,&lt;/strong&gt; An interface acts as a CONTRACT&lt;span style="font-size: x-small;"&gt;. &lt;/span&gt;This means, any class that implements an interface agrees to implement all the methods in that&amp;nbsp;interface. Also note that, (ideally) once implemented, interface (contract) definition cannot change i.e. you cannot add new methods and modify existing ones in an interface. This is very important aspect of interfaces. This assures the consumer of your class that, the class will&amp;nbsp;implement all the methods defined in the interface contract though the implementation can change. For example, I know that Person class implements IEquatable interface. Hence I am guaranteed that Person class always contains Equals method. Hence, I am ensured that my below code always works.&lt;/div&gt;
&lt;div class="WordSection1"&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;
&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;"&gt;IEquatable&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;&amp;gt; person1 = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;(&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;"&gt;IEquatable&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;&amp;gt; person2 = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;(&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="SpellE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span class="SpellE"&gt;areTheyEqual&lt;/span&gt; = 
person1.Equals(person2);&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Having this, a new version of Person class is released and Equals method now includes a check for address of person also.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;: &lt;span style="color: #2b91af;"&gt;IEquatable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt; Name;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt; DateOfBirth;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Address&lt;/span&gt; Address;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt; Equals(&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt; other)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="font-family: Times New Roman; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;  other.Equals(other.Name) &amp;amp;&amp;amp; &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; other.DateOfBirth == &lt;span style="color: blue;"&gt;this&lt;/span&gt;.DateOfBirth &amp;amp;&amp;amp;&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;other.Address == this.Address;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
See that, though the&amp;nbsp;implementation has changed, my consumer code&amp;nbsp;&amp;nbsp;still works. So, having an interface implemented makes your class Backward compatible.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
&lt;strong&gt;Secondly, &lt;/strong&gt;interfaces are quite useful when you want call a method on dynamic object. For example, below helper function prints all the items in a collection - A List, Array, String etc.&lt;/div&gt;
&lt;div class="WordSection1"&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DisplayAllItems&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; 
collection)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; 
(&lt;span style="color: blue;"&gt;var&lt;/span&gt; item &lt;span style="color: blue;"&gt;in&lt;/span&gt; 
collection)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(item);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
In the above function, you can pass a List, array or any object that implements IEnumerable interface. Otherwise, we would need to implement the display logic for each collection type separately.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
These are&amp;nbsp;2 examples of why to use interfaces. There are other lot of reasons for using interfaces like laying out complex architectures, implementing design patterns etc. But, I hope this blog gives you an idea of why to use interfaces.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
That concludes the discussion of differentiating between interface &amp;amp; abstract class, which one to prefer &amp;amp; when. I hope you enjoyed reading my blog. Please put your feedback which help me in improving myself and also to help others.&lt;/div&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/VX2M-85sygk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/7633704706254884012/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2012/06/interface-vs-abstract-class-why-and.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/7633704706254884012?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/7633704706254884012?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/VX2M-85sygk/interface-vs-abstract-class-why-and.html" title="Interface Vs. Abstract Class, Why and When" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2012/06/interface-vs-abstract-class-why-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQBQHc7fSp7ImA9WhVaF0k.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-4294544825077810275</id><published>2012-04-04T12:39:00.000+05:30</published><updated>2012-06-15T13:49:11.905+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-06-15T13:49:11.905+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="BCL" /><title>Query KB updates installed in Window 7</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
Prior to Windows Vista, WMI's Win32_QuickFixEngineering class was used for querying the KB updates installed in the system. But, Starting with Windows Vista, win32_QuickFixEngineering returns only the updates supplied by Component Based Servicing (CBS). It will not return updates installed by MSI or Windows Updates. Read &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394391(v=vs.85).aspx" target="_blank"&gt;this&lt;/a&gt; page for more info. So, to query the updates installed via MSI or Windows Updates, you can use &lt;a href="http://msdn.microsoft.com/en-us/library/aa387287%28VS.85%29.aspx" target="_blank"&gt;Windows Update Agent Object&lt;/a&gt; Model. Here I am going to show you, how to query the installed KB updates in C#.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
We begin by adding reference to Wuapi.dll. You can do that by selecting "WUAPI 2.0 Type Library"&amp;nbsp;from COM tab in Add Reference dialog in Visual Studio.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-uy3Xqt-uS54/T3vplEGDdhI/AAAAAAAAAQw/58711SaWvzQ/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="337" nda="true" src="http://1.bp.blogspot.com/-uy3Xqt-uS54/T3vplEGDdhI/AAAAAAAAAQw/58711SaWvzQ/s400/Untitled.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Once you add reference to Wuapi.dll, you can create an object of WUApiLib.UpdateSession class. This class&amp;nbsp;cab be used by the&amp;nbsp;caller to perform operations that involve updates, such as download, install or search for updates. Once you have UpdateSession object ready, you can call QueryHistory on it to get history of installed updates. Let me show you the code directly.&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span class="SpellE"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt;WUApiLib.&lt;span style="color: #2b91af;"&gt;UpdateSession&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt; session = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;UpdateSession&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt;IUpdateSearcher&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt; us = &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;session.CreateUpdateSearcher&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;updatesC&lt;span class="SpellE"&gt;ollection&lt;/span&gt; = &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;session.QueryHistory&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt;.Empty&lt;/span&gt;, 0, &lt;span class="SpellE"&gt;us.GetTotalHistoryCount&lt;/span&gt;());&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt;for&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt; (&lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/span&gt; i = 0; i &amp;lt; &lt;span class="SpellE"&gt;updatesC&lt;span class="SpellE"&gt;ollection&lt;/span&gt;.Count&lt;/span&gt;; i++)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span class="SpellE"&gt;coll&lt;/span&gt;[i].Title);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Note that, the list of updates resulted from above code may not match with the list of updates in Add/Remove Programs window in control panel. &amp;nbsp;The missing updates might be installed via Component Based Servicing or they are non-microsoft updates.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
References:&lt;/div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
&amp;nbsp;Searching, Downloading, and Installing Updates (&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa387102(v=vs.85).aspx" target="_blank"&gt;link&lt;/a&gt;)&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
&amp;nbsp;Using the Windows Update Agent API (&lt;a href="http://msdn.microsoft.com/en-us/library/aa387287%28VS.85%29.aspx" target="_blank"&gt;link&lt;/a&gt;)&lt;/div&gt;
&lt;/li&gt;
&lt;div style="text-align: justify;"&gt;

&lt;/div&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/_SFcFdc74YA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/4294544825077810275/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2012/04/query-kb-updates-installed-in-window-7.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/4294544825077810275?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/4294544825077810275?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/_SFcFdc74YA/query-kb-updates-installed-in-window-7.html" title="Query KB updates installed in Window 7" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-uy3Xqt-uS54/T3vplEGDdhI/AAAAAAAAAQw/58711SaWvzQ/s72-c/Untitled.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2012/04/query-kb-updates-installed-in-window-7.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEcMRXYyeyp7ImA9WhVTEEo.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-4388893524928030148</id><published>2012-02-24T13:55:00.001+05:30</published><updated>2012-02-24T15:04:44.893+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-24T15:04:44.893+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="CLR" /><title>Memory Leaks in .NET Application - Don't let them slip through your eyes.</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Before talking about the main topic, I would like to briefly go through the CLR garbage collection mechanism.&amp;nbsp; When a .NET application is executed, CLR allocates a block of memory which is called managed heap. This managed heap is logically divided among 3 generations - Gen0, Gen1 and Gen2. Usually Gen0 contains the newly created and short lived objects. So, here is a quick view on what happens during garbage collection -&lt;/div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
Whenever generation 0 gets full, garbage collection occurs. During garbage collection, the garbage collector examines each object in Gen0 to know whether the object is a root. A root&amp;nbsp; object is one which has a valid reference (or simply the object is still in use). After the root examination, garbage collector collects all non-root objects and frees the memory occupied by them and the root objects which survived the garbage collection are moved to Gen1. At the end of garbage collection, Gen0 will be 100% empty.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
Now, suppose the application needs more memory than which is available in Gen0. So, Garbage collection must occur which collects objects in both Gen0 and Gen1. Again, the garbage collection starts from identifying root objects in Gen1. All the non-root objects are collected and their memory is reclaimed. The survived objects (roots) will be moved to Gen2. Then Garbage collector collects Gen0 objects as explained in Step1.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
Sometimes Later the application might require more memory than which is available in Gen0 &amp;amp; Gen1 together. So, the garbage collection must occur on all three generations. So, Garbage Collector starts examining the objects in Gen2. All non-root objects are collected and their memory is freed-up. The survived objects are going to remain in Gen2 only. Then Garbage collector collects Gen1 and Gen0 objects as explained in Step1 &amp;amp; 2.&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;&lt;em&gt;What is a Memory Leak?&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
What we can see from the above explanation is that until an object has a root it is going to remain in memory and is always promoted to higher generation. Having this said, let's see what is memory leak. &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Consider you have a class which holds a reference to&amp;nbsp;an unmanaged handle as shown below.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
class UnmanagedClass&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
{&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IntPtr handle;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Int32[] someBigArray = new Int32[200000];&amp;nbsp; //a dummy array to hold sufficiently large memory.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public UnmanagedClass()&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div style="text-align: justify;"&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; handle = GetUnmanagedHandle(); // consider this method returns an unmanaged handle&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
}&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Then I will create an instance of above class as below,&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
private void MemoryLeakTest()&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
{&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0;&amp;nbsp; i &amp;lt; 1000000; i++)&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;
&lt;div style="text-align: justify;"&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;String str = new String();&lt;/div&gt;
&lt;div style="text-align: justify;"&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; UnmanagedClass uc = new UnmanagedClass();&amp;nbsp; //doesn't &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
}&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
In the above function, after every loop, both sr &amp;amp; uc become eligible for garbage collection. Suppose, after 5 loops, generation 0 becomes full and GC must run. See that after 5 loops there are 5 string objects and 5 UnmanagedClass objects are created on heap. Garbage collection starts and sees that all five string objects have no roots. So, it frees the memory occupied by the string objects. Then it starts examiniting the Unmanaged class objects. But, each uc object has an unmanaged root. Since GC cares for only managed object but not unmanaged objects, it will not examine the unmanaged handle. Hence, it treats all 5 UnmanagedClass instances to be roots and moves them to Generation1. At this point the generations look like below.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-hWAqIFv8vho/T0c3-WtKgNI/AAAAAAAAAQU/moq1WzFKlOw/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="181" lda="true" src="http://1.bp.blogspot.com/-hWAqIFv8vho/T0c3-WtKgNI/AAAAAAAAAQU/moq1WzFKlOw/s400/Untitled.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Ultimately Generation0 becomes empty and the loop starts executing again. Now, again after 5 loops Gen0 becomes full and GC occurs. As explained previously, all 5 Unmanaged objects are treated to be roots and they are moved to Generation1. But, there may not be sufficient space in Gen1 to accumulate all objects that survived in Gen0 collection. So, GC has to run on Gen1 as well. Hence, GC starts examining UnmanagedClass objects in Gen1. Again GC sees that they contain a valid handle hence they are moved to Gen2. At this point Gen1 and Gen2 have 5 UnmanagedClass objects each and Gen0 is empty.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-7BlY_W3TZJg/T0c4lmFZNuI/AAAAAAAAAQc/rWUN2tE0xgg/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="175" lda="true" src="http://2.bp.blogspot.com/-7BlY_W3TZJg/T0c4lmFZNuI/AAAAAAAAAQc/rWUN2tE0xgg/s400/Untitled.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
In the same way, after another 5 loops, the 5 UnmanagedClass objects in Gen1 will survive GC and moved to Gen2 and Gen0 objects will be moved to Gen1 and the picture looks like below,&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-TxiZosWzubw/T0c6UnNqq2I/AAAAAAAAAQk/Y509Ceq4lwA/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="168" lda="true" src="http://3.bp.blogspot.com/-TxiZosWzubw/T0c6UnNqq2I/AAAAAAAAAQk/Y509Ceq4lwA/s400/Untitled.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
You can see that now the generation2 is getting full and it has to be garbage collected. But, again, all the objects in contain an unmanaged handle and they will not be collected at all. Hence, at the next GC, there will be no memory to&amp;nbsp;left&amp;nbsp;in Gen2 to move any objects into it.&amp;nbsp;At this point, you can say that the application is leaking memory.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
So, if the application continues to run, at some point of time, there will be no memory left to allocate any objects and CLR will throw OutOfMemoryException and process terminates.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;em&gt;&lt;strong&gt;How to avoid Memory Leaks?&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;ul&gt;
&lt;li&gt;If your class has an unmanaged handle, implement Finalize and&amp;nbsp;Dispose pattern to release the unmanaged handle. &lt;a href="http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.71).aspx" target="_blank"&gt;This&lt;/a&gt; article gives you an overview of Dispose pattern and &lt;a href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx" target="_blank"&gt;this&lt;/a&gt; page shows you how dispose an unmanaged handle. &lt;/li&gt;
&lt;li&gt;If you are a consumer of a class that implements IDisposable, as a developer, you are responsible for calling Dispose on it. Ensure, all Disposable objects are disposed or at least those objects that do not have finalizers.&lt;/li&gt;
&lt;li&gt;Avoid static collections. You might know that a type loaded in memory is never loaded until the application is shutdown. Since, static members are type members, they are going to stay in the memory always. So, use static members carefully.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
I hope you enjoyed reading this article. Happy Programming.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/MRRnlJdzgOw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/4388893524928030148/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2012/02/memory-leaks-in-net-application-dont.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/4388893524928030148?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/4388893524928030148?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/MRRnlJdzgOw/memory-leaks-in-net-application-dont.html" title="Memory Leaks in .NET Application - Don't let them slip through your eyes." /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-hWAqIFv8vho/T0c3-WtKgNI/AAAAAAAAAQU/moq1WzFKlOw/s72-c/Untitled.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2012/02/memory-leaks-in-net-application-dont.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4HSH49eCp7ImA9WhRaEEs.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-1119117639717217488</id><published>2012-02-12T21:53:00.000+05:30</published><updated>2012-02-12T21:55:39.060+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-12T21:55:39.060+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="CLR" /><title>GC.AddMemoryPressure - Working with native resources.</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
In this writing, I am going to explain how to deal with a situation where an object occupies a large amount of unmanaged memory while consuming very little managed memory. For example, suppose you are using a Bitmap object in your application. The Bitmap application can consume a lot of native memory. But your application just uses the handle to Bitmap which just uses just 4 bytes in 32 bit machines and 8 bytes in 64 bit machines.&amp;nbsp;This means, your application could create several Bitmaps before the garbage collection kicks in. But at the same time, the native memory consumption by the process can increase enormously.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Let me show you an example. In figure A below, I have allocated 2 bitmaps, each of which occupies some big amount of native memory. But you can see that managed heap just containes wrapper to the Bitmaps which occupy very less memory. I will go ahead and create 2 more bitmaps (figure-B). You can see that native memory usage is gradually increasing while there is still enough memory on managed heap. Since, there is enough memory available on the heap, garbage collection doesn't kickin. So, if some more bitmaps are created and garbage collection doesn't occur, then you might run out of native memory which can result in catastrophic failures.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-7l_6F3j0Clc/Tzfb8892_3I/AAAAAAAAAPk/SCnlSzk8j3o/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="266" sda="true" src="http://1.bp.blogspot.com/-7l_6F3j0Clc/Tzfb8892_3I/AAAAAAAAAPk/SCnlSzk8j3o/s640/Untitled.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
To deal with such problems, System.GC class&amp;nbsp; provides two static methods - AddMemoryPressure and RemoveMemoryPressure, whose signature is like below.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="WordSection1"&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span class="SpellE"&gt;AddMemoryPressure&lt;/span&gt;(&lt;span style="color: blue;"&gt;long&lt;/span&gt; &lt;span class="SpellE"&gt;bytesAllocated&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;public&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span class="SpellE"&gt;RemoveMemoryPressure&lt;/span&gt;(&lt;span style="color: blue;"&gt;long&lt;/span&gt; &lt;span class="SpellE"&gt;bytesAllocated&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
To know the advantage of these two methods, have a look at the below BitmapObject class. This class is a wrapper around Bitmap. For simplicity, the constructor accepts an image file and constructs a Bitmap. &lt;br /&gt;
&lt;br /&gt;
&lt;div class="WordSection1"&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;BitmapObject&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;System.Drawing.&lt;span style="color: #2b91af;"&gt;Bitmap&lt;/span&gt;&lt;/span&gt; _bitmap;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Int64&lt;/span&gt; _&lt;span class="SpellE"&gt;memoryPressure&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;BitmapObject&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; file, &lt;span style="color: #2b91af;"&gt;Int64&lt;/span&gt; size)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;_bitmap = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;System.Drawing.&lt;span style="color: #2b91af;"&gt;Bitmap&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;file);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;/span&gt; (_bitmap != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;_&lt;span class="SpellE"&gt;memoryPressure&lt;/span&gt; = size;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;GC&lt;/span&gt;.AddMemoryPressure&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;_&lt;span class="SpellE"&gt;memoryPressure&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;System.Drawing.&lt;span style="color: #2b91af;"&gt;Bitmap&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;GetBitmap&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;/span&gt; _bitmap;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;~&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;BitmapObject&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;/span&gt; (_bitmap != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;_&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;bitmap.Dispose&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;GC&lt;/span&gt;.RemoveMemoryPressure&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;_&lt;span class="SpellE"&gt;memoryPressure&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
Whenever you want to create an instance of System.Bitmap, you can consider creating an instance of above class instead. For instance, I want to create a Bitmap which can have&amp;nbsp;approximately 5MB size. So, I will create an instance of BitmapOject by passing fileName and size as below,&lt;br /&gt;
&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;BitmapObject&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span class="SpellE"&gt;oBitmap&lt;/span&gt; = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;BitmapObject&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"c:\\SomePicture.bmp"&lt;/span&gt;, 5 * 1024 * 1024);&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
When the constructor executes, it first creates a Bitmap and stores it reference in _bitmap. Then the constructor calls GC.AddMemoryPressure method&amp;nbsp;passing the size&amp;nbsp;(5MB) to it.&amp;nbsp;&amp;nbsp;This gives CLR a hint of how much native memory is actually occupied by the object. So, though only 4 bytes (or 8 bytes in 64 bit machines) in managed heap, clr assumes that the object actually consumes 5MB. So, suppose Managed heap is of 50MB, then creating 10 instances of BitmapObject&amp;nbsp;makes CLR think managed heap is full and hence it enforces garbage collection. When the garbage collection occurs, the finalizer method executes disposes the bitmap and removes the memory pressure.&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/atisXEKFRnA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/1119117639717217488/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2012/02/gcaddmemorypressure-working-with-native.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/1119117639717217488?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/1119117639717217488?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/atisXEKFRnA/gcaddmemorypressure-working-with-native.html" title="GC.AddMemoryPressure - Working with native resources." /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-7l_6F3j0Clc/Tzfb8892_3I/AAAAAAAAAPk/SCnlSzk8j3o/s72-c/Untitled.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2012/02/gcaddmemorypressure-working-with-native.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4DQ385eyp7ImA9WhRaEEs.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-6353052229491731058</id><published>2012-02-10T23:34:00.001+05:30</published><updated>2012-02-12T21:56:12.123+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-12T21:56:12.123+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="BCL" /><title>Contravariance: Is it really what it is?</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
By definition, contravariance referes to a situtaion where you assign base class instance to a child class reference. Let's directly see an example,&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;BaseClass&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;ChildClass&lt;/span&gt;: &lt;span style="color: #2b91af;"&gt;BaseClass&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;TestContraVariance&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &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;SomeContraVariantDelegate&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;ChildClass&lt;/span&gt; argument);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; CreateContravariantDelegate()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&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; &lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;SomeContraVariantDelegate&lt;/span&gt; cvd = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SomeContraVariantDelegate&lt;/span&gt;(EventHandlerMethod);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; EventHandlerMethod(&lt;span style="color: #2b91af;"&gt;BaseClass&lt;/span&gt; arg)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; text-align: justify;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Here, you can see the contravariance - Delegate &lt;span style="color: #a64d79;"&gt;SomeContraVariantDelegate &lt;/span&gt;expects a method that accepts &lt;span style="color: #a64d79;"&gt;ChildClass&lt;/span&gt; instance. But, the function EventHandlerMethod accepts an argument of type &lt;span style="color: #2b91af;"&gt;&lt;span style="color: #a64d79;"&gt;BaseClass&lt;/span&gt;&lt;/span&gt;. So, it appears that a base class object is assigned to a child class object which is not possible in normal scenarios. But, is it really what it seems to be? I don't think so. Let me explain,&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
In the above example, if you see carefully, the delegate calls the method but not method calls delegate. So, when delegate calls &lt;span style="color: #741b47;"&gt;EventHandlerMethod&lt;/span&gt; it sends a &lt;span style="color: #2b91af;"&gt;&lt;span style="color: #a64d79;"&gt;ChildClass&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;instance as parameter. This is perfectly valid because the method accepts &lt;span style="color: #a64d79;"&gt;BaseClass&lt;/span&gt; instance and the delegate passes &lt;span style="color: #2b91af;"&gt;&lt;span style="color: #a64d79;"&gt;ChildClass&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;instance. So, actually &lt;span style="color: #2b91af;"&gt;&lt;span style="color: #a64d79;"&gt;ChildClass&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;/span&gt;instance is assigned to &lt;span style="color: #a64d79;"&gt;BaseClass&lt;/span&gt; object but not the other way.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
So, there exists nothing like assigning assigning &lt;span style="color: #2b91af;"&gt;&lt;span style="color: #a64d79;"&gt;BaseClass&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;instance to &lt;span style="color: #2b91af;"&gt;&lt;span style="color: #a64d79;"&gt;ChildClass&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;/span&gt;object and hence no contravariance like thing exists too.&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/GigXCn8uxKU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/6353052229491731058/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2012/02/contravariance-is-it-really-what-it-is.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/6353052229491731058?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/6353052229491731058?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/GigXCn8uxKU/contravariance-is-it-really-what-it-is.html" title="Contravariance: Is it really what it is?" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2012/02/contravariance-is-it-really-what-it-is.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YCSH48fSp7ImA9WhRWEUg.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-2359624504894572713</id><published>2011-12-29T17:13:00.003+05:30</published><updated>2011-12-29T17:16:09.075+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-29T17:16:09.075+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="CLR" /><title>CLR Optimizations - String Interning</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
In my previous &lt;a href="http://adavesh.blogspot.com/2011/12/beware-of-excessive-string-objects.html" target="_blank"&gt;post&lt;/a&gt;, I explained how the immutable nature of strings can hurt the performance of your application. In this post, I am going to explain how CLR optimizes string handling through a technique called String Interning.&lt;/div&gt;
&lt;br /&gt;
To begin with, let's take a look at below simple program.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;static&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Main()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; s1 = &lt;span style="color: #a31515;"&gt;"tiger"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; s2 = &lt;span style="color: #a31515;"&gt;"tiger"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;//compare the values of s1 and s2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;valuesEqual&lt;/span&gt; = &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt;.Equals&lt;/span&gt;(s1, s2);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;//compare the references of s1 and s2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;referenceEqual&lt;/span&gt; = &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;Object&lt;/span&gt;.ReferenceEquals&lt;/span&gt;(s1, s2);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
When you execute this program, &lt;span class="SpellE"&gt;valuesEqual&lt;/span&gt; will be true which is expected to be true since both s1 and s2 contain the same value "tiger". What about the value of &lt;span class="SpellE"&gt;&lt;em&gt;referenceEquals &lt;/em&gt;&lt;/span&gt;variable? there is a twist here.&amp;nbsp;You expect &lt;em&gt;referenceEquals&lt;/em&gt; to be false because both s1 and s2 are completely different objects and hence their references should be different. But wait, the value of&amp;nbsp;&lt;span class="SpellE"&gt;&lt;em&gt;referenceEquals &lt;/em&gt;&lt;/span&gt;will also be true! &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
To proceed further, just change the value of s2 to something else say "lion" and run your program. Now, &lt;em&gt;valueEquals &lt;/em&gt;is false which is expected. Also &lt;span class="SpellE"&gt;referenceEqual&lt;/span&gt;&amp;nbsp;is false too. Now, if you are wondoring why &lt;span class="SpellE"&gt;&lt;em&gt;referenceEquals &lt;/em&gt;&lt;/span&gt;was true in first case. The answer is - it was because the result of String Interning, an optimization technique adapted by clr for string manipulation. So, let's understand what is String interning.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
When you run your application (say the above program iteself),&amp;nbsp;CLR creates an internal hash table. Initially the hash table will empty. Then, string s1 is created on heap with value "tiger". Now, an entry is made in the hash table where key will be "tiger" and value will be reference to string object created on the heap. &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-hw7gGlCWfgk/TvxDDkeCukI/AAAAAAAAAOc/2LnJb-ntImY/s1600/StringTable1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="172" rea="true" src="http://4.bp.blogspot.com/-hw7gGlCWfgk/TvxDDkeCukI/AAAAAAAAAOc/2LnJb-ntImY/s400/StringTable1.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal"&gt;
You see that s1 is created on the heap and the address (reference)&amp;nbsp;of the object is stored in hash table for "tiger". Then, the CLR sees the second instruction &lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: black; font-family: Times New Roman; font-size: small;"&gt;&lt;strong&gt;&lt;em&gt;String s2 = "tiger";&lt;/em&gt;&lt;/strong&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Now, instead of creating a new string object on heap, it first searches the hash table for the key "tiger" and it will defnitely find an entry. This means that a string "tiger" already exists on the heap whose reference is 0x100. Hence, CLR simly stores the reference&amp;nbsp;from hash table into s2. This way, creating of a new object is avoided and thereby saving memory.&lt;/div&gt;
&lt;div class="MsoNormal"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-QAy2BEi6g2k/TvxN49NF1CI/AAAAAAAAAPA/zWYoGEZdFH4/s1600/StringTable1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="172" rea="true" src="http://3.bp.blogspot.com/-QAy2BEi6g2k/TvxN49NF1CI/AAAAAAAAAPA/zWYoGEZdFH4/s400/StringTable1.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="MsoNormal"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="MsoNormal"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="text-align: justify;"&gt;
Later&amp;nbsp; if s2 is assigned a different value say "lion", then CLR will first search for the key "lion" in hash table. But it will not find any entry in hash table. Hence, a new String object will be created on heap for "lion". Also, a new entry will be made in the hash table with key being "lion" and value being reference to new object on heap.&lt;/div&gt;
&lt;div class="MsoNormal" style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-QX5yDzB_YEE/TvxNGWsJVeI/AAAAAAAAAO0/g5VyEqxEiNM/s1600/StringTable1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="172" rea="true" src="http://1.bp.blogspot.com/-QX5yDzB_YEE/TvxNGWsJVeI/AAAAAAAAAO0/g5VyEqxEiNM/s400/StringTable1.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="text-align: justify;"&gt;
Pretty interesting right? By adapting string interning mechanism, CLR efficiently controls the creation of strings. If you think that this feature is quite useful and want to take advantage of it, you can&amp;nbsp;refer MSDN for String class's static methods &lt;em&gt;Intern&lt;/em&gt; and &lt;em&gt;IsInterned. &lt;/em&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="text-align: justify;"&gt;
&lt;strong&gt;A&lt;/strong&gt;part from above said advantanges, this mechanism has also some disadvantages. The additional overhead in creating &amp;amp; maintaining hash table, repeated hash table lookups can hurt the performance of your application. If you think, string interning hurts your application performance, you can turn this feature off by supplying assembly level attribute "CompilationRelaxationAttribute" with value "CompilationRelaxation.NoStringInterning". But there is a catch here. Even if you supply this attribute, CLR may ignore this attribute and use String Interning. So, just be aware of this.&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/s1ty55vcMFo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/2359624504894572713/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/12/clr-optimizations-string-interning.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/2359624504894572713?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/2359624504894572713?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/s1ty55vcMFo/clr-optimizations-string-interning.html" title="CLR Optimizations - String Interning" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-hw7gGlCWfgk/TvxDDkeCukI/AAAAAAAAAOc/2LnJb-ntImY/s72-c/StringTable1.png" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/12/clr-optimizations-string-interning.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkQHR34zeip7ImA9WhRWEEQ.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-7858870723113454494</id><published>2011-12-28T23:13:00.002+05:30</published><updated>2011-12-28T23:15:36.082+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-28T23:15:36.082+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WMI" /><title>WBEMTEST - A simple tool to access WMI classes</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
&lt;span style="font-family: inherit;"&gt;Windows Management Instrumentation (WMI) is the repository of windows information. Basically WMI contains a whole bunch of classes where each class holds complete information about some part of windows. For example there is a class called Win32_Printer which represents a printer installed in the system. Similarly there is a Win32_Product which represents a product installed in the system. You can complete information of all WMI classes &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394554(v=vs.85).aspx" target="_blank"&gt;&lt;span style="font-family: inherit;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: inherit;"&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Whenver you decide to use WMI for accessing any piece of information, the first thing is to know which class contains the information and what are all the properties and methods present in that class. Initially, I always used to visit msdn to search for the right class and its components. This usually is quite time consuming. That time I cam across a handy tool that comes along with windows operating system itself - wbemtest.exe. This tool is quite handy - you can quickly access a class to know what and all properties it contains. Also, you can access existing istances of these classes. For example, there exist as many istannces of Win32_Processor class as the number of&amp;nbsp;processors&amp;nbsp;present in the system. If you are interested in knowing any information about a perticular processor, you can open corresponding instance. Here is how you can use wbemtest.exe to access existing instances of Win32_Processor class.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
1. Run "wbemtest.exe" either from command prompt or directly from \System32\wbem folder. This will open the window shown in below figure.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-U2oy_tfbQEc/TvtP4P_ssoI/AAAAAAAAAM8/5zy89MqXLeE/s1600/wbemtest_start.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="267" rea="true" src="http://1.bp.blogspot.com/-U2oy_tfbQEc/TvtP4P_ssoI/AAAAAAAAAM8/5zy89MqXLeE/s320/wbemtest_start.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
2. Then click "Connect" button. This will open a window (Figure B) where you have to give the namespace which contains your desired class. A namespace is just like your C# namespace, which contains many classes. You can also think of namespace as a category like CimV2, SecurityCenter etc. Find more info on these namespaces &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394554(v=vs.85).aspx" target="_blank"&gt;&lt;span style="font-family: inherit;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: inherit;"&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-tUszUj41gB4/TvtSCWxvpoI/AAAAAAAAANs/Jr1e6-9r8Qo/s1600/wbemtest_cimv2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" rea="true" src="http://3.bp.blogspot.com/-tUszUj41gB4/TvtSCWxvpoI/AAAAAAAAANs/Jr1e6-9r8Qo/s320/wbemtest_cimv2.png" width="287" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
3. I am intersted in Win32_Processor class which is in cimv2 namespace. So, I will enter cimv2 and click connect. This will load cimv2 and all the buttons in first figure will be enabled. Click "Enum Instances" button which will ask you to enter the class name. It looks like below.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-OY7HWkWM4z0/TvtSxX9DJQI/AAAAAAAAAN4/URRUsbEJ0sw/s1600/Capture_open.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="270" rea="true" src="http://1.bp.blogspot.com/-OY7HWkWM4z0/TvtSxX9DJQI/AAAAAAAAAN4/URRUsbEJ0sw/s320/Capture_open.PNG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
4. Click Ok. This will open another window listing all the instances of Win32_Processor. In my computer, there is only one processor in my system, so i get only one entry.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-GcoYaYKD4CY/TvtT2YGXHPI/AAAAAAAAAOE/A03H663wBsQ/s1600/win32_processorinstance.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="205" rea="true" src="http://4.bp.blogspot.com/-GcoYaYKD4CY/TvtT2YGXHPI/AAAAAAAAAOE/A03H663wBsQ/s320/win32_processorinstance.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
5. Double click on the entry. This will open the details of the processor which looks like below image. You can see that the window lists all Win32_Processor class properties with values loaded into them. So, you can get all information like, processor architecture, name, address width, data width etc.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-0oawKEINZqQ/TvtUrbueZ5I/AAAAAAAAAOQ/F4SLa3duEpk/s1600/Win32_final.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="296" rea="true" src="http://1.bp.blogspot.com/-0oawKEINZqQ/TvtUrbueZ5I/AAAAAAAAAOQ/F4SLa3duEpk/s320/Win32_final.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Isn't this a nice tool? One drawback of this tool is that you need to know which class you want to access which means you cannot loop through all WMI classes. But, this tool is very handy as it is at your fingertip. I hope you too will find this useful.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/6jHssELTWj0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/7858870723113454494/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/12/wbemtest-simple-tool-to-access-wmi.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/7858870723113454494?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/7858870723113454494?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/6jHssELTWj0/wbemtest-simple-tool-to-access-wmi.html" title="WBEMTEST - A simple tool to access WMI classes" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-U2oy_tfbQEc/TvtP4P_ssoI/AAAAAAAAAM8/5zy89MqXLeE/s72-c/wbemtest_start.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/12/wbemtest-simple-tool-to-access-wmi.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEAGR345eyp7ImA9WhRXFkg.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-1627308588877954086</id><published>2011-12-22T22:38:00.001+05:30</published><updated>2011-12-23T21:42:06.023+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-23T21:42:06.023+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="BCL" /><title>Obtaining Culture Information through FormatProviders</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="text-align: justify;" trbidi="on"&gt;
Often we come across the situations where we need to read the culture specific information like date/time format, decimal seperator, currency symbol, number styles etc. Mostly we need this information for formatting the output according to user culture. In this post, I will show you how you can obtain such culture specific information.&lt;br /&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: justify;" trbidi="on"&gt;
That said, without spending much time in theory, let's see where we find this information. As you might know, System.Globalization.CultureInfo class is the one you must target for this purpose. This class, CultureInfo, provides a public function called GetFormat (a method declared in IFormatProvider interface) whose signture looks like below,&lt;br /&gt;
&lt;/div&gt;
&lt;span style="color: #a2c4c9;"&gt;&lt;span style="font-family: Times, &amp;quot;Times New Roman&amp;quot;, serif;"&gt;&lt;div dir="ltr" style="text-align: justify;" trbidi="on"&gt;
&lt;pre style="background: url(&amp;quot;http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif&amp;quot;) rgb(240, 240, 240); border: 1px dashed rgb(204, 204, 204); color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; public virtual object GetFormat(Type formatType);&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;/div&gt;
&lt;/span&gt;&lt;/span&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
As of now, formatType paramter can be either NumberFormatInfo or DataTimeFormatInfo both of which are defined in System.Globalization namespace. The NumberFormatInfo class provides you information about number system such as Positive Sign, Negetive Sign, Decimal seperator, Currency Symbo, Native Digits etc. On the other hand DateTimeFormatInfo provides information about Date and Time formats like, DateSeperator, FirstDayOfWeek, LongDate format, ShortDate format, Time Seperator etc. &lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div dir="ltr" style="text-align: justify;" trbidi="on"&gt;
For example,&amp;nbsp;here is how I get the currency symbol for the culture fr-FR.&lt;br /&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: justify;" trbidi="on"&gt;
&lt;pre style="background: url(&amp;quot;http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif&amp;quot;) rgb(240, 240, 240); border: 1px dashed rgb(204, 204, 204); color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; Type formatType = typeof(NumberFormatInfo);  
 CultureInfo frCulture = CultureInfo.GetCultureInfo("fr-FR");  
 NumberFormatInfo info = frCulture.GetFormat(formatType) as NumberFormatInfo;  
 String currencySymbol = info.CurrencySymbol;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: justify;" trbidi="on"&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: justify;" trbidi="on"&gt;
I think the above example gives you the whole idea on how to get culture specific information. Below is the code that will give you all DateTime format info that is possible to get from current culture. You can use this code for NumberFormatInfo as well (just replace DateTimeFormatInfo with NumberFormatInfo).&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;span style="color: #a2c4c9; font-family: Times, &amp;quot;Times New Roman&amp;quot;, serif;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #a2c4c9; font-family: Times, &amp;quot;Times New Roman&amp;quot;, serif;"&gt;&lt;pre style="background: url(&amp;quot;http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif&amp;quot;) rgb(240, 240, 240); border: 1px dashed rgb(204, 204, 204); color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; public String GetDateTimeFormatInfo(String cultureName)  
 {  
   CultureInfo cultInfo = CultureInfo.GetCultureInfo(cultureName);  
   var formatInfo = (DateTimeFormatInfo)cultInfo.GetFormat(typeof(DateTimeFormatInfo));  
   PropertyInfo[] properties = formatInfo.GetType().GetProperties();  
   String info = String.Empty;  
   foreach (var prop in properties)  
   {  
      if (prop.GetValue(formatInfo, null) is Array)  
      {  
         Array a = prop.GetValue(formatInfo, null) as Array;  
         String values = String.Empty;  
         foreach (var item in a)  
         {  
            values += item + ",";  
         }  
         info += prop.Name+":"+values.Substring(0,values.Length-1);  
         info += Environment.NewLine;  
      }  
      else  
      {  
         info += prop.Name + " : " + prop.GetValue(formatInfo, null);  
         info += Environment.NewLine;  
      }  
   }  
   return info;  
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Note: You &amp;nbsp;can also obtain all this information from registry. If you are keen, you can have examine the below registry key.&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;pre style="background: url(&amp;quot;http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif&amp;quot;) rgb(240, 240, 240); border: 1px dashed rgb(204, 204, 204); color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; HKEY_CURRENT_USER\Control Panel\International  
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/o-3av2rKlck" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/1627308588877954086/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/12/obtaining-culture-information-through.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/1627308588877954086?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/1627308588877954086?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/o-3av2rKlck/obtaining-culture-information-through.html" title="Obtaining Culture Information through FormatProviders" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/12/obtaining-culture-information-through.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYAQ3ozcSp7ImA9WhRXFks.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-4822660276510528088</id><published>2011-12-05T23:53:00.001+05:30</published><updated>2011-12-24T00:19:02.489+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-24T00:19:02.489+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="FCL" /><title>Handling System Shutdown / Logoff events in C#.NET</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
On .NET forums, I often come across people interested in knowing about notifications of something changed in the system. For example whether windows is shutting down, whether user is logging off, power mode changed, or system font changed etc. For instance, you may want to perform some operation when your application is terminating. In that case, what are all the possible ways of terminating the application?&lt;/div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
Closing the application by clicking close 'x' button&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
Due to exception&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
User kills the process&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;div style="text-align: justify;"&gt;
When the user logs off or system shuts down&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div style="text-align: justify;"&gt;
The first 2 are common scenarios which the user can take care of easily. But what about the 3rd and 4th cases? The 3rd case is, I don't think we can control. However, 4th one is interesting. When the system shutdown is started either by user or by any automated process, the shutdown process will terminate all running processes. Before that it sends a signal to all running processes that the system is going to shutdown. Upon receiving this signal, a process can do any windup operation so as to prevent any data loss. This is very important because you might be in middle of some important operation and sudden application termination may cause valuable data loss.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
So, how do a .NET application receives this signal? Whenver it comes to the matter of system related things, many people think of native OS DLLs or PInvoke (some unmanaged way). But wait, we have some managed objects that do the work. There is a class called SystemEvents in Microsoft.Win32 dll which offers many system based events. You can google or refer MSDN for more info SystemEvents class. Here, I will just take an example of how System shutdown or user logoff events are handled.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Below is a simple console application that displays a message box whenever a system shutdown occurs or user logs off (In any case, it causes CLR to unload and hence a notification is sent about the event).&lt;/div&gt;
&lt;br /&gt;
&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;&lt;div class="WordSection1"&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: #2b91af;"&gt;Program&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span style="color: black;"&gt;Main(&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;[]&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: black;"&gt;args&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;SystemEvents&lt;/span&gt;.&lt;span style="color: black;"&gt;SessionEnding&lt;/span&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;+=&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: black;"&gt;SystemEvents_SessionEnding;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span class="SpellE"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.&lt;span style="color: black;"&gt;ReadLine&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;//This is needed to keep the application running.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: black;"&gt;SystemEvents_SessionEnding&lt;/span&gt;&lt;/span&gt;(&lt;span style="color: blue;"&gt;object&lt;/span&gt; &lt;span style="color: black;"&gt;sender&lt;/span&gt;, &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;SessionEndingEventArgs&lt;/span&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;e&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;switch&lt;/span&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;(&lt;span class="SpellE"&gt;e.Reason&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;case&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;SessionEndReasons&lt;/span&gt;.&lt;span style="color: black;"&gt;Logoff&lt;/span&gt;&lt;/span&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&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&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;MessageBox&lt;/span&gt;.&lt;span style="color: black;"&gt;Show&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"User logging off"&lt;/span&gt;&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&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&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;case&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;SessionEndReasons&lt;/span&gt;.&lt;span style="color: black;"&gt;SystemShutdown&lt;/span&gt;&lt;/span&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&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&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;MessageBox&lt;/span&gt;.&lt;span style="color: black;"&gt;Show&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"System is shutting down"&lt;/span&gt;&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&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&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="color: black;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/W97MVDsD-TQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/4822660276510528088/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/12/handling-system-shutdown-logoff-events.html#comment-form" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/4822660276510528088?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/4822660276510528088?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/W97MVDsD-TQ/handling-system-shutdown-logoff-events.html" title="Handling System Shutdown / Logoff events in C#.NET" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>5</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/12/handling-system-shutdown-logoff-events.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEENR3Y8fip7ImA9WhRXFks.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-5345764155799211963</id><published>2011-12-05T12:50:00.001+05:30</published><updated>2011-12-24T00:28:16.876+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-24T00:28:16.876+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="CLR" /><title>Beware of excessive string objects</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Of all the primitive types in C#, string is a special primitive type in the following regard,&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;It is a reference type (class), unlike - other primitive types being ValueType.&lt;/li&gt;
&lt;li&gt;It is immutable - i.e. once a string is created, it's contents cannot be altered.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div&gt;
Since string is a reference type, a string object must be created on a managed heap which should be garbage collected when the object becomes orphan. This is fine in the sense that at one or other place we can't avoid creating managed objects and all managed objects have to be garbage collected. But, if you look at the second point above which states strings are immutable. Having said this, what if you want to manipulate the string contents (like appending substring, converting to lower case etc.) ? Ofcourse you can do that. But, the original string is never altered, instead, a new string object is created with new value. What does that mean? Take a look at below code.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;/span&gt; s1 = &lt;span style="color: #a31515;"&gt;"Hello World"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;/span&gt; temp = s1;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;// store the reference of s1 into temp&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s1 = &lt;span style="color: #a31515;"&gt;"New World"&lt;/span&gt;;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;// modify s1.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
First I created a string s1 with value "Hello Word". Then I stored the reference of s1 in temp i.e. both temp &amp;amp; s1 are pointing to the same object. You can verify that by calling Object.ReferenceEquals (s1, temp) which will return true if the reference is same. Then I modify s1 to "New World". At this time, I am actually creating a new String rather than modifying the value in s1. You can verify that by comparing contents of temp and s1.&amp;nbsp;So what is the impact of this? Well, to know the impact, see the below code.&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;/span&gt; s = &lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Stopwatch&lt;/span&gt; watch = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Stopwatch&lt;/span&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Int32&lt;/span&gt; count = 0;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;watch.Start&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;while&lt;/span&gt;&lt;/span&gt; (&lt;span style="color: blue;"&gt;true&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s = s + (count++).&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;ToString&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;/span&gt; (&lt;span class="SpellE"&gt;watch.ElapsedMilliseconds&lt;/span&gt; &amp;gt;= 1000)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;//&lt;span class="SpellE"&gt;Thread.Sleep&lt;/span&gt; (1);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
When I ran this code on my i3 machine running Windows7, the loop ran approximately 3 million times. Also, Garbage collection (Gen0) ran for 100 times !!! This is really huge impact on the performance. This is because, after every loop, a new string is created and the old reference becomes the candidate for garbage collection. Also, if you look at the average size of string created at every loop - it is 3Mb. If the garbage collection had not occured, just calculate the total memory occupied by all strings in one second. Thanks to GC for avoiding a disaster. When I uncommented the Sleep statement, the GC collection count reduced to 0.1 per second as the number of strings created per second reduced from 3 million to just 300 !&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
Above demo clearly states that too many strings in memory is not good. So, what are all the possible ways?&lt;/div&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Prefer StringBuilder over String in cases involving heavy string manipulations&lt;/li&gt;
&lt;li&gt;Avoid string manipulations inside a loop at all possible places.&lt;/li&gt;
&lt;li&gt;Avoid too many class level/ global string fileds.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/0ju74c1iQ5U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/5345764155799211963/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/12/beware-of-excessive-string-objects.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/5345764155799211963?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/5345764155799211963?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/0ju74c1iQ5U/beware-of-excessive-string-objects.html" title="Beware of excessive string objects" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/12/beware-of-excessive-string-objects.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0IASHo4fip7ImA9WhRWEEU.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-2606311064958856795</id><published>2011-11-22T14:03:00.001+05:30</published><updated>2011-12-28T21:55:49.436+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-28T21:55:49.436+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#.NET" /><title>Be Careful of Constant fields in .NET</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
We all know that Constant fields are those whose value never changes in the lifetime of an application. Constant are really useful constructs that offer many benifits to the user like,&lt;/div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Constants make developer sure that their value never changes (even by mistake). &lt;/li&gt;
&lt;li&gt;Constants are compile time values, which means their values are directly embedded inside the metadata. Hence, constants don't require memory allocation at runtime. &lt;/li&gt;
&lt;/ul&gt;
&lt;div style="text-align: justify;"&gt;
All that said, there is a risk in using the constants if appropriate care is not taken. See the second point above - "constant values are embedded directly inside the metadata". This statement has an impact when you have constant fields in a DLL which are used in one or more applications. Let me start demostrating you through a sample code. Consider that the below class exists in a library called MyLibrary.DLL.&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;GlobalConstants&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;const&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Int32&lt;/span&gt; MAX_VALUE = 2;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;const&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; MALESTRING = &lt;span style="color: #a31515;"&gt;"Male"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;const&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; FEMALESTRING = &lt;span style="color: #a31515;"&gt;"Female"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;Now, I have to use GlobalConstants in my application. For that I will create a Console Application called SampleClient.exe. Inside the Main function I use the above constants as&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: #2b91af;"&gt;Program&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] &lt;span class="SpellE"&gt;args&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;maxValue&lt;/span&gt; = &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;GlobalConstants&lt;/span&gt;.MAX_VALUE&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; Female = &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;GlobalConstants&lt;/span&gt;.FEMALESTRING&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span class="SpellE"&gt;maxValue&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;Female);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Build SampleClient.exe and run it directly from bin folder. Your application will print 2&amp;nbsp; and Female as expected and everything is fine. Now I got a requirment that MAX_VALUE should be changed to 100, MALE should be "Man" and FEMALE should change to "Woman". Ok, thats fine. I will update GlobalConstants in MyLibrary.DLL class as below.&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;GlobalConstants&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;const&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Int32&lt;/span&gt; MAX_VALUE = 100;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;const&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; MALESTRING = &lt;span style="color: #a31515;"&gt;"Man"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: blue;"&gt;const&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; FEMALESTRING = &lt;span style="color: #a31515;"&gt;"Woman"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
I will re-build MyLibrary.DLL and send the dll for use in SampleClient.exe. So, place the new dll in bin folder of SampleClient and run it. SampleClient is now refering to new MyLibrary.DLL. But what output do you see? you expect that it will print 100 and Woman but it still prints 2 and Female. So, what is the problem? For that you need to see the IL code of Main method in SampleClient.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-7JDpJedt2B8/Tt0Cyb4OuCI/AAAAAAAAAL8/nEshh8GkDkI/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="187" rea="true" src="http://2.bp.blogspot.com/-7JDpJedt2B8/Tt0Cyb4OuCI/AAAAAAAAAL8/nEshh8GkDkI/s400/Capture.PNG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Observe that the values 2 and FEMALE are hardcoded in the IL. Though you have changed MAX_VALUE to 100 &amp;amp; FEMALE to "Woman", the values are not reflected. To correct this problem, you must re-build SampleClient application too to make use of new constant value in the dll. Rebuilding SampleClient will embed the new MAX_VALUE and FEMALE values into metadata of Main method.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;With this problem, consider your DLL is consumed by Hundreds of applications. So, changing a constant value in your library will result in rebuilding of all it's consumers. So, be careful with constants, use them only when you are sure that their value never-ever change or if they change, you afford re-building it's consumers. Or, if you think constant values may change, then prefer using 'readonly' fields over constants. I will discuss about 'readonly' fields in my next writing.&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/e3Ii4dJbj1k" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/2606311064958856795/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/11/be-careful-of-constant-fields-in-net.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/2606311064958856795?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/2606311064958856795?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/e3Ii4dJbj1k/be-careful-of-constant-fields-in-net.html" title="Be Careful of Constant fields in .NET" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-7JDpJedt2B8/Tt0Cyb4OuCI/AAAAAAAAAL8/nEshh8GkDkI/s72-c/Capture.PNG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/11/be-careful-of-constant-fields-in-net.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkENSX05eSp7ImA9WhRWEEU.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-7210668207799660438</id><published>2011-11-15T17:33:00.001+05:30</published><updated>2011-12-28T21:41:38.321+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-28T21:41:38.321+05:30</app:edited><title>Creating Multi Column ComboBox in .NET</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: justify;"&gt;
Recently I was working on an assignment wherein I had to create a Custom ComboBox to display multiple columns when it is dropped down. Something similar to below image.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-J3ALiUOnMws/TsJYTP9eTKI/AAAAAAAAAKI/-PJ63fpw2JY/s1600/2+Column+Combo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" rea="true" src="http://2.bp.blogspot.com/-J3ALiUOnMws/TsJYTP9eTKI/AAAAAAAAAKI/-PJ63fpw2JY/s320/2+Column+Combo.png" width="304" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
The actual assignmet was little more complex than the one I have shown in the above image. Anyways, if you look at the image, you will also find the combo box pretty useful as it can display data in column manner like FirstName-LastName, Name-Place etc. So, if you like it, you can design it yourself. All you have to do is, create class that inherits ComboBox, set the DrawMode property to OwnerDrawFixed, override OnDrawItem method to draw the columns manually. &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Sounds simple, isn't it? Now, how do we supply items to this ComboBox? If we have to display an item in multicolumn fashion, ideally the item must be composite one i.e. a Dictionary&amp;lt;T, T&amp;gt; or a class with 2 instance members. Since ComboBox's Items property is a collection of objects, we can place anything into it. This has got both advantage as well as disadvantages.&amp;nbsp;&amp;nbsp; Advantage is we can place object of our custom class into ComboBox while the disadvantage is user can enter any kind of item which cannot be represented in the way we need to display it. &lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;Ok, all that said, I will create a class whose instances we are going to place in the combo box. I will call this class as XComboItem (X stands for Extended).&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;XComboItem&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; &lt;span class="SpellE"&gt;DisplayName&lt;/span&gt; { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Object&lt;/span&gt; Value { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; Description { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
So, this is the class, whose objects we are going to place in out custom combo Box. I will name the combo as XComboBox. &lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;partial&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;XComboBox&lt;/span&gt;&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;ComboBox&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Int32&lt;/span&gt; &lt;span class="SpellE"&gt;ColumnGap&lt;/span&gt; = 20;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Int32&lt;/span&gt; &lt;span class="SpellE"&gt;firstColumnWidth&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Int32&lt;/span&gt; &lt;span class="SpellE"&gt;secondColumnWidth&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;XComboBox&lt;/span&gt;(): &lt;span style="color: blue;"&gt;base&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;DrawMode&lt;/span&gt; = &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;DrawMode&lt;/span&gt;.OwnerDrawFixed&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;firstColumnWidth&lt;/span&gt;&lt;/span&gt; = &lt;span class="SpellE"&gt;DropDownWidth&lt;/span&gt; / 2;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;secondColumnWidth&lt;/span&gt;&lt;/span&gt; = &lt;span class="SpellE"&gt;DropDownWidth&lt;/span&gt; / 2;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
In this, firstColumnWidth and secondColumnWidth are the widths of both columns that I am going to display in combo dropdown list (in pixels). Column gap is gap between two columns.Initially I will set the column widths to half the width of DropDownWidth. The only major thing is to override OnDrawItem() function. By overriding this function, you can draw the items in DropDownList of the combobox in the way you want. You can display each item with different font, differen style or even add image to each item. In our case, we need to display DisplayItem &amp;amp; Description of XComboItem in two seperate columns. The logic is getting the drawing object of current item, then use DrawString method appropriately. So, here goes our OnDrawItem.&lt;/div&gt;
&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span class="GramE"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;protected&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span class="SpellE"&gt;OnDrawItem&lt;/span&gt;(&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;DrawItemEventArgs&lt;/span&gt;&lt;/span&gt; e)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;/span&gt; (&lt;span class="SpellE"&gt;e.Index&lt;/span&gt; &amp;lt; 0)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;XComboItem&lt;/span&gt;&lt;/span&gt; item = (&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;XComboItem&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;)Items&lt;/span&gt;[&lt;span class="SpellE"&gt;e.Index&lt;/span&gt;];&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;ColumnGap&lt;/span&gt; = &lt;span class="SpellE"&gt;firstColumnWidth&lt;/span&gt; == &lt;span class="GramE"&gt;0 ?&lt;/span&gt; &lt;span class="GramE"&gt;0 :&lt;/span&gt; &lt;span class="SpellE"&gt;ColumnGap&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;e.DrawBackground&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;e.DrawFocusRectangle&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; first = &lt;span class="SpellE"&gt;item.DisplayName&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt; second = &lt;span class="SpellE"&gt;item.Description&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;while&lt;/span&gt;&lt;/span&gt; (&lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;TextRenderer&lt;/span&gt;.MeasureText&lt;/span&gt;(first, &lt;span class="SpellE"&gt;e.Font&lt;/span&gt;).Width &amp;gt; &lt;span class="SpellE"&gt;firstColumnWidth&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;first&lt;/span&gt; = &lt;span class="SpellE"&gt;first.Substring&lt;/span&gt;(0, &lt;span class="SpellE"&gt;first.Length&lt;/span&gt; - 1);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Brush&lt;/span&gt; &lt;span class="SpellE"&gt;solidBrush&lt;/span&gt; = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: #2b91af;"&gt;SolidBrush&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span class="SpellE"&gt;e.ForeColor&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;e.Graphics.DrawString&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;first, &lt;span class="SpellE"&gt;e.Font&lt;/span&gt;, &lt;span class="SpellE"&gt;solidBrush&lt;/span&gt;, &lt;span class="SpellE"&gt;e.Bounds.Left&lt;/span&gt;, &lt;span class="SpellE"&gt;e.Bounds.Top&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;e.Graphics.DrawString&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;second, &lt;span class="SpellE"&gt;e.Font&lt;/span&gt;, &lt;span class="SpellE"&gt;solidBrush&lt;/span&gt;, &lt;span class="SpellE"&gt;e.Bounds.Left&lt;/span&gt; + &lt;span class="SpellE"&gt;firstColumnWidth&lt;/span&gt; + &lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&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; &lt;span class="SpellE"&gt;ColumnGap&lt;/span&gt;, &lt;span class="SpellE"&gt;e.Bounds.Top&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
You can add any properties or methods to make the control more user friendly. So, That's it, our control is ready. To use, create the object of XComboItem and add the object to Items collection of XComboBox. Then run your application to see the effect.&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/mmyBZbFck6s" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/7210668207799660438/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/11/creating-multi-column-combobox-in-net.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/7210668207799660438?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/7210668207799660438?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/mmyBZbFck6s/creating-multi-column-combobox-in-net.html" title="Creating Multi Column ComboBox in .NET" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-J3ALiUOnMws/TsJYTP9eTKI/AAAAAAAAAKI/-PJ63fpw2JY/s72-c/2+Column+Combo.png" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/11/creating-multi-column-combobox-in-net.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUADQnc-eyp7ImA9WhRQEU0.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-152514698293196108</id><published>2011-09-04T22:57:00.000+05:30</published><updated>2011-12-05T23:26:13.953+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-05T23:26:13.953+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#.NET" /><title>Built-in General Purpose Delegates in .NET</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
In day-to-day coding, we often need to create delegates. Every time we need a delegate, we usually create a new delegate and use it. But, if you know .NET already provides some ready made delegates which we can use for varying purposes. So, in this post I will be discussing some of the common built-in delegates that we can use for general purpose.&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;&lt;u&gt;System.Action:&lt;/u&gt;&lt;/strong&gt; The Action delegate can accept any method which takes zero arguments and returns void. The declaration goes like&lt;br /&gt;
&lt;br /&gt;&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;public delegate void Action();&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;&lt;u&gt;System.Action&amp;lt;…&amp;gt; :&lt;/u&gt; &lt;/strong&gt;This is a generic version of System.Action delegate. This delegate can accept any method that takes 1 to 16 parameters and returns void. You can use this delegate as below,&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;public void SomeMethod(int a, string b, double c){}&lt;br /&gt;System.Action&amp;lt;int, string , double&amp;gt; action = new System.Action&amp;lt;int, string , double&amp;gt;( SomeMethod );&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;&lt;u&gt;System.Comparison&amp;lt;T&amp;gt;: &lt;/u&gt;&lt;/strong&gt;Actually this delegate is used for specific purpose, I am discussing this here because it is often used in Sorting techniques.&amp;nbsp; This takes a method as parameter which takes two arguments of type T and return an Integer. For example, I want to sort list of employees based on their first names then I can do it as,&lt;br /&gt;
&lt;br /&gt;&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;private int CompareEmpoyees(Employee a, Employee b)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return a.FirstName.CompareTo(b.FirstName);&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;ListempList = GetEmployeeList();&lt;br /&gt;Comparison comparer = new Comparison(CompareEmpoyees);&lt;br /&gt;empList.Sort(comparer);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;&lt;u&gt;System.Func&amp;lt;…, out TOut&amp;gt; :&lt;/u&gt;&lt;/strong&gt; This is a generic delegate that accepts any method that takes 0 to 16 arguments of any type and return an argument of type TOut. For example,&lt;br /&gt;
&lt;br /&gt;&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;public Employee SomeMethod(int a, string b, double c)&lt;br /&gt;{&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;System.Action&amp;lt;int, string , double&amp;gt; action = new System.Action&amp;lt;int, string , double, Employee&amp;gt;( SomeMethod );&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;&lt;u&gt;System.MethodInvoker:&lt;/u&gt;&lt;/strong&gt; This delegate is similar to System.Action delegate which accepts a method that takes zero arguments and returns void. But, as the name suggests, this delegate is used at places where you need to talk to UI elements from a background thread. Something like below,&lt;br /&gt;
&lt;br /&gt;&lt;span style="color: #76a5af; font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;MethodInvoker mi = new MethodInvoker ( () =&amp;gt; { textBox1.Text = "Hello World"; });&lt;br /&gt;this.Invoke(mi);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;That is it for now. I will be editing this page if I find any other delegates that fit here. Meanwhile, If you want me to add anything here, please let me know.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/TYjOG_bw3B0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/152514698293196108/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/09/builtin-general-purpose-delegates-in.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/152514698293196108?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/152514698293196108?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/TYjOG_bw3B0/builtin-general-purpose-delegates-in.html" title="Built-in General Purpose Delegates in .NET" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/09/builtin-general-purpose-delegates-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0EHRng-eip7ImA9WhRXFks.&quot;"><id>tag:blogger.com,1999:blog-3862379332918459388.post-3644508145972380468</id><published>2011-07-02T13:53:00.000+05:30</published><updated>2011-12-24T00:10:37.652+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-24T00:10:37.652+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WinForms" /><title>Closing all open forms in .NET</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
In your application you may have many hidden forms which you must close before exiting the application. It's because, hidden forms always stay in the memory until you close them. But, .net framework always takes care of disposing off all those objects when the application is terminated. But, the real problem comes when those hidden forms contain COM/ActiveX components. You all know, you must manually clean all unmanaged components. Now, if you forget to close such forms, it may lead to memory leaks.&lt;br /&gt;
&lt;br /&gt;It's very simple to close all such open forms. Application.OpenForms is the collection that contains all open forms. So, just loop through this collection and close one by one. Below is the code, where I am closing all open forms in main form's FormClosing event handler.&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span class="SpellE"&gt;MainForm_FormClosing&lt;/span&gt;(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;FormClosingEventArgs&lt;/span&gt;&lt;/span&gt; e)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;Form&lt;/span&gt; &lt;span class="SpellE"&gt;openForm&lt;/span&gt; &lt;span style="color: blue;"&gt;in&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;Application&lt;/span&gt;.OpenForms&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;openForm.Close&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;On first look, this code looks to be fine. But, there are two potential problems with this code. &lt;br /&gt;
First,Application.OpenForms contains your main form because it is also open currently. So, when the loop executes, it will call Close() on MainForm, which in-turn will fire FormClosing event of MainForm. This will results in calling MainForm_FormClosing method recursively and infinitely which leads to deadlock. So, a StackOverFlowException will occur. To avoid this, you can add a check as below.&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span class="SpellE"&gt;MainForm_FormClosing&lt;/span&gt;(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;FormClosingEventArgs&lt;/span&gt;&lt;/span&gt; e)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;Form&lt;/span&gt; &lt;span class="SpellE"&gt;openForm&lt;/span&gt; &lt;span style="color: blue;"&gt;in&lt;/span&gt; &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;Application&lt;/span&gt;.OpenForms&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;/span&gt; (&lt;span class="SpellE"&gt;openForm&lt;/span&gt; != &lt;span style="color: blue;"&gt;this&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&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&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;openForm.Close&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;simple, isn't it? Is it good to go now? No. Still there is a problem. To understand the problem, let us consider, we have 3 open forms. So, Application.OpenForms collection contains 3 items (say in following order)&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp; 1. MainForm&lt;br /&gt;
&amp;nbsp;&amp;nbsp; 2. SubForm1&lt;br /&gt;
&amp;nbsp;&amp;nbsp; 3. SubForm2&lt;br /&gt;
&lt;br /&gt;Ok. The loop executes first time and skips the Close() since the condition fails. During second loop, it closes the SubForm1 and imporatantly SubForm1 is removed from Application.OpenForms collection. This means the collection is altered. Now, during 3rd loop, it tries to access the collection, an 'InvalidOperationException' is thrown saying "Collection was modified; enumeration operation may not execute". Hence, what you can do is instead of directly accessing Application.OpenForms, store all open forms in an array or a list. Then loop through the array/List and close the forms. So, below is the final code.&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; &lt;span class="SpellE"&gt;MainForm_FormClosing&lt;/span&gt;(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;FormClosingEventArgs&lt;/span&gt;&lt;/span&gt; e)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Form&lt;/span&gt;&amp;gt; &lt;span class="SpellE"&gt;lstOpenForms&lt;/span&gt; = &lt;span class="SpellE"&gt;&lt;span style="color: #2b91af;"&gt;Application&lt;/span&gt;.OpenForms.OfType&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Form&lt;/span&gt;&lt;span class="GramE"&gt;&amp;gt;(&lt;/span&gt;).&lt;span class="SpellE"&gt;ToList&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;Form&lt;/span&gt; &lt;span class="SpellE"&gt;openForm&lt;/span&gt; &lt;span style="color: blue;"&gt;in&lt;/span&gt; &lt;span class="SpellE"&gt;lstOpenForms&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;/span&gt; (&lt;span class="SpellE"&gt;openForm&lt;/span&gt; != &lt;span style="color: blue;"&gt;this&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&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&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;openForm.Close&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;span style="font-family: Consolas; font-size: 9.5pt; mso-bidi-language: HI;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"&gt;
&lt;br /&gt;&lt;/div&gt;
I hope this information was useful.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/IdeasOnHand/~4/4-eeYfn-UOw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://adavesh.blogspot.com/feeds/3644508145972380468/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://adavesh.blogspot.com/2011/07/closing-all-open-forms-in-net.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/3644508145972380468?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3862379332918459388/posts/default/3644508145972380468?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IdeasOnHand/~3/4-eeYfn-UOw/closing-all-open-forms-in-net.html" title="Closing all open forms in .NET" /><author><name>Adavesh BM</name><uri>http://www.blogger.com/profile/01790207998617262971</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="19" src="http://4.bp.blogspot.com/-q46CtSQLxkk/Tg6s14LoH7I/AAAAAAAAAH0/dA2bL-ygAB0/s220/2011-06-15%2B14.06.37.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://adavesh.blogspot.com/2011/07/closing-all-open-forms-in-net.html</feedburner:origLink></entry></feed>
