<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Cluebat-man to the rescue : C#</title><link>http://msmvps.com/blogs/vandooren/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Using reflection to ease code maintenance</title><link>http://msmvps.com/blogs/vandooren/archive/2008/12/02/using-reflection-to-ease-code-maintenance.aspx</link><pubDate>Tue, 02 Dec 2008 11:35:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1655548</guid><dc:creator>vanDooren</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/rsscomments.aspx?PostID=1655548</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/commentapi.aspx?PostID=1655548</wfw:comment><comments>http://msmvps.com/blogs/vandooren/archive/2008/12/02/using-reflection-to-ease-code-maintenance.aspx#comments</comments><description>&lt;p style="margin:6pt 0cm 0pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;Currently I am programming an application for performing code analysis on the software that controls the production process. Apart from the parsing of the code, and iterating across a hierarchical representation of the expression tree, &lt;i style="mso-bidi-font-style:normal;"&gt;all&lt;/i&gt; these rules (verifications) have to be executed against all the code.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:6pt 0cm 0pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;I could change the code verifier every time I implemented a new rule, but instead I used reflection to do this for me. Every verification that has to be done is represented by a class that implements the right interface. This interface defines the prototype of the method that performs the verification.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:6pt 0cm 0pt;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IActionVerification&lt;br /&gt;&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt; Verify(&lt;span style="color:#2b91af;"&gt;CodeContext&lt;/span&gt; context);&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:6pt 0cm 0pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;The context variable contains the complete context of the portion of the code that is being looked at: the current action, step, function block and phase.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:6pt 0cm 0pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;When a new code verifier instance is create, it will use reflection to examine the current assembly, and extract all classes that implement this interface.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:6pt 0cm 0pt;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; CodeVerifier()&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;actionVerifications = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IActionVerification&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; currAssembly = &lt;span style="color:#2b91af;"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly();&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;foreach&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; t &lt;span style="color:blue;"&gt;in&lt;/span&gt; currAssembly.GetTypes())&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;br /&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;if&lt;/span&gt; (&lt;span style="color:blue;"&gt;null&lt;/span&gt; != t.GetInterface(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IActionVerification&lt;/span&gt;).Name))&lt;br /&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;actionVerifications.Add(&lt;br /&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; &lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IActionVerification&lt;/span&gt;)&lt;br /&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;currAssembly.CreateInstance(t.FullName));&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:6pt 0cm 0pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;This way, it is very easy to populate the list of all verifications to do. The code verification itself is conceptually as simple as this:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:6pt 0cm;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;color:blue;font-family:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;foreach&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; verification &lt;span style="color:blue;"&gt;in&lt;/span&gt; actionVerifications)&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;verification.Verify(context);&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 6pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Times New Roman;"&gt;Implementing a new verification is the as easy as creating a new class and implementing the interface. After that everything will be done automatically. Ading a new verification is as easy as this:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 6pt;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ActionExpressionCount&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IActionVerification&lt;br /&gt;&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;span style="color:gray;"&gt;&lt;br /&gt;&lt;/span&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;void&lt;/span&gt; Verify(&lt;span style="color:#2b91af;"&gt;CodeContext&lt;/span&gt; context)&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;br /&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;if&lt;/span&gt; (context.Action.ParsedExpression.Count &amp;lt; 1)&lt;br /&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;ActionExpressionCountInvalid&lt;/span&gt;.LogIssue(context);&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 6pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Times New Roman;"&gt;Hip hip hooray for the .NET framework!&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1655548" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/vandooren/archive/tags/Interoperability/default.aspx">Interoperability</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Access != database</title><link>http://msmvps.com/blogs/vandooren/archive/2008/07/14/access-database.aspx</link><pubDate>Mon, 14 Jul 2008 06:27:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1640689</guid><dc:creator>vanDooren</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/rsscomments.aspx?PostID=1640689</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/commentapi.aspx?PostID=1640689</wfw:comment><comments>http://msmvps.com/blogs/vandooren/archive/2008/07/14/access-database.aspx#comments</comments><description>&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;While working on a data application for the finance guys, I already discovered that Excel sucks when used with OleDb. It seems as if Excel was specifically &lt;i style="mso-bidi-font-style:normal;"&gt;designed&lt;/i&gt; to make your life as a programmer miserable.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;Fair&amp;rsquo;s fair: Excel is not a database, so perhaps it is acceptable if the data link layer feels as if it has been thrown together by a vba hacker without a clear grasp of data types and interfaces. Probably they needed another handful of buzzwords on the Excel box, and decided to hire a high-school summer intern who installed basic once.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;But I digress.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;While I am willing to let Excel off the hook for not being something it was not supposed to be, I have a problem with extending the same courtesy to Access; an application specifically designed to be a database.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;For the last couple of hours I have been debugging the finance app because some of the allocated items showed up as having cost nothing. This would have enjoyed the CFO immensely if it had been true, but since that wasn&amp;rsquo;t the case, they were wondering why my app insisted it. &lt;/span&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;For some systems the totals and the details were correct. For others they were zero, or something far less that what they had cost in reality.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;I examined a correct example, compared it with one of the problem cases, and found that all required data was there, in the correct tables. Then I debugged through the code, and the algorithm seemed to work just fine. Only for some systems it didn&amp;rsquo;t return any values. And since this was done via LINQ, debugging the actual SQL was difficult.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;Then I decided to go spelunking in the raw data tables themselves. Again and again I checked, and nothing seemed to be wrong. But then it suddenly jumped at me:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;tbl_keymstr -&amp;gt; row 1234 -&amp;gt; Alloc_key == &amp;lsquo;all&amp;rsquo;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;tbl_alloc -&amp;gt; row 123456 -&amp;gt; Alloc_key == &amp;lsquo;All&amp;rsquo;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;How could this be possible? I double checked, and verified that the links were all defined with the &amp;lsquo;Enforce referential integrity&amp;rsquo; flag set.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;After some testing, it seems that Access is pretty loose in its judgments. When it comes to comparing strings, &amp;lsquo;equals&amp;rsquo; really means &amp;lsquo;kinda looks like&amp;rsquo;, so &amp;lsquo;All&amp;rsquo;, &amp;lsquo;all&amp;rsquo; and &amp;lsquo;alL&amp;rsquo; are all the same for access.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;The LINQ queries in my app all use the C# &amp;lsquo;==&amp;rsquo; to define joinings and equality operations, so if the rows in 1 table say &amp;lsquo;All&amp;rsquo; and those in the other say &amp;lsquo;all&amp;rsquo; the result of the joined set is empty of course.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;Key definitions and cost allocations get uploaded separately, and my app doesn&amp;rsquo;t check whether the key fields are spelled the same. Everything gets uploaded, and the integrity is insured by the database itself, as it should be. If there is an upload error it gets reported and everything is rolled back. Unfortunately, key value comparison does not work that well.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;The fix was very simple: I implemented a case insensitive comparison in my queries, and the problem was solved. Too bad Access is allowed by the Office team to&amp;nbsp;let sloppy input through.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0cm 0cm 12pt;" class="MsoNormal"&gt;&lt;span style="font-size:small;font-family:Times New Roman;"&gt;&lt;strong&gt;EDIT: I just checked, and by default, SQL server has the same behavior. But you can change it to case sensitive if you want, which is important in scenarios like these. Granted, I should have checked first before slagging off Access, but I still think that this should be an option in Access&lt;/strong&gt;. &lt;strong&gt;The fact that it isn&amp;#39;t is a severe shortcoming in my eyes, since lots of smaller databases use strings as keys.&amp;nbsp;In case sensitive clients (like e.g. C# apps by default) this causes a lot of work to get correct results. And if you forget to case insensitivize (is that even a word?) your queries even once, you will get incorrect data.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1640689" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/OleDb/default.aspx">OleDb</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/Access/default.aspx">Access</category></item><item><title>Differences between C++ and C#, part 3</title><link>http://msmvps.com/blogs/vandooren/archive/2008/02/27/differences-between-c-and-c-part-3.aspx</link><pubDate>Wed, 27 Feb 2008 11:52:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1525890</guid><dc:creator>vanDooren</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/rsscomments.aspx?PostID=1525890</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/commentapi.aspx?PostID=1525890</wfw:comment><comments>http://msmvps.com/blogs/vandooren/archive/2008/02/27/differences-between-c-and-c-part-3.aspx#comments</comments><description>&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;One of the things that C# (and VB.NET) programmers have to live with is non-deterministic destruction of their objects.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;The garbage collector was introduced in .NET to take care of memory leaks and remove the need for manual memory management. It does this by keeping track of references, and as soon as an object is no longer referenced, it ‘can’ be collected by the garbage collector.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;The ‘can’ is important here, because cleanup happens only when the GC thinks is should happen. On systems with lots of memory, and few memory allocations and releases, it is possible that this takes minutes or hours, or even doesn’t happen at all.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;This can be a real problem, because now you don’t have memory leaks anymore, but resource leaks.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Suppose your app opens a file for writing, and does not explicitly dispose of the stream reference.&lt;/font&gt;&lt;/p&gt;&lt;span style="FONT-SIZE:10pt;COLOR:#2b91af;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;FileStream&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt; stream =&lt;br /&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;FileStream&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;Path&lt;/span&gt;.GetTempFileName(), &lt;span style="COLOR:#2b91af;"&gt;FileMode&lt;/span&gt;.Open);&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;COLOR:#2b91af;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;StreamWriter&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt; writer = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;StreamWriter&lt;/span&gt;(stream);&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;writer.WriteLine(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;Hello, world.&amp;quot;&lt;/span&gt;);&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;writer.Close();&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;The file is written and flushed to disk, but as long as the garbage collector does not collect the ‘stream’ object, it will hold an open file handle that will prevent you or anyone else from accessing that file. To overcome this you have to make sure that the file is released. On way of doing that would be like this:&lt;/font&gt;&lt;/p&gt;&lt;span style="FONT-SIZE:10pt;COLOR:#2b91af;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;FileStream&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt; stream = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;FileStream&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;Path&lt;/span&gt;.GetTempFileName(), &lt;span style="COLOR:#2b91af;"&gt;FileMode&lt;/span&gt;.Open);&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;try&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:#2b91af;"&gt;StreamWriter&lt;/span&gt; writer = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;StreamWriter&lt;/span&gt;(stream);&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;writer.WriteLine(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;Hello, world.&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;writer.Close();&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;finally&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;stream.Dispose();&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;You could also use the ‘using’ keyword, but the same holds true. It is easy to make mistakes, and if you have several of such objects in the same scope, then your code will become a convoluted mess of nested try/finally or using constructs.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;In C++/CLI, this problem can be solved differently.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;In native C++, they way you create an object determines whether it will live on the stack or in the heap. The location implies the way the object will be cleaned up. If it is on the stack, it will be released automatically when it goes out of scope.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;C++/CLI does not have the ability to create managed objects on the stack, but it supports stack like semantics. I.e. even if you use the declaration that would put the object in the stack, it is still created in the managed heap. But the syntax and the semantics are stack based.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;This means that in the following example, the stream object will automatically be released when it goes out of scope.&lt;/font&gt;&lt;/p&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;FileStream stream(Path::GetTempFileName(), FileMode::Open);&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;StreamWriter writer(%stream);&lt;br /&gt;&lt;/span&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;writer.WriteLine(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;Hello, world.&amp;quot;&lt;/span&gt;);&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;writer.Close();&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;And regardless of how many such objects we need to create or use locally, they will all be cleaned up automatically without needing ugly nested structures, and without needing extra lines of code.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Of course, you still have to determine if this is something you want, because sometimes you need objects to live beyond the scope in which they were created. In that case you can use the gcnew operator to use heap semantics.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 12pt;mso-layout-grid-align:none;"&gt;&lt;span style="FONT-SIZE:10pt;FONT-FAMILY:&amp;#39;Courier New&amp;#39;;mso-no-proof:yes;"&gt;FileStream^ stream = &lt;span style="COLOR:blue;"&gt;gcnew&lt;/span&gt; FileStream(Path::GetTempFileName(), FileMode::Open);&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE:12pt;FONT-FAMILY:&amp;#39;Times New Roman&amp;#39;;mso-fareast-font-family:&amp;#39;Times New Roman&amp;#39;;mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;"&gt;C++ gives you the ability to choose the best approach, which is nice if you want to write correct code without wanting to increase the number of lines of code, and structural complexity.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1525890" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Is C++ still a viable language?</title><link>http://msmvps.com/blogs/vandooren/archive/2007/02/05/is-c-still-a-viable-language.aspx</link><pubDate>Mon, 05 Feb 2007 08:02:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:545260</guid><dc:creator>vanDooren</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/rsscomments.aspx?PostID=545260</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/commentapi.aspx?PostID=545260</wfw:comment><comments>http://msmvps.com/blogs/vandooren/archive/2007/02/05/is-c-still-a-viable-language.aspx#comments</comments><description>&lt;P class=MsoNormal style="MARGIN:0cm 0cm 0pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Every so often someone in microsoft.public.dotnet.languages.vc asks about the future of C++, or if he should learn C# or VB.NET instead of C++. Some people are also confused if using C++ means they carry around C++ legacy code if they want to write pure .NET apps.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 0pt;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;With C++ you can write pure .NET apps that have absolutely no legacy luggage whatsoever. On the contrary, you can write .NET apps and still use powerful features like templates. Another advantage is that the next version of VC++ will be able to compile for different .NET platforms (2.0 and higher). &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;You can also reuse native C++ code and libraries and create mixed mode applications. C++/CLI is the only language that allows you to do this.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;But I have also learned C# and .NET, and I have used it since version 1.0 was released. I use it fairly often. In fact, at work I use it as much as possible.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;C# comes with a lot of stuff that makes programming a lot easier and less time consuming. I love C++ but I admit that C++ does not hold a candle to C# when it comes to rapid application development.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;But C++ is still the only viable option for scenarios where you need absolute control over memory and real-time performance. Interoperability with other code is also a strong area for C++ where C# wimps out. Raw performance also makes a difference when you are writing a piece of code that has to crunch through 300 MB of code to do some image processing for example. 95% of all large code bases are still native C++. Vista is completely native as well, so C++ is not going away anytime soon.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;My reason for choosing C# for pure .NET applications is that it is easier to learn, easier to maintain, and less effort to create an application. And C# and VB will also have support for LINQ, and lots of other cool new features that are not planned for C++ at the moment. And by now there are more good C# programmers than C++ programmers. It is far easier to transfer ownership of a C# project.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;I would not touch VB.NET with a 10 foot pole because I think VB is evil incarnate, and the syntax is too verbose and ugly as hell. But that is just my personal preference of course. Most people in the C++ forums would agree. &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;People in the VB forums may have a different opinion of course. &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Anyway, if you want to do .NET programming, learn C#. If you want to do C++ programming, learn C# and C++ and the low level stuff like marshalling and interop. If you want to do C++ programming in the windows world, knowing only native C++ will not suffice anymore in most cases.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0cm 0cm 6pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;C++ is about power and flexibility. C# is about getting the job done with as little fuss and effort as possible. Both have their place.&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=545260" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Differences between C# and C++/CLI, part 2</title><link>http://msmvps.com/blogs/vandooren/archive/2006/08/31/Differences-between-C_2300_-and-C_2B002B002F00_CLI_2C00_-part-2.aspx</link><pubDate>Thu, 31 Aug 2006 20:58:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:145317</guid><dc:creator>vanDooren</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/rsscomments.aspx?PostID=145317</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/commentapi.aspx?PostID=145317</wfw:comment><comments>http://msmvps.com/blogs/vandooren/archive/2006/08/31/Differences-between-C_2300_-and-C_2B002B002F00_CLI_2C00_-part-2.aspx#comments</comments><description>&lt;p&gt;&lt;span style="font-size:10pt;color:#003300;font-family:Verdana;"&gt;One question that comes up from time to time in the C++ newsgroups is that functions or constants that exist in a C++/CLI class library are not accessible in a C# or VB.NET project.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;color:#003300;font-family:Verdana;"&gt;The reason for this is nearly always that that constants or functions are defined outside of a class scope.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;color:#003300;font-family:Verdana;"&gt;Consider the following - perfectly valid - C++/CLI:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:&amp;#39;Courier New&amp;#39;;"&gt;namespace&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt; Bla&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; CONST = 42;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt; func(&lt;span style="color:blue;"&gt;void&lt;/span&gt;)&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;return&lt;/span&gt; CONST;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 0pt;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;};&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;color:#003300;font-family:Verdana;"&gt;You can use CONST and func in other C++/CLI code.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;color:#003300;font-family:Verdana;"&gt;What you can&amp;rsquo;t do is to use those symbols in other .NET languages. The reason is that those languages do not have a concept of things existing outside of a class or struct scope.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;color:#003300;font-family:Verdana;"&gt;In other words, even though the CLR supports it, languages other than C++/CLI do not support free standing functions, variables and constants.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;color:#003300;font-family:Verdana;"&gt;A simple workaround is to put them in a dummy class. For example, you could put all constants in a class called &amp;lsquo;Consts&amp;rsquo;, and functions in a class called &amp;lsquo;Functions&amp;rsquo;.&lt;/span&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=145317" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/cplusplus/default.aspx">cplusplus</category></item><item><title>Differences between C# and C++/CLI, part 1</title><link>http://msmvps.com/blogs/vandooren/archive/2006/08/31/Differences-between-C_2300_-and-C_2B002B002F00_CLI_2C00_-part-1.aspx</link><pubDate>Thu, 31 Aug 2006 20:56:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:145312</guid><dc:creator>vanDooren</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/rsscomments.aspx?PostID=145312</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/vandooren/commentapi.aspx?PostID=145312</wfw:comment><comments>http://msmvps.com/blogs/vandooren/archive/2006/08/31/Differences-between-C_2300_-and-C_2B002B002F00_CLI_2C00_-part-1.aspx#comments</comments><description>&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Learning C# if you already know C++ is relatively easy. The syntax is similar, and the semantics are definitely a lot easier to understand.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Since C# looks like C++, most of the concepts are easy to learn, but here and there are a few details that might cause problems later on.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;On of these things is the difference between the meaning of classes and structures in C# and C++.&lt;/font&gt;&lt;/p&gt;&lt;h1 style="margin:12pt 0cm 3pt;"&gt;&lt;font size="5"&gt;C++&lt;/font&gt;&lt;/h1&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;With C++, a distinction needs to be made between native C++ and C++/CLI&lt;/font&gt;&lt;/p&gt;&lt;h2 style="margin:12pt 0cm 3pt;"&gt;&lt;em&gt;Native C++&lt;/em&gt;&lt;/h2&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Classes and structures are virtually identical in native C++. Instances can be located on the stack or on the heap. It all depends on how they are instantiated.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;The only difference between the two is that in a class, the default access of a class member is private, while the default access of a structure member is public.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;That&amp;rsquo;s all there is to it.&lt;/font&gt;&lt;/p&gt;&lt;h2 style="margin:12pt 0cm 3pt;"&gt;&lt;em&gt;C++/CLI&lt;/em&gt;&lt;/h2&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;With the rise of C++/CLI, the compiler needs to know&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt 36pt;text-indent:-18pt;tab-stops:list 36.0pt;"&gt;&lt;font face="Times New Roman"&gt;&lt;span&gt;&lt;font size="3"&gt;a)&lt;/font&gt;&lt;span style="font:7pt &amp;#39;Times New Roman&amp;#39;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;font size="3"&gt;whether a type is managed or unmanaged, and&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt 36pt;text-indent:-18pt;tab-stops:list 36.0pt;"&gt;&lt;font face="Times New Roman"&gt;&lt;span&gt;&lt;font size="3"&gt;b)&lt;/font&gt;&lt;span style="font:7pt &amp;#39;Times New Roman&amp;#39;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;font size="3"&gt;whether the type (if managed) is a reference or value type.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;To solve this, the new keywords &amp;lsquo;ref&amp;rsquo; and &amp;lsquo;value&amp;rsquo; are used. The term &amp;lsquo;ref &lt;em&gt;type&lt;/em&gt;&amp;rsquo; indicated a reference type. The type is managed, and is located on the managed heap. The term &amp;lsquo;value &lt;em&gt;type&lt;/em&gt;&amp;rsquo; indicates a managed value type. It is also managed, but located on the stack&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Type can be either a class or a structure. As a result, it is possible for classes to be value types, and structures to be reference types in C++.&lt;/font&gt;&lt;/p&gt;&lt;h2 style="margin:12pt 0cm 3pt;"&gt;&lt;em&gt;Semantics&lt;/em&gt;&lt;/h2&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;In C++, the way in which you instantiate &amp;ndash; and not the declaration &amp;ndash; of a type determines whether it lives on the heap or on the stack.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;2 important consequences of type location in C++ are that stack based instances are fixed in memory during their lifetime, and that stack based instances are guaranteed to be cleaned up when the go out of scope.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;In reality, the C++ compiler is cheating you. It does some things behind your back to fool you into thinking that things live on the stack, while in reality they live on the managed heap.&lt;/font&gt;&lt;/p&gt;&lt;ul style="margin-top:0cm;"&gt;&lt;li class="MsoNormal" style="margin:0cm 0cm 6pt;tab-stops:list 36.0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Native types are either allocated on the stack or the native heap, based on whether you use stack semantics or operator new.&lt;/font&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin:0cm 0cm 6pt;tab-stops:list 36.0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Value types can be created on the stack, on the native heap and on the managed heap, based on whether you instantiate them using stack semantics, operator new, or operator gcnew. But still, what you see is what you get.&lt;/font&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin:0cm 0cm 6pt;tab-stops:list 36.0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Reference types are always created on the managed heap. Even if you use stack semantics for instantiating a reference type, the compiler still allocates the object on the managed heap. The compiler then takes care to insure that the object behavior (like deterministic cleanup etc&amp;hellip;) mimics what you would expect of a stack based object.&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;If you are programming C++ in a mixed mode environment (both native and managed code in the same module) it is important to be aware of these possibilities and their impact.&lt;/font&gt;&lt;/p&gt;&lt;h1 style="margin:12pt 0cm 3pt;"&gt;&lt;font size="5"&gt;C#&lt;/font&gt;&lt;/h1&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;C# has a type system that is a lot easier to grasp.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Classes are reference types. They are always created on the managed heap.&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Structures are value types. They are always created on the stack. C# -like C++/CLI- also supports the concept of boxing, where a copy of the value type is saved on the managed heap.&lt;/font&gt;&lt;/p&gt;&lt;h1 style="margin:12pt 0cm 3pt;"&gt;&lt;font size="5"&gt;Conclusion&lt;/font&gt;&lt;/h1&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;C# and C++/CLI share some type traits. The cases that are identical in C++/CLI and C# are in the following table:&lt;/font&gt;&lt;/p&gt;&lt;table cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse:collapse;border:medium none;"&gt;&lt;tr&gt;&lt;td style="padding-right:5.4pt;padding-left:5.4pt;padding-bottom:0cm;width:147.6pt;padding-top:0cm;background-color:transparent;border:windowtext 1pt solid;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:windowtext 1pt solid;padding-left:5.4pt;padding-bottom:0cm;border-left:#ece9d8;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;C#&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:windowtext 1pt solid;padding-left:5.4pt;padding-bottom:0cm;border-left:#ece9d8;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;C++/CLI&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:#ece9d8;padding-left:5.4pt;padding-bottom:0cm;border-left:windowtext 1pt solid;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Value type&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:#ece9d8;padding-left:5.4pt;padding-bottom:0cm;border-left:#ece9d8;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;struct&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:#ece9d8;padding-left:5.4pt;padding-bottom:0cm;border-left:#ece9d8;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;value class&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:#ece9d8;padding-left:5.4pt;padding-bottom:0cm;border-left:windowtext 1pt solid;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Reference type&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:#ece9d8;padding-left:5.4pt;padding-bottom:0cm;border-left:#ece9d8;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;class&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right:windowtext 1pt solid;padding-right:5.4pt;border-top:#ece9d8;padding-left:5.4pt;padding-bottom:0cm;border-left:#ece9d8;width:147.6pt;padding-top:0cm;border-bottom:windowtext 1pt solid;background-color:transparent;"&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;ref struct&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0cm 0cm 6pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;All other types in C++ and C++/CLI do not have a 100% equivalent in C#.&lt;/font&gt;&lt;/p&gt;&lt;span style="font-size:12pt;font-family:&amp;#39;Times New Roman&amp;#39;;"&gt;When building class libraries for use in other .NET enabled languages, you have to watch out for incompatibilities that can be caused by using C++/CLI specific types.&lt;/span&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=145312" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/vandooren/archive/tags/cplusplus/default.aspx">cplusplus</category></item></channel></rss>