<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">I Love that New Syntax Smell</title><subtitle type="html">&lt;i&gt;C++ articles, code snippets, musings, etc. from Andy Rich&lt;/i&gt;&lt;br&gt;If this is your first time here, you may want to check out my &lt;a color="#FFFFFF" href="http://blogs.msdn.com/arich/archive/2003/11/06/56812.aspx"&gt;blog introduction&lt;/a&gt;.</subtitle><id>http://blogs.msdn.com/arich/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/arich/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2004-07-09T09:53:00Z</updated><entry><title>The VC Team Blog</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2006/06/12/628731.aspx" /><id>http://blogs.msdn.com/arich/archive/2006/06/12/628731.aspx</id><published>2006-06-13T01:01:00Z</published><updated>2006-06-13T01:01:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Arial&gt;Recently launched at &lt;A href="http://blogs.msdn.com/vcblog"&gt;http://blogs.msdn.com/vcblog&lt;/A&gt;, the VC team is attempting an almost frightening level of transparency (in my opinion).&amp;nbsp; Customer comments I've read so far have ranged from mildly disinterested to "I'm never going to use your product again."&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Brandon is fond of saying: "Transparency is painful."&amp;nbsp; It gives people a glimpse into how we're attempting to get and stay organized, and what agility does to a bug bar.&amp;nbsp; The cuts we're making are harsh, and I know that people won't be pleased to see their Connect-filed feedback sometimes not being fixed, but my hope is that the vcblog can help explain why.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;There are some interesting things coming up, and the Tex has made some &lt;A href="http://blogs.msdn.com/texblog/archive/2006/06/04/617708.aspx"&gt;feints&lt;/A&gt; in that direction, but I don't think anyone is ready to show their hands yet.&amp;nbsp; We're close, though - and when the river card hits, we'll see if going all-in made sense.&amp;nbsp; Do we have &lt;A href="http://en.wikipedia.org/wiki/The_nuts"&gt;the nuts&lt;/A&gt;, or are we just nuts?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Expect a post from me on&amp;nbsp;vcblog (if they'll let me) explaining some of the rationale of our big shift - if I can.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=628731" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author></entry><entry><title>Properties Part 2 - defining default properties</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2005/08/10/211482.aspx" /><id>http://blogs.msdn.com/arich/archive/2005/08/10/211482.aspx</id><published>2005-08-11T03:10:00Z</published><updated>2005-08-11T03:10:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Disclaimer.&lt;/STRONG&gt;&amp;nbsp; This is an ancient post.&amp;nbsp; By the looks of it, I originally intended to write this almost a year ago, as a follow up to my &lt;a href="http://blogs.msdn.com/arich/archive/2004/07/27/199213.aspx"&gt;scalar properties writeup&lt;/A&gt;.&amp;nbsp; That was back when I was testing properties (and more exactly, default properties) and some of the design was still in flux.&amp;nbsp; Now it's pretty nailed down (there's very little time to change it for Whidbey, anyhow), and it dawned on me that&amp;nbsp;I hadn't posted this material.&amp;nbsp; So, I'll do it now, with a bit of fixup.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;The Way C# Does It.&lt;/STRONG&gt;&amp;nbsp; Reader Rob Walker asked over a year ago:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;Why do I have to specify the type of the property 3 times in the definition? It makes this new syntax more verbose than the old.&amp;nbsp; Why not just adopt the C# style?&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;Let me start by mentioning that I'm not a member of the design team.&amp;nbsp; I've been included on various discussions that take place regarding the syntax, but I haven't been the one making the decisions.&amp;nbsp; I can only make guesses as to their reasonings (or ask them).&amp;nbsp; Either way, the reasoning is my own.&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;I believe there are two reasons why we wouldn't adopt the C# style.&amp;nbsp; First, there's an existing property syntax that users are familiar with.&amp;nbsp; The changes between the two syntaxes are somewhat minor.&amp;nbsp; Second, the C# property syntax doesn't fit well with C++ paradigms.&amp;nbsp; Though simple, I imagine the syntax could wreak havok on parsers, and could introduce a number of ambiguities in the language.&amp;nbsp; One of the goals of adding CLI to C++ was to not break existing paradigms, where possible.&amp;nbsp; Finally, the designers wanted to do what felt natural to C++ users.&amp;nbsp; C++ users aren't familiar with functions that have no parameter lists, that's unusual.&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;Speaking of how C# does it, here it is:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New" size=2&gt;public class ArrayWrap {&lt;BR&gt;&amp;nbsp; public ArrayWrap(){ arr = new int[10]; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp; public int this[int idx] {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; get{&amp;nbsp; return arr[idx]; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set{&amp;nbsp; arr[idx] = value; }&lt;BR&gt;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp; private int[] arr;&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;And here's code to produce the equivalent class in C++:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New" size=2&gt;public ref class ArrayWrap {&lt;BR&gt;public:&amp;nbsp; &lt;BR&gt;&amp;nbsp; ArrayWrap(){ arr = gcnew array&amp;lt;int&amp;gt;(10); }&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp; property int default[int] {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int get(int idx){&amp;nbsp; return arr[idx]; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void set(int idx, int value){ arr[idx] = value; }&lt;BR&gt;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New" size=2&gt;private:&lt;BR&gt;&amp;nbsp; array&amp;lt;int&amp;gt;^ arr;&lt;BR&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;We stole the default keyword&lt;/STRONG&gt; and co-opted it for use in indexed properties.&amp;nbsp; Note that you still make use of the property keyword.&amp;nbsp; When this comes out in IL, it looks almost exactly like the C# indexer - with the default property being named "Item" and the getters and setters also thusly named.&amp;nbsp; You can change the name of the default property by using the attribute System::Reflection::DefaultMember on the class.&amp;nbsp; The compiler sees this attribute and interprets it to mean that the default member you created in the class should be named whatever you pass in the attribute ctor.&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;A weird IL trick &lt;/STRONG&gt;leads to another way you can generate a class with a default indexer using that attribute.&amp;nbsp; In IL, a property is a default indexer if it meets two requirements: 1) it is an indexed property, and 2) its name matches the name in the DefaultMember attribute.&amp;nbsp; So, you can actually generate a default member in C++ using code like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P dir=ltr&gt;&lt;FONT face="Courier New" size=2&gt;[System::Reflection::DefaultMember("Foo")]&lt;BR&gt;public ref class ArrayWrap {&lt;BR&gt;public:&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp; property int Foo [int] { ...&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;However, I do not recommend this approach, except as a possible way to workaround bugs, or to obfuscate for fun, as the C++ compiler will not recognize Foo as a default property when &lt;EM&gt;compiling&lt;/EM&gt;, only when &lt;EM&gt;importing&lt;/EM&gt;.&amp;nbsp; Also, although it is legal in IL, I do not recommend using this trick to make default scalar properties.&amp;nbsp; That's just unpleasant.&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;In a future posting,&lt;/STRONG&gt; I'll go over the various ways you can access properties, which may come in handy as workarounds.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=211482" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /><category term="post responses" scheme="http://blogs.msdn.com/arich/archive/tags/post+responses/default.aspx" /></entry><entry><title>The long-awaited return of DF</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2005/06/09/427389.aspx" /><id>http://blogs.msdn.com/arich/archive/2005/06/09/427389.aspx</id><published>2005-06-09T23:10:00Z</published><updated>2005-06-09T23:10:00Z</updated><content type="html">&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Back from the dead&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Well, not precisely dead, but I certainly began feeling that way - shipping a product is hard work, and it is incredibly easy to get "heads down." Here on the VCQA team, we're very focused on stabilization. A lot of testruns. Harsh bug bars. Only the highest quality fixes. Beta &lt;/FONT&gt;&lt;FONT face=Arial&gt;2 is &lt;A href="http://lab.msdn.microsoft.com/vs2005/"&gt;out the door&lt;/A&gt;, and it's time to tighten up for the endgame.&amp;nbsp; &lt;/FONT&gt;&lt;FONT face=Arial&gt;As we're fond of saying: the product has 2005 on it.&amp;nbsp; By my count, we've got about 6 months to ship a product.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;About that DF thing&lt;/STRONG&gt;. While composing what was to be my final post on this whole DF topic, I ran into a brick wall. I couldn't seem to explain how to tie it together. It didn't fit well enough with the &lt;/FONT&gt;&lt;FONT face=Arial&gt;&lt;a href="http://blogs.msdn.com/arich/archive/2004/09/23/233683.aspx"&gt;&lt;FONT face="Courier New"&gt;Dispose(bool)&lt;/FONT&gt; pattern&lt;/A&gt;. There were too many caveats - too many special cases that the user had to handle, both from the authoring end and the consuming end.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;The new, new DF&lt;/STRONG&gt; is more straightforward. When I heard about it, I was glad. Glad as a &lt;EM&gt;user&lt;/EM&gt; - as a &lt;EM&gt;tester&lt;/EM&gt;, when I hear "redesign," it sounds an awful lot like "here's a bunch of extra testing work, and some of those cool tests you wrote can go in the wastebasket." Even though the redesign impacted my already heavy workload (my dentist: "Do you know you're grinding your teeth at night?"), I was happy to see we were doing what I felt was right.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;The executive overview&lt;/STRONG&gt; is this : we implement the&amp;nbsp;Dispose(bool) pattern for you, and hook it up in the best way possible to your base. (If you have one.)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Here, I'm only going to delve into the first part; that "best way possible" stuff requires a complex decision tree that I don't have the energy to iterate over right now.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;We still have the same general design: &lt;FONT face="Courier New"&gt;~T&lt;/FONT&gt; is basically your &lt;/FONT&gt;&lt;FONT face=Arial&gt;destructor, which&amp;nbsp;we implement with&amp;nbsp;&lt;FONT face="Courier New"&gt;Dispose(void)&lt;/FONT&gt;, and &lt;FONT face="Courier New"&gt;!T&lt;/FONT&gt; is basically your finalizer,&amp;nbsp;implemented through&amp;nbsp;&lt;/FONT&gt;&lt;FONT face=Arial&gt;&lt;FONT face="Courier New"&gt;Finalize(void)&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Arial&gt;. What we've added to the mix is &lt;FONT face="Courier New"&gt;Dispose(bool)&lt;/FONT&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Here's how I would write what the compiler does for you:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;Dispose(&lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt; disposing) {&lt;BR&gt;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;if&lt;/FONT&gt;(disposing) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;GC::SuppressFinalize(&lt;FONT color=#0000ff&gt;this&lt;/FONT&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#006400&gt;//call your ~T code&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; }&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;else&lt;/FONT&gt; { &lt;FONT color=#006400&gt;/* call your !T code */&lt;/FONT&gt;&amp;nbsp;}&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; &lt;FONT color=#006400&gt;//contingent upon what your base class looks like we may call&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; __super::Dispose(disposing);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;The major difference between our &lt;FONT face="Courier New"&gt;Dispose(bool)&lt;/FONT&gt; and the one&amp;nbsp;suggested by the&amp;nbsp;CLR Dispose pattern is the &lt;FONT face="Courier New"&gt;else&lt;/FONT&gt;. This is mostly because the user writes &lt;/FONT&gt;&lt;FONT face=Arial&gt;&lt;FONT face="Courier New"&gt;Dispose(bool)&lt;/FONT&gt; in the CLR version - in the C++ version, the compiler authors it, so it is left up to the user how to handle it. (The best thing to do, usually, is to invoke the finalizer from the destructor, unless you *really* know what you're doing.)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Typically, you release Disposable managed items from your destructor and unmanaged items for your finalizer. I was confused by this at first.&amp;nbsp; &lt;/FONT&gt;&lt;FONT face=Arial&gt;For some reason, I thought it would be the reverse. But eventually, I &lt;/FONT&gt;&lt;FONT face=Arial&gt;understood why.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;An object allocated on the GC heap has a few potential invocation states for cleanup: user-invoked, intentional finalization, and unintentional finalization. &lt;/FONT&gt;&lt;FONT face=Arial&gt;Intentional finalization is easiest, so we'll tackle it first.&amp;nbsp; &lt;/FONT&gt;&lt;FONT face=Arial&gt;Sometimes, the cleanup of an object isn't a priority. If you're just dealing with managed heap space, for example, the GC is great at that. &lt;/FONT&gt;&lt;FONT face=Arial&gt;It prioritizes, finds the right times to go about cleaning up, and is quite efficient at it.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;As to user-invoked cleanup; when dealing with a scarce or untracked resource (e.g. hunks of unmanaged heap space, or database connections), you typically want to free up those resources as soon as you know you're done with them. This is why you want objects with Disposers: it's good engineering practice to let go of scarce resources ASAP.&amp;nbsp; That's why you want to provide them to your users.&amp;nbsp; However, not all of the people using your type may get this (or care) and - this is important - there's no way in the CLR to&amp;nbsp;&lt;STRONG&gt;get&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face=Arial&gt; them to care. You can't force it. Heck, you yourself are bound to forget now and then.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;So we need to handle unintentional finalization, to&amp;nbsp;make sure we don't leak. In your Finalizer, you clean up those resources that wouldn't be cleaned up on their own. And those are the &lt;STRONG&gt;&lt;EM&gt;only&lt;/EM&gt;&lt;/STRONG&gt; resources you should clean up in a Finalizer.&amp;nbsp; You should read that again, it's an important distinction in my mind.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Finalization is NOT deterministic&lt;/STRONG&gt;. When your finalizer is invoked by the CLR, you can't be certain that any finalizable objects you have references to have not already been finalized. Trying to Dispose something that's already been finalized is dangerous.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;With all these strong admonitions and specifics, a good example is due - look for that in an upcoming post.&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=427389" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author></entry><entry><title>C++ DF Discussion on hold</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2005/01/17/354782.aspx" /><id>http://blogs.msdn.com/arich/archive/2005/01/17/354782.aspx</id><published>2005-01-18T00:30:00Z</published><updated>2005-01-18T00:30:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Astute readers&lt;/strong&gt; may note two things: 1) It has been a while since I posted what should have been a followup to the previous posts, wherein I complete my discussion of the C++ DF model, and finally get this DF monkey off my back.&amp;nbsp; And 2) that a previous post (DF Part 2, where I actually explain how the C++ DF model works) has been temporarily removed.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Many reasonable scenarios&lt;/strong&gt;&amp;nbsp;exist to explain these phenomena.&amp;nbsp; Suprisingly, the truth has very little to do with the Bermuda Triangle.&amp;nbsp; I believe the C++ DF model may be undergoing some revision - and it would be a Bad Thing to leave up a post on what could be an outdated (and incorrect) description of the model.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;When the language design team&lt;/strong&gt; (a team I must stress I am an &lt;em&gt;&lt;strong&gt;observer&lt;/strong&gt; &lt;/em&gt;of) has Finalize()d their design, I will recreate post #2 anew, and the final post on DF will also see the light of day.&amp;nbsp; I can't say much about it yet (not until I've had a chance to play around with it), but from what I've seen, I'm very pleased with the new design.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;So, I'm on board with the new design&lt;/strong&gt;, if for no other reason than the fact that my final post (how the C++ DF model and the CLR Dispose pattern) was nearly impossible for me to write under the previous design.&amp;nbsp; Not because I'm a poor writer :), but because the previous design left the developer with some very tough problems to handle elegantly.&amp;nbsp; This new design is far more elegant, to my eyes.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;While we wait&lt;/strong&gt; on that post, I may take some time later this week to mull over some general issues that have been bothering me recently.&amp;nbsp; In the meantime, here's a &lt;a href="http://pluralsight.com/blogs/hsutter/archive/2004/11/23/3666.aspx"&gt;juicy article&lt;/a&gt; by &lt;a href="http://pluralsight.com/blogs/hsutter/default.aspx"&gt;Herb&lt;/a&gt; (who is a member of the language design team) on a subject very close to our current lines of discussion.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=354782" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /><category term="errata" scheme="http://blogs.msdn.com/arich/archive/tags/errata/default.aspx" /></entry><entry><title>Deterministic Finalization IV - Benefits, part II</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/11/05/253006.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/11/05/253006.aspx</id><published>2004-11-05T18:52:00Z</published><updated>2004-11-05T18:52:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Long ago&lt;/strong&gt;, I wrote a post on the first part of DF benefits.&amp;nbsp; Now, I'm finally getting back to it.&amp;nbsp; My apologies about the laxness in posting.&amp;nbsp; Blame it on my Cards losing to the Sox.&amp;nbsp; And on being really busy with testpasses and bug bounces for a while.&amp;nbsp; We're finally settling down a little bit, so that gives me time to pull out the keyboard and do some more blogging.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Last time&lt;/strong&gt;, we discussed ref types on the stack, and the benefits they provide.&amp;nbsp; But, if you can put ref types on the stack, where else can you put them?&amp;nbsp; Try this one on for size:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;ref struct&lt;/font&gt; A{ ~A(){ Console::WriteLine("~A()"); } };&lt;br /&gt;&lt;font color="#0000ff"&gt;ref struct&lt;/font&gt; B{&lt;br /&gt;&amp;nbsp; A a1;&lt;br /&gt;&amp;nbsp; A a2;&lt;br /&gt;};&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; B b;&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Compile and run&lt;/strong&gt; that, and you'll see ~A() output twice.&amp;nbsp; Now, you can have ref types as class members, and we call the destructors for you automatically.&amp;nbsp; Dig this, they can even be static!&amp;nbsp; The same rules as native C++ apply all over the place.&amp;nbsp; If I want to call a constructor with arguments, I can do it inside the ctor initializer list.&amp;nbsp; And object unwinding works as well.&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Okay,&lt;/strong&gt; so maybe I didn't need to split this into a separate posting.&amp;nbsp; I could have bundled it, and divluged this little nugget of coolness weeks ago.&amp;nbsp; In any case, next time, I'll wrap up this whole DF business with a discussion on how our DF model works with the CLR Dispose pattern.&amp;nbsp; (See part 1, or some CLR persons' blog for more on the CLR Dispose pattern.)&amp;nbsp; After that, I'll probably get into copy ctors on ref types,&amp;nbsp;or&amp;nbsp;maybe&amp;nbsp;my new favorite subject: hidebysig.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=253006" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /></entry><entry><title>Deterministic Finalization III - Benefits, part 1</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/10/18/244148.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/10/18/244148.aspx</id><published>2004-10-18T21:34:00Z</published><updated>2004-10-18T21:34:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;I'm pretty angry&lt;/strong&gt; at&amp;nbsp;blogs.msdn.com right now (or maybe I'm just angry at myself), as it completely nuked a post I had composed, because my session had timed out on it.&amp;nbsp; I went to post, and it asked me to log in, and in the process destroyed a lot of work.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;I'll try to put my frustrations out of mind,&lt;/strong&gt; and continue with my increasingly tardy discussion of C++ DF.&amp;nbsp; I've already discussed the CLR Dispose pattern, and the C++ destructor syntax.&amp;nbsp; Originally, I'd planned for this third part to be about interaction between those two models, but I think I'll save that for later, and instead start talking about the benefits of C++ DF, by way of discussing a comment on my previous blog post.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;A bit of discussion about Dispose&lt;/strong&gt; brought up an interesting point - in C++, now, we've disabled the ability to call the Dispose function directly.&amp;nbsp; Instead, you call it thusly:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;ref class&lt;/font&gt; MyClass{&lt;br /&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;:&lt;br /&gt;&amp;nbsp; ~MyClass(){} &lt;font color="#008000"&gt;//overrides IDisposable::Dispose()&lt;/font&gt;&lt;br /&gt;};&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; MyClass^ mc = &lt;font color="#0000ff"&gt;gcnew&lt;/font&gt; MyClass();&lt;br /&gt;&amp;nbsp; &lt;font color="#008000"&gt;//do stuff&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&amp;nbsp; mc-&amp;gt;Dispose(); &lt;font color="#008000"&gt;//error!&lt;/font&gt;&lt;br /&gt;&amp;nbsp; mc-&amp;gt;~MyClass(); &lt;font color="#008000"&gt;//legal, calls Dispose()&lt;/font&gt;&lt;br /&gt;&amp;nbsp; delete mc; &lt;font color="#008000"&gt;//also legal, also calls Dispose()&lt;/font&gt;&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;The options left open&lt;/strong&gt; to the user are, I think, a little more familiar.&amp;nbsp; However, there's a large caveat included in this discussion.&amp;nbsp; Imagine, in that innocuous comment titled "do stuff," that I throw an exception.&amp;nbsp; I haven't handled it in my code snippet, so I'm likely to run into issues.&amp;nbsp; The most obvious way to handle this would be to wrap our instantiation in a try/finally:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; MyClass^ mc = &lt;font color="#0000ff"&gt;gcnew&lt;/font&gt; MyClass();&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;try&lt;/font&gt; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#008000"&gt;//do stuff, maybe even throw an exception&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;} &lt;font color="#0000ff"&gt;finally&lt;/font&gt; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;delete&lt;/font&gt; mc; &lt;font color="#008000"&gt;//dispose, regardless of execution path&lt;/font&gt;&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;That works,&lt;/strong&gt; but it's likely to get nasty very quickly when you have multiple disposable objects, as you should be nesting try/finallys (to guard against exceptions in an object Disposer, for example).&amp;nbsp; C# has a nifty piece of syntax set up to handle this, the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfusingstatement.asp"&gt;using statement&lt;/a&gt; (not to be confused with the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfusingdirective.asp"&gt;using directive&lt;/a&gt;).&amp;nbsp; You can scope out the code example in that article for more information on it.&amp;nbsp; In C++, however, we've developed our own bit of &lt;a href="http://en.wikipedia.org/wiki/Syntactic_sugar"&gt;syntactic sugar&lt;/a&gt; to handle disposable objects cleanly: ref types on the stack.&amp;nbsp; With a nod to the example above, here's a quick look at ref types on the stack:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;#using&lt;/font&gt; &amp;lt;System.Drawing.dll&amp;gt;&lt;br /&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System::Drawing;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; Font f1("Arial", 10.0f);&lt;br /&gt;&amp;nbsp; Font f2("Courier New", 8.0f);&lt;br /&gt;&amp;nbsp; &lt;font color="#008000"&gt;//do stuff with these fonts&lt;/font&gt;&lt;br /&gt;} &lt;font color="#008000"&gt;//Dispose called automatically,&amp;nbsp;even with exceptions&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;This is a syntax&lt;/strong&gt; that should be intimately familiar to C++ users - what's more, if you look at the IL, it actually&amp;nbsp;behaves the way they will expect.&amp;nbsp; That is, your disposer gets called even if an exception is thrown during the "do stuff" phase.&amp;nbsp; Ref types on the stack aren't really anything special, they're still a ^ underneath, it's just a lightweight "shim," with a few added bonuses.&amp;nbsp; However, this syntax does represent a slight problem - there are no BCL methods that take a font-on-the-stack, because this whole "on the stack" thing is a C++ convention.&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;To handle this,&lt;/strong&gt; (pun intended) we introduced the &lt;font face="Courier New"&gt;%&lt;/font&gt; (Anders-of) operator.&amp;nbsp; This basically gets you the handle underneath our implementation.&amp;nbsp; So, you can have a ref type on the stack, and still pass it to functions that expect a handle to that type.&amp;nbsp; It's used exactly like the &lt;font face="Courier New"&gt;&amp;amp;&lt;/font&gt; (address-of) operator, which is pleasantly analogous, in light of&amp;nbsp;the &lt;font face="Courier New"&gt;&amp;amp;&lt;/font&gt; reference type and the &lt;font face="Courier New"&gt;%&lt;/font&gt; tracking reference type.&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;#using&lt;/font&gt; &amp;lt;System.Drawing.dll&amp;gt;&lt;br /&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System;&lt;br /&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System::Drawing;&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt; foo(Font^ f);&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; Font f1("Arial", 10.0f);&lt;br /&gt;&amp;nbsp; Console::WriteLine(f1.FontFamily);&lt;br /&gt;&amp;nbsp; foo(%f1); &lt;font color="#008000"&gt;//foo requires a Font^&lt;/font&gt;&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Why not use something similar to the &lt;A href="http://blogs.msdn.com/arich/archive/2003/12/04/41307.aspx"&gt;boxing conversion&lt;/a&gt;?&lt;/strong&gt;&amp;nbsp; (Is it some kind of coincidence that my post on boxing was also apparently nuked at some point?)&amp;nbsp; Anyhow, there's a very good reason for not using a boxing-style conversion, and it's something new we've implemented for Whidbey C++: deep copy semantics.&amp;nbsp; (That is, copy constructors for ref types.)&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;We're starting to stray&lt;/strong&gt; outside the bounds of this conversation, however.&amp;nbsp; We'll save copy ctors for another day.&amp;nbsp; Next time, I'll get into more benefits of DF.&amp;nbsp; Also still to come: how C++ DF interacts with the CLR Dispose pattern.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=244148" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /></entry><entry><title>Deterministic Finalization I - a primer for CLR Dispose</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/09/23/233683.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/09/23/233683.aspx</id><published>2004-09-24T01:49:00Z</published><updated>2004-09-24T01:49:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;A large subject&lt;/strong&gt; like DF needs a few posts.&amp;nbsp; My generalized plan to lay it out will start&amp;nbsp;by describing the&amp;nbsp;CLR's Dispose pattern, how our DF pattern works, and finally how the two patterns fit together.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;The CLR's Dispose patterns&lt;/strong&gt; can be quite confusing.&amp;nbsp; It took me a while to get my mind around &lt;font face="Courier New"&gt;Dispose()&lt;/font&gt; and &lt;font face="Courier New"&gt;Dispose(bool)&lt;/font&gt;, and I'm still not sure I fully understand it.&amp;nbsp; But, I'm going to take a crack at it.&amp;nbsp; Most of what I know about Dispose/Finalize patterns comes from two sources; talks with &lt;A href="http://blogs.msdn.com/branbray"&gt;Brandon&lt;/a&gt;, and &lt;a href="http://www.gotdotnet.com/team/libraries/whitepapers/resourcemanagement/resourcemanagement.aspx"&gt;this excellent whitepaper&lt;/a&gt; on the subject.&amp;nbsp; It's wordy, but it will do a far better job explaining Dispose than I can, and it will talk about more Dispose patterns than I plan to.&amp;nbsp; In fact, it might be a good idea to read that whitepaper, and then return here, to see how my&amp;nbsp;take on it differs from your own.&amp;nbsp; I'll take this opportunity to remind you that this is&amp;nbsp;my &lt;em&gt;opinion&lt;/em&gt; of the way things are, not the way they really are.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;&lt;font size="2"&gt;&lt;strong&gt;The usefulness of Disposing&lt;/strong&gt; is primarily for freeing unmanaged resources.&amp;nbsp; In a nutshell, the Garbage Collector really only gets invoked when there's memory pressure.&amp;nbsp; But the GC can't "feel" pressure from unmanaged resources.&amp;nbsp; If you have a bunch of objects hanging around that have gobs of natively allocated memory (C++ new, or Marshal.AllocHGlobal), the GC won't know about this memory pressure, and won't begin freeing up these objects.&amp;nbsp; (It may notice the free memory being reduced, but it has no sense of which objects are holding the memory.)&amp;nbsp; The aforementioned whitepaper also talks about database handles, file handles, and message queue handles as other forms of unmanaged resource.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;When creating .NET Disposable types,&lt;/strong&gt; you usually want to create a &lt;font face="Courier New"&gt;Finalize&lt;/font&gt; (C# destructor syntax), &lt;font face="Courier New"&gt;Dispose()&lt;/font&gt;, &lt;font face="Courier New"&gt;Dispose(bool)&lt;font face="Arial"&gt;, and inherit from &lt;/font&gt;System.IDisposable.&lt;/font&gt;&amp;nbsp; &lt;em&gt;Almost all of your actual object cleanup will occur inside &lt;font face="Courier New"&gt;Dispose(bool)&lt;/font&gt;.&amp;nbsp; &lt;/em&gt;That's the pattern they came up with, and it's a good way to keep all your cleanup in one place.&amp;nbsp; We'll get to cases in a second, but first, here's a hunk of C# code plaigarized from the whitepaper:&lt;/font&gt;&lt;/p&gt;&lt;font face="Arial" size="2"&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p class="StyleCodeDarkBlue"&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;public class&lt;/font&gt; BothType: IDisposable{&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;public void&lt;/font&gt; Dispose(){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(&lt;font color="#0000ff"&gt;true&lt;/font&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GC.SupressFinalize(&lt;font color="#0000ff"&gt;this&lt;/font&gt;);&lt;br /&gt;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt; &lt;p class="StyleCodeDarkBlue"&gt;&lt;font face="Courier New"&gt;&amp;nbsp; ~BothType(){&amp;nbsp; &lt;font color="#008000"&gt;//finalizer&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(&lt;font color="#0000ff"&gt;false&lt;/font&gt;);&lt;br /&gt;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt; &lt;p class="StyleCodeDarkBlue"&gt;&lt;font face="Courier New"&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;protected virtual void&lt;/font&gt; Dispose(&lt;font color="#0000ff"&gt;bool&lt;/font&gt; disposing){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;if&lt;/font&gt;(disposing){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#008000"&gt;//'Disposable'&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000"&gt;&amp;nbsp;managed resource&amp;nbsp;cleanup code&lt;/font&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#008000"&gt;//Unmanaged resource cleanup code&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;span style="COLOR: navy"&gt;&lt;font color="#000000"&gt;&lt;font color="#0000ff"&gt;base&lt;/font&gt;.Dispose(disposing);&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;font face="Courier New"&gt;&lt;/font&gt;&lt;/blockquote&gt;&lt;/font&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;When Disposing, the first call is always to &lt;font face="Courier New"&gt;Dispose()&lt;/font&gt;.&lt;/strong&gt;&amp;nbsp; Biggest take-away from one of my talks with Brandon.&amp;nbsp; All disposing starts with that call.&amp;nbsp; The CLR way of doing "chaining" destructors is the manual&amp;nbsp;&lt;font face="Courier New"&gt;Dispose(bool)&lt;/font&gt; pattern.&amp;nbsp; Basically, the user calls &lt;font face="Courier New"&gt;Dispose()&lt;/font&gt;, and inside that function, &lt;font face="Courier New"&gt;Dispose(true)&lt;/font&gt; is called.&amp;nbsp; This does a &lt;font face="Courier New"&gt;callvirt&lt;/font&gt; on &lt;font face="Courier New"&gt;Dispose(bool)&lt;/font&gt;, which will thunk down to the &lt;em&gt;lowest child instance&lt;/em&gt;.&amp;nbsp; Then, you have the manual &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;base&lt;/font&gt;.Dispose(disposing)&lt;/font&gt; calls that will&amp;nbsp;walk up the chain.&amp;nbsp; I had to step up to my whiteboard for a while to prove to myself that this pattern works&amp;nbsp;- but it does.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;The Finalizer is a backup system &lt;/strong&gt;in this paradigm, only there to ensure that your unmanaged resources get cleaned up.&amp;nbsp; That is, if a Disposable object is being Finalized, it's probably a mistake - the user forgot to &lt;font face="Courier New"&gt;Dispose()&lt;/font&gt;.&amp;nbsp; So a call to&amp;nbsp;&lt;font face="Courier New"&gt;Finalize()&lt;/font&gt;&amp;nbsp;yields a call to &lt;font face="Courier New" color="#000000"&gt;Dispose(&lt;font color="#0000ff"&gt;false&lt;/font&gt;)&lt;/font&gt;, which takes care of only those unmanaged resources it holds, those that the GC is unable to clean up.&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Note: a call to &lt;font face="Courier New"&gt;Dispose(&lt;font color="#0000ff"&gt;false&lt;/font&gt;)&lt;/font&gt; should &lt;strong&gt;only&lt;/strong&gt; clean up the unmanaged resources.&amp;nbsp; If we're finalizing, we have no discrete order for finalization.&amp;nbsp; If I try and&amp;nbsp;Dispose objects the GC is aware of within a finalizer, I could be in for a world of pain, because those objects might already be collected.&amp;nbsp;&amp;nbsp;Finalization is &lt;em&gt;&lt;strong&gt;not deterministic&lt;/strong&gt;&lt;/em&gt;.&amp;nbsp; There was an &lt;a href="http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030501clrba/manifest.xml"&gt;MSDN TV episode&lt;/a&gt;&amp;nbsp;where Brad Abrams tells an antecdote about a bug where they were calling Console.WriteLine() in their Finalizer and getting a NullReferenceException, because the Console object had already been finalized.&amp;nbsp; Talk about hard to track down...&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;You clean up your managed resources &lt;/strong&gt;inside the if(disposing) scope.&amp;nbsp; Note that in that scope, you should only&amp;nbsp;clean up your Disposable member objects.&amp;nbsp; Don't bother with objects that don't have a Disposer, those objects will be cleaned up just fine by the GC.&amp;nbsp; It's a&amp;nbsp;trade-off you make between those objects&amp;nbsp;you need to get rid of, to free up&amp;nbsp;scarce resources, and not having to clean it all up right away.&amp;nbsp; Like at home; I might do the dishes on a weeknight (especially if I've run out of ramen bowls), but I don't go vaccuuming&amp;nbsp;the floor every time I walk on it.&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;The call to &lt;font face="Courier New"&gt;GC.SuppressFinalize()&lt;/font&gt;&lt;/strong&gt; shouldn't be overlooked - it ensures the Garbage Collector won't Finalize an object that's already been Disposed.&amp;nbsp; In general, you're either going to Dispose an object, or the GC will Finalize it, not both.&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Though this is the most flexible cleanup model,&lt;/strong&gt;&amp;nbsp;it isn't the only cleanup model for objects in the CLR.&amp;nbsp; The whitepaper goes over most of them.&amp;nbsp; The simplest case is&amp;nbsp;where your object has no Disposable members, or unmanaged references.&amp;nbsp; ("Simple" objects.)&amp;nbsp; No Disposers or Finalizers needed there, the CLR will clean everything up for you.&amp;nbsp; Or, your object might only have&amp;nbsp;non-scarce resources and the aforementioned&amp;nbsp;Simple objects, in which case you might have a Finalizer, but not a Disposer.&amp;nbsp; But, if you've got only unmanaged resources, or both unmanaged and managed resources, you probably want both a Disposer and&amp;nbsp;a Finalizer, where the Finalizer is more a safety net than intended use.&amp;nbsp; The whitepaper also talks about the case where you might want only a Disposer.&amp;nbsp; While this case does exist, I think it should be used with caution.&amp;nbsp; If you or your user forgets to Dispose() such an object, your unmanged resources could be hanging around for a very long time.&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Coming in Part II, &lt;/strong&gt;I'll discuss the C++ DF model, and how it fits with the CLR's &lt;font face="Courier New"&gt;Dispose()&lt;/font&gt; pattern.&amp;nbsp; (Depending on how long it takes, that discussion might be split into two parts.)&amp;nbsp; I'm also planning on a Part III (or IV), where I'll talk about the C# &lt;em&gt;using&lt;/em&gt; declaration (see 2.4 of the whitepaper), and how C++ tackles the same problems.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=233683" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /></entry><entry><title>Another good customer bug</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/09/13/228898.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/09/13/228898.aspx</id><published>2004-09-13T17:28:00Z</published><updated>2004-09-13T17:28:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial" size="2"&gt;Reader Andy Neilson writes in with another bug:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font size="2"&gt;The current compiler implementation has some problems. If the variable is a field of this, then the compiler will die. For example:&lt;/font&gt; &lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;class MyClass { &lt;br /&gt;public: &lt;br /&gt;&amp;nbsp; int i; &lt;br /&gt;&lt;br /&gt;&amp;nbsp; void Foo() { &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;array&amp;lt;int&amp;gt;^ x = {1, 2, 3}; &lt;br /&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; for each (i in x) { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; if (i == 2) break; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&lt;br /&gt;&amp;nbsp; } &lt;br /&gt;};&lt;/font&gt; &lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;This is also a bug.&amp;nbsp; I'll file it in our bug tracking database.&amp;nbsp; I'm not sure what the design decision will be; it feels like we should disallow the construct you show - but I'm not certain.&amp;nbsp; I found a "sort-of" workaround; you can use a tracking reference to an integer to do it, such as:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt; Foo() {&lt;br /&gt;&amp;nbsp; array&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;^ x = {1, 2, 3};&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;int&lt;/font&gt;% a = i;&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;for each&lt;/font&gt; (a in x) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;Thanks for the bug report!&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=228898" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="post responses" scheme="http://blogs.msdn.com/arich/archive/tags/post+responses/default.aspx" /></entry><entry><title>I love when customers find bugs!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/09/09/227557.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/09/09/227557.aspx</id><published>2004-09-09T20:12:00Z</published><updated>2004-09-09T20:12:00Z</updated><content type="html">&lt;div style="DISPLAY: block"&gt;&lt;font face="Arial" size="2"&gt;Reader Rob Walker asks:&lt;/font&gt;&lt;/div&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;div style="DISPLAY: block"&gt;&lt;font face="Arial" size="2"&gt;Is there a neat way of handling dictionaries?&amp;nbsp; I have a Dictionary&amp;lt;Guid, Object^&amp;gt; and want to iterate over the values. &lt;br /&gt;&lt;br /&gt;Currently I have to use the syntax: &lt;br /&gt;&lt;br /&gt;for each(KeyValuePair&amp;lt;Guid, Object^&amp;gt; v in dict) &lt;br /&gt;{ &lt;br /&gt;&amp;nbsp; v.Value ... &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;I can't find an invocation that would allow &lt;br /&gt;&lt;br /&gt;for each (Object^ v in dict-&amp;gt;Values) &lt;br /&gt;&lt;br /&gt;(This is using the Whidbey beta 1 compiler refresh).&lt;/font&gt; &lt;/div&gt;&lt;/blockquote&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Arial" size="2"&gt;I imagine, Rob, that you're getting what I'm getting from the compiler, which is:&lt;/font&gt;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Arial" size="2"&gt;&amp;nbsp;&amp;nbsp; &lt;font face="Courier New"&gt;error C3285: for each statement cannot operate on variables of type 'overloaded-function'&lt;/font&gt;&lt;/font&gt;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Arial" size="2"&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Arial" size="2"&gt;That just doesn't seem right to me.&amp;nbsp; In fact, I made an incredibly simple case, and tried it with a very recent compiler:&lt;/font&gt;&lt;/div&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;using namespace &lt;/font&gt;&lt;font color="#000000"&gt;System;&lt;/font&gt;&lt;/font&gt;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;/font&gt;&amp;nbsp;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;ref struct&lt;/font&gt; R{&lt;br /&gt;&amp;nbsp;&lt;font color="#0000ff"&gt;property&lt;/font&gt; array&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;^ p;&amp;nbsp;&lt;br /&gt;};&lt;/font&gt;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp;R^ r = &lt;font color="#0000ff"&gt;gcnew&lt;/font&gt; R;&lt;br /&gt;&amp;nbsp;for each( &lt;font color="#0000ff"&gt;int&lt;/font&gt; i in r-&amp;gt;p ){}&lt;br /&gt;}&lt;/font&gt;&lt;font face="Arial" size="2"&gt;&lt;/div&gt;&lt;/blockquote&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;And this also gives me that same error.&amp;nbsp; I talked with the guy responsible for testing for each, and he confirmed my suspicion - that's a bug.&amp;nbsp; Good catch!&amp;nbsp; I've filed it in our bug tracking database, and shot it over to the proper developer.&amp;nbsp; (With a note that this bug came from a customer - which always raises bug priority.)&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&amp;nbsp;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;In the meantime, there's a pretty simple workaround.&amp;nbsp; You should be able to get around this problem by calling the property accessor method directly.&amp;nbsp; In my case, that'd be &lt;font face="Courier New"&gt;r-&amp;gt;p::get()&lt;/font&gt;, and in your case, it'd be &lt;font face="Courier New"&gt;dict-&amp;gt;Values::get()&lt;/font&gt;.&amp;nbsp; That should allow you to use &lt;font face="Courier New"&gt;for each&lt;/font&gt; on that collection.&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;&amp;nbsp;&lt;/div&gt; &lt;div dir="ltr" style="DISPLAY: block"&gt;By the way, I'm sort-of touching on bits of the property syntax which I haven't discussed yet in this blog.&amp;nbsp; I'll try to get back to that subject after DF.&lt;/div&gt;&lt;/font&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=227557" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="post responses" scheme="http://blogs.msdn.com/arich/archive/tags/post+responses/default.aspx" /></entry><entry><title>The C++ "for each" syntax</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/09/08/227139.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/09/08/227139.aspx</id><published>2004-09-09T01:09:00Z</published><updated>2004-09-09T01:09:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;For Each?&lt;/strong&gt;&amp;nbsp; I won't go into a huge justification - suffice to say, there are some instances where it is nice to be able to iterate over a set, and perform operations on each member of that set.&amp;nbsp; A good primer might be the MSDN node on &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheforeachstatement.asp"&gt;C# foreach&lt;/a&gt;.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;A basic sample.&lt;/strong&gt;&amp;nbsp; If you're familiar with C++ for statements or the C# foreach statement, the C++ for each statement should be fairly familiar.&amp;nbsp; I'll steal from a C# sample, and convert to C++:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; array&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;^ arr = &lt;font color="#0000ff"&gt;gcnew&lt;/font&gt; array&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;{0,1,2,5,7,8,11};&lt;br /&gt;&lt;font color="#0000ff"&gt;&amp;nbsp; int&lt;/font&gt; even=0, odd=0;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;&amp;nbsp; for each&lt;/font&gt; (&lt;font color="#0000ff"&gt;int&lt;/font&gt; i in arr) {&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;font color="#0000ff"&gt;if&lt;/font&gt; (i%2 == 0)&amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;even++;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color="#0000ff"&gt;else&lt;/font&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; odd++;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp; Console::WriteLine("Found {0} Odd Numbers, and {1} Even Numbers.",&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; odd, even);&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Pretty simple&lt;/strong&gt;.&amp;nbsp; There are some definite advantages to for each.&amp;nbsp; Most importantly, it&amp;nbsp;beats struggling&amp;nbsp;with iterators and IEnumerables almost any day of the week.&amp;nbsp; There are also some interesting things you can do with for each.&amp;nbsp; For example, you&amp;nbsp;can do fun things like &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;for each&lt;/font&gt;(Char c in "abcdefg")&lt;/font&gt;.&amp;nbsp; Go crazy with Generic Collections.&amp;nbsp; And, here's a code sample that I know will make some of you out there salivate:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;#include&lt;/font&gt; &amp;lt;vector&amp;gt;&lt;br /&gt;&lt;font color="#0000ff"&gt;#include&lt;/font&gt; &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; std;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main() {&lt;br /&gt;&lt;font color="#0000ff"&gt;&lt;font color="#000000"&gt;&amp;nbsp; &lt;/font&gt;int&lt;/font&gt; total = 0;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp; vector&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt; v(6);&lt;br /&gt;&amp;nbsp; v[0] = 10; v[1] = 20; v[2] = 30;&lt;br /&gt;&amp;nbsp; v[3] = 40; v[4] = 50; v[5] = 60;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;&lt;font color="#000000"&gt;&amp;nbsp; &lt;/font&gt;for each&lt;/font&gt;(&lt;font color="#0000ff"&gt;int&lt;/font&gt; i in v) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; total += i;&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; cout &amp;lt;&amp;lt; total &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Here's the kicker: you can compile the above &lt;strong&gt;natively&lt;/strong&gt;.&amp;nbsp; Why throw in the CLR if you don't want to?&amp;nbsp; Of course, it compiles just fine &lt;strong&gt;with&lt;/strong&gt; the CLR, but for each works on any STL-compliant container in native C++.&amp;nbsp; Now &lt;strong&gt;that's&lt;/strong&gt; a compiler improvement I can get behind.&amp;nbsp; Next time, I'll take a first look at DF.&amp;nbsp; I think it's going to take me a few posts to get it sorted out.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=227139" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /></entry><entry><title>Pinning Pointers</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/08/27/221588.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/08/27/221588.aspx</id><published>2004-08-27T20:32:00Z</published><updated>2004-08-27T20:32:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Hot on the heels&lt;/STRONG&gt; of my article on interior pointers, comes a much more insightful one by &lt;a href="http://blogs.msdn.com/slippman/"&gt;Stan Lippman&lt;/A&gt; on the same &lt;a href="http://blogs.msdn.com/slippman/archive/2004/08/27/221373.aspx"&gt;issue&lt;/A&gt;.&amp;nbsp; That happens sometimes.&amp;nbsp; I enjoyed the chat we had on the VC++ 2005 Beta, and I wanted to point that there are two other online chats coming up.&amp;nbsp; One is on upgrading COM apps to .NET, and the other is on the library and runtime enhancements in VC++ 2005.&amp;nbsp; They're lumped together with other chats that might be of interest&amp;nbsp;on &lt;A href="http://www.msdn.microsoft.com/chats/"&gt;this page&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;On to pinning pointers.&amp;nbsp; &lt;/STRONG&gt;Sometimes, the interior pointer simply won't suffice.&amp;nbsp; Say I have a function I really need to access in a native code module.&amp;nbsp; For example, I have a native function that loads an array for me, and I want to use it to load a managed array.&amp;nbsp; Sounds like a complicated task, and it can be.&amp;nbsp; Conventional wisdom tells us that the native function can't get access to the managed array, because it's on the GC heap, and could move around all over the place.&amp;nbsp; I can't use &lt;a href="http://blogs.msdn.com/arich/archive/2004/08/16/215313.aspx"&gt;interior pointers&lt;/A&gt; because&amp;nbsp;this is native code.&amp;nbsp; So it seems the onlyh choice left is to make another native array, call into the native function, and then loop through the native array, copying the members over from it to the managed array.&amp;nbsp; Clunky, to say the least.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;There's a better way?&lt;/STRONG&gt;&amp;nbsp; You bet.&amp;nbsp; It's called the pinning pointer.&amp;nbsp; In Managed Extensions, we exposed it using the keyword __pin.&amp;nbsp; In the new syntax, we expose it through another smart-pointer-ish type, &lt;FONT face="Courier New"&gt;pin_ptr&amp;lt;&amp;gt;&lt;/FONT&gt;, located in the &lt;FONT face="Courier New"&gt;cli&lt;/FONT&gt; namespace (the namespace formerly known as &lt;FONT face="Courier New"&gt;stdcli::language&lt;/FONT&gt;).&amp;nbsp; What the pinning pointer does is "pin" our managed object down on the GC heap, preventing the garbage collector from moving it around.&amp;nbsp; In addition to this pinning, it gives us what we need;&amp;nbsp;a conversion to a native pointer.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Though pinning pointers are cool&lt;/STRONG&gt;, they are sometimes not well understood.&amp;nbsp; The best way to think of them is that &lt;EM&gt;an object is pinned so long as a pinning pointer points to it&lt;/EM&gt;.&amp;nbsp; That's important enough a concept that you ought to read that sentence again.&amp;nbsp; I'll wait.&amp;nbsp; What this means is that your pin_ptr object is only pinning something on the GC help while its in scope.&amp;nbsp; When it goes out of scope, it stops pointing to that object, and that object can be moved at any time.&amp;nbsp; That means you can't go saving a native pointer, and expect your pin_ptr to hold it forever.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Dangers of pinning pointers.&amp;nbsp; &lt;/STRONG&gt;In fact, because of the dangers of misinterpretation, we severely restrict where and how you can use pinning pointers.&amp;nbsp; They can't be involved anywhere temporaries are created, can't be the argument to or the return type from a function, can't be members of a type, and can't be involved in casts, to name a few.&amp;nbsp; But this is C++, and what would C++ be without a way to shoot yourself in the foot.&amp;nbsp; Long ago, I wrote an article that included some warnings about the pinning pointer.&amp;nbsp; The examples are in Managed Extensions, but the concepts are pretty solid.&amp;nbsp; Rather than repeat myself, I'll just &lt;a href="http://blogs.msdn.com/arich/archive/2003/12/22/45219.aspx"&gt;link to it&lt;/A&gt;, and request that you read it.&amp;nbsp; Especially that example of how to make a quick and easy GC hole.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Enough talk, let's see an example.&lt;/STRONG&gt;&amp;nbsp; Right.&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;using namespace&lt;/FONT&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using namespace&lt;/FONT&gt; cli;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;void&lt;/FONT&gt; myUnmanagedFunction(&lt;FONT color=#0000ff&gt;wchar_t&lt;/FONT&gt;* p, &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; size){&lt;BR&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;for&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; i=0; i&amp;lt;size; i++)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p[i] = 'A'+i;&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; main(){&lt;BR&gt;&amp;nbsp; array&amp;lt;Char&amp;gt;^ arr = gcnew array&amp;lt;Char&amp;gt;(10);&lt;BR&gt;&amp;nbsp; pin_ptr&amp;lt;Char&amp;gt; ppC = &amp;amp;arr[0];&amp;nbsp; //implicit conversion from interior to pin&amp;nbsp;pointer&lt;BR&gt;&amp;nbsp; myUnmanagedFunction(ppC, 10);&lt;BR&gt;&amp;nbsp; &lt;BR&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;for&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; i=0; i&amp;lt;10; i++) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console::Write(arr[i]);&lt;BR&gt;&amp;nbsp; Console::Write("\n");&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Putting it all together&lt;/STRONG&gt; can be complicated.&amp;nbsp; &lt;a href="http://blogs.msdn.com/branbray"&gt;Brandon&lt;/A&gt; helped me sort it out one day by drawing a helpful diagram, which I'll replicate here.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://www.arich.net/pix/pin_ptr_diag.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Don't quit your day job.&lt;/STRONG&gt;&amp;nbsp; I know, I'm not much of an artist.&amp;nbsp; Think of the arrows as "can convert to."&amp;nbsp; Note that for orthogonality, native pointers can convert to pin and interior pointers.&amp;nbsp; Hey, it's sometimes useful, you'll be glad it's there.&amp;nbsp; That's it for pin pointers.&amp;nbsp; In a future article, I might look at our upcoming for each syntax.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=221588" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /></entry><entry><title>Online Chat: Visual C++ 2005 Beta</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/08/16/215329.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/08/16/215329.aspx</id><published>2004-08-16T20:46:00Z</published><updated>2004-08-16T20:46:00Z</updated><content type="html">&lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;We're having an online VC++ chat this coming Thursday.&amp;nbsp; I and several of my coworkers from all areas of the product (IDE, front-end, back-end, etc.) will be available for questions.&amp;nbsp; If you're interested in attending, here's the announcement they asked us blog hosters to post:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font size="2"&gt;&lt;font face="Arial"&gt;Join the Visual C++ team to discuss your questions and comments on the Beta &lt;br /&gt;release of Visual C++ 2005. Whether you are a first-time user of the Visual &lt;br /&gt;C++ Express Edition Beta (&lt;/font&gt;&lt;a href="http://lab.msdn.microsoft.com/express/visualc"&gt;&lt;font face="Arial"&gt;http://lab.msdn.microsoft.com/express/visualc&lt;/font&gt;&lt;/a&gt;&lt;font face="Arial"&gt;) or &lt;br /&gt;an experienced developer exploring the full Visual Studio 2005 suite, we &lt;br /&gt;want to answer your questions to provide you with a smooth development &lt;br /&gt;experience. So please bring your questions and issues to our attention and &lt;br /&gt;get the answers you need from the Visual C++ team.&lt;br /&gt;&lt;br /&gt;August 19, 2004&lt;br /&gt;12:00 - 1:00 P.M. Pacific time&lt;br /&gt;3:00 - 4:00 P.M. Eastern time&lt;br /&gt;19:00 - 20:00 GMT&lt;br /&gt;&lt;br /&gt;Chat time for cities world-wide:&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;a href="http://www.timeanddate.com/worldclock/fixedtime.html?day=19&amp;amp;month=8&amp;amp;year=2004&amp;amp;hour=12&amp;amp;min=0&amp;amp;sec=0&amp;amp;p1=234&amp;amp;sort=1"&gt;&lt;font face="Arial"&gt;http://www.timeanddate.com/worldclock/fixedtime.html?day=19&amp;amp;month=8&amp;amp;year=2004&amp;amp;hour=12&amp;amp;min=0&amp;amp;sec=0&amp;amp;p1=234&amp;amp;sort=1&lt;/font&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;font face="Arial"&gt;&lt;font size="2"&gt;To add this chat to you calendar:&lt;br /&gt;&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;a href="http://msdn.microsoft.com/chats/outlook_reminders/MSDN_VC2005B_Aug19_04.ics"&gt;http://msdn.microsoft.com/chats/outlook_reminders/MSDN_VC2005B_Aug19_04.ics&lt;/a&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;&lt;font size="2"&gt;For more info on MSDN chats, including other upcoming developer chats, chat&lt;br /&gt;archives, and other info see &lt;a href="http://www.msdn.microsoft.com/chats"&gt;http://www.msdn.microsoft.com/chats&lt;/a&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=215329" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="et cetera" scheme="http://blogs.msdn.com/arich/archive/tags/et+cetera/default.aspx" /></entry><entry><title>Interior Pointers</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/08/16/215313.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/08/16/215313.aspx</id><published>2004-08-16T20:28:00Z</published><updated>2004-08-16T20:28:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Where's the rest of the properties stuff?&lt;/strong&gt;&amp;nbsp; I &lt;strong&gt;was&lt;/strong&gt; going to write about default properties in this entry (and have quite a lengthy one saved for future use), but there are a few disagreements I have with the current implementation of default properties, and I want to see those&amp;nbsp;issues resolved before I discuss a feature.&amp;nbsp; (I hate when I discuss &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/11/56816.aspx"&gt;something&lt;/a&gt;, and then it changes on me, and I have to &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/25/56823.aspx"&gt;backpedal&lt;/a&gt;.)&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;Interior Pointers.&lt;/strong&gt;&amp;nbsp; Instead, I'm going to take this opportunity to delve into interior pointers.&amp;nbsp; I'd say the major difference between interior pointers in V1 and the updated C++ syntax is, in V1, you used the same &lt;font face="Courier New" color="#0000ff"&gt;__gc&lt;/font&gt; keyword to denote both interior pointers and handles to objects (for which we now use the&amp;nbsp;carat or&amp;nbsp;"hat" symbol).&amp;nbsp; The confusion, for many users, is that a &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;__gc&lt;/font&gt; *&lt;/font&gt; would actually be something different, depending on the type of object it pointed to.&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;#&lt;font color="#0000ff"&gt;using&lt;/font&gt; &amp;lt;mscorlib.dll&amp;gt;&lt;br /&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;__gc class&lt;/font&gt; A{&lt;br /&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;:&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;int&lt;/font&gt; i;&lt;br /&gt;};&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int &lt;/font&gt;main(){&lt;br /&gt;&amp;nbsp; A* a = &lt;font color="#0000ff"&gt;new&lt;/font&gt; A;&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;int __gc&lt;/font&gt;&amp;nbsp;*p = &amp;amp;(a-&amp;gt;i);&lt;br /&gt;&amp;nbsp; *p = 10;&lt;br /&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Arial"&gt;&lt;strong&gt;Seems simple enough.&lt;/strong&gt;&amp;nbsp; The confusion, for most users, is why they can't take that&amp;nbsp;&lt;font face="Courier New"&gt;p&lt;/font&gt; and pass it to some unmanaged function or static library and perform the same sorts of operations.&amp;nbsp; The reason, though, is actually somewhat obvious, when you consider it.&amp;nbsp; The type handle &lt;font face="Courier New"&gt;a&lt;/font&gt;&amp;nbsp;is pointing to the&amp;nbsp;GC heap - a managed, garbage-collected heap.&amp;nbsp; At any time, the gc can fire up, and perform collections and compactions.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Arial"&gt;&lt;strong&gt;Compaction?&amp;nbsp; &lt;/strong&gt;The collection doesn't worry us so much - it's the compactions we need to worry about.&amp;nbsp; The&amp;nbsp;GC is allowed to move our objects around during compaction.&amp;nbsp; The runtime then updates the active type handles to continue to point to the proper place.&amp;nbsp; What about types that the runtime doesn't know anything about?&amp;nbsp; They're not updated, plain and simple.&amp;nbsp; That's why the runtime has its own type of pointer that gets updated.&amp;nbsp; Native C++ pointers know nothing about the GC, and frankly, that's the way we want it.&amp;nbsp; To protect the user, we prevent the creation of native pointers to memory in the GC heap (except for pinning pointers - which I'll discuss in a later post).&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Arial"&gt;&lt;strong&gt;What's the new syntax?&lt;/strong&gt;&amp;nbsp; The new syntax is similar to template smart-pointers (such as std::auto_ptr or shared_ptr from the boost library).&amp;nbsp; The interior pointer template name is &lt;font face="Courier New"&gt;interior_ptr&amp;lt;T&amp;gt;&lt;/font&gt; and it's located in the namespace &lt;font face="Courier New"&gt;cli&lt;/font&gt;, along&amp;nbsp;with &lt;font face="Courier New"&gt;array&lt;/font&gt;, &lt;font face="Courier New"&gt;pin_ptr&lt;/font&gt;, and &lt;font face="Courier New"&gt;safe_cast&lt;/font&gt;.&amp;nbsp; The above sample would look something like:&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System;&lt;br /&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; cli;&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;ref class&lt;/font&gt; A{&lt;br /&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;:&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;int&lt;/font&gt; p;&lt;br /&gt;};&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; A^ a = &lt;font color="#0000ff"&gt;gcnew&lt;/font&gt; A;&lt;br /&gt;&amp;nbsp; interior_ptr&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt; p = &amp;amp;(a-&amp;gt;p);&lt;br /&gt;&amp;nbsp; *p = 10;&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;strong&gt;What's the point of interior pointers?&lt;/strong&gt;&amp;nbsp; I was having a discussion with a coworker while preparing this post, and that question came up, and we were both dumbfounded for a split second.&amp;nbsp; The answer came to me, but it took a little while: the number one use in Whidbey is probably by-ref parameters.&amp;nbsp; If I want a by-ref parameter that modifies the value of an int that's passed to it, the best type to use is an &lt;font face="Courier New"&gt;interior_ptr&lt;/font&gt;.&amp;nbsp; The main reason is that native pointers to &lt;font face="Courier New"&gt;T&lt;/font&gt; can convert to &lt;font face="Courier New"&gt;interior_ptr&amp;lt;T&amp;gt;&lt;/font&gt;, so I can pass by-ref parameters from almost anywhere into that function, and have it modify the value whether the type is on the heap or on the stack.&amp;nbsp; For example:&lt;/font&gt;&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; System;&lt;br /&gt;&lt;font color="#0000ff"&gt;using namespace&lt;/font&gt; cli;&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;ref class&lt;/font&gt; A{&lt;br /&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;:&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;int&lt;/font&gt; i;&lt;br /&gt;};&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Courier New" size="2"&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt; applyTen(interior_ptr&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt; p){&lt;br /&gt;&amp;nbsp; *p=10;&lt;br /&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; main(){&lt;br /&gt;&amp;nbsp; A^ a = &lt;font color="#0000ff"&gt;gcnew&lt;/font&gt; A;&lt;br /&gt;&amp;nbsp; &lt;font color="#0000ff"&gt;int&lt;/font&gt; i;&lt;br /&gt;&amp;nbsp; applyTen(&amp;amp;i);&lt;br /&gt;&amp;nbsp; applyTen(&amp;amp;a-&amp;gt;i);&lt;br /&gt;}&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p dir="ltr"&gt;&lt;font face="Arial" size="2"&gt;If the parameter to &lt;font face="Courier New"&gt;applyTen&lt;/font&gt; was an &lt;font face="Courier New"&gt;int*&lt;/font&gt; as opposed to an &lt;font face="Courier New"&gt;interior_ptr&amp;lt;int&amp;gt;&lt;/font&gt;, the types wouldn't match for the second call.&amp;nbsp; In a future article, I'll delve into the pinning pointer.&lt;br /&gt;&lt;/p&gt;&lt;/font&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=215313" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /></entry><entry><title>Properties Part 1 - the updated property syntax</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/07/27/199213.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/07/27/199213.aspx</id><published>2004-07-28T03:38:00Z</published><updated>2004-07-28T03:38:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;What are properties?&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Technically, properties are CLR "aliases."&amp;nbsp; They are exposed as standard methods, and any compiler that consumes them simply transforms the user's code into the proper function calls.&amp;nbsp; Similarly, any compiler that wants to author CLR properties just needs to follow the naming convention rules, and provide the necessary metadata entries.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;The property appears as a red triangle in ildasm.&amp;nbsp; In the shot below, note that the functions implementing the property are not hidden or obfuscated.&amp;nbsp; Indeed, languages that don't support the property can still call the underlying functions.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&lt;IMG src="http://www.arich.net/pix/prop_il.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;The Managed Extensions property syntax&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;__gc class &lt;/FONT&gt;MyClass{&lt;BR&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;__property int&lt;/FONT&gt; get_MyProp(){ ... }&lt;BR&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;__property void&lt;/FONT&gt; set_MyProp(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; value) { ... }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; main(){&lt;BR&gt;&amp;nbsp; MyClass* mc = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; MyClass();&lt;BR&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; mc-&amp;gt;get_MyProp();&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;By using the keyword &lt;FONT face="Courier New" color=#0000ff&gt;__property&lt;/FONT&gt;, and providing a getter and/or a setter (using the naming convention get_PropName), a user was able to signal to the compiler that they wanted the extra property information generated.&amp;nbsp; When a user wanted to call a property, they would simply use the get_ and set_ functions directly.&amp;nbsp; This fit in the C++ syntax neatly, but it wasn't exactly first-class.&amp;nbsp; Whereas other languages could get the length of an array by saying &lt;FONT face="Courier New"&gt;MyArray.Length&lt;/FONT&gt;, we were limited to &lt;FONT face="Courier New"&gt;MyArray-&amp;gt;get_Length()&lt;/FONT&gt;&lt;FONT face=Arial&gt;.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;The new C++ property syntax&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;In the Whidbey syntax, the property is defined as a block, similar to how C# handles properties.&amp;nbsp; The general syntax uses the context-sensitive word &lt;FONT face="Courier New" color=#0000ff&gt;property&lt;/FONT&gt;, followed by the general type of that property, followed by the name.&amp;nbsp; Curly-braces then define the "scope" of the property block, and inside, you're allowed to define get and/or set functions that match the expected signature.&amp;nbsp; (The C# MSDN &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_10_6.asp"&gt;node on properties&lt;/A&gt; provides more context on how C# exposes properties.)&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=#0000ff&gt;ref class&lt;/FONT&gt; MyClass{&lt;BR&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;property int&lt;/FONT&gt; MyProp{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; get(){ ... }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;void&lt;/FONT&gt; set(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; v){ ... }&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Calling properties is also similar to how C# handles them, with a few caveats.&amp;nbsp; In general, you should be able to call a property via its alias name, as in &lt;FONT face="Courier New"&gt;int i = myObject-&amp;gt;MyProp&lt;/FONT&gt;, or &lt;FONT face="Courier New"&gt;myObject-&amp;gt;MyProp = 10&lt;/FONT&gt;.&amp;nbsp; The compiler then transforms these calls into a getter or setter method, as appropriate.&amp;nbsp; This works on all properties, not just those you create.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;More to come&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;In future articles, I'll talk more about what you can do with properties, including:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;default properties / default indexers&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;overloading of properties&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;limitations of properties in C++&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=199213" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="articles" scheme="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx" /></entry><entry><title>.NET, 7.0, 2003, what's it all mean?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/arich/archive/2004/07/09/178503.aspx" /><id>http://blogs.msdn.com/arich/archive/2004/07/09/178503.aspx</id><published>2004-07-09T16:53:00Z</published><updated>2004-07-09T16:53:00Z</updated><content type="html">&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;A reader asked the question:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial size=2&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;EM&gt;Is .NET, in fact, the SAME THING as Visual Studio 7.0? Could it be possible that a developer with .NET would be able to simply open the project file and recompile without rewriting code?&lt;/EM&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;.NET itself is a runtime. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Unfortunately, the word ".NET" has started creeping into a lot of our product names, including our latest Server OS, and a few versions of Visual Studio.&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Visual Studio .NET&lt;/STRONG&gt; is, indeed, version 7.0. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;The latest public offering, &lt;STRONG&gt;Visual Studio .NET 2003&lt;/STRONG&gt; (codename &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:City w:st="on"&gt;&lt;st1:place w:st="on"&gt;Everett&lt;/st1:place&gt;&lt;/st1:City&gt;), is version 7.1.&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;The next product we're going to offer, codename Whidbey (and named externally as &lt;STRONG&gt;Visual Studio 2005&lt;/STRONG&gt;), is version 8.0.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;&lt;o:p&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;(I have a little more on this subject here: &lt;/FONT&gt;&lt;/FONT&gt;&lt;A href="http://blogs.msdn.com/arich/archive/2003/12/17/44187.aspx"&gt;&lt;FONT face=Arial size=2&gt;http://blogs.msdn.com/arich/archive/2003/12/17/44187.aspx&lt;/FONT&gt;&lt;/A&gt;)&lt;/P&gt;&lt;/o:p&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;You'd have to talk to marketing as to why we can't use release names that make sense.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Believe me, they make no sense inside the company, either. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;(At least the next version won't have ".NET" in the product name.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;That was starting to bug me.)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;Furthermore, you should be able to open a VS7.0 project file in VS7.1 (.NET 2003).&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Upon opening it, the IDE will ask if you want to convert the project to 7.1.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Be warned, however, that this conversion is *not* reversible. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Arial size=2&gt;As to your question about being able to take 7.0 code into other versions, and compile immediately, the answer is most usually no.&amp;nbsp; Improvements, bug fixes, and conformance all tend to hurt a compiler's backwards compatibility, and occasionally, we find it necessary to &amp;#8220;break&amp;#8220; our upgrading customers in the name of progress.&amp;nbsp; We try to do this as infrequently as possible.&amp;nbsp; For help on converting from 7.0 to 7.1, I suggest you check out the &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/vclrfBreakingChangesInVisualCCompiler.asp"&gt;breaking changes article&lt;/A&gt; on MSDN.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoPlainText style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=178503" width="1" height="1"&gt;</content><author><name>arich</name><uri>http://blogs.msdn.com/members/arich.aspx</uri></author><category term="post responses" scheme="http://blogs.msdn.com/arich/archive/tags/post+responses/default.aspx" /></entry></feed>