<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>I Love that New Syntax Smell : articles</title><link>http://blogs.msdn.com/arich/archive/tags/articles/default.aspx</link><description>Tags: articles</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Properties Part 2 - defining default properties</title><link>http://blogs.msdn.com/arich/archive/2005/08/10/211482.aspx</link><pubDate>Thu, 11 Aug 2005 03:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:211482</guid><dc:creator>arich</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/arich/comments/211482.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=211482</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category><category domain="http://blogs.msdn.com/arich/archive/tags/post+responses/default.aspx">post responses</category></item><item><title>C++ DF Discussion on hold</title><link>http://blogs.msdn.com/arich/archive/2005/01/17/354782.aspx</link><pubDate>Tue, 18 Jan 2005 00:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:354782</guid><dc:creator>arich</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/arich/comments/354782.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=354782</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category><category domain="http://blogs.msdn.com/arich/archive/tags/errata/default.aspx">errata</category></item><item><title>Deterministic Finalization IV - Benefits, part II</title><link>http://blogs.msdn.com/arich/archive/2004/11/05/253006.aspx</link><pubDate>Fri, 05 Nov 2004 18:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:253006</guid><dc:creator>arich</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/arich/comments/253006.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=253006</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Deterministic Finalization III - Benefits, part 1</title><link>http://blogs.msdn.com/arich/archive/2004/10/18/244148.aspx</link><pubDate>Mon, 18 Oct 2004 21:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:244148</guid><dc:creator>arich</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/arich/comments/244148.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=244148</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Deterministic Finalization I - a primer for CLR Dispose</title><link>http://blogs.msdn.com/arich/archive/2004/09/23/233683.aspx</link><pubDate>Fri, 24 Sep 2004 01:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:233683</guid><dc:creator>arich</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/arich/comments/233683.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=233683</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>The C++ "for each" syntax</title><link>http://blogs.msdn.com/arich/archive/2004/09/08/227139.aspx</link><pubDate>Thu, 09 Sep 2004 01:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:227139</guid><dc:creator>arich</dc:creator><slash:comments>15</slash:comments><comments>http://blogs.msdn.com/arich/comments/227139.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=227139</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Pinning Pointers</title><link>http://blogs.msdn.com/arich/archive/2004/08/27/221588.aspx</link><pubDate>Fri, 27 Aug 2004 20:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:221588</guid><dc:creator>arich</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/arich/comments/221588.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=221588</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Interior Pointers</title><link>http://blogs.msdn.com/arich/archive/2004/08/16/215313.aspx</link><pubDate>Mon, 16 Aug 2004 20:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:215313</guid><dc:creator>arich</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/arich/comments/215313.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=215313</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Properties Part 1 - the updated property syntax</title><link>http://blogs.msdn.com/arich/archive/2004/07/27/199213.aspx</link><pubDate>Wed, 28 Jul 2004 03:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:199213</guid><dc:creator>arich</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/arich/comments/199213.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=199213</wfw:commentRss><description>&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;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>VC++ Whidbey Beta1 Ships!</title><link>http://blogs.msdn.com/arich/archive/2004/06/29/169174.aspx</link><pubDate>Tue, 29 Jun 2004 21:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:169174</guid><dc:creator>arich</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/arich/comments/169174.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=169174</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Finally!&lt;/STRONG&gt;&amp;nbsp; &lt;A href="http://lab.msdn.microsoft.com/vs2005/"&gt;VC++ 2005 Beta1&lt;/A&gt; has dropped, and we had a little party last Friday to celebrate.&amp;nbsp; It was nothing fancy, and I spent most of my time playing bridge in a corner.&amp;nbsp; The full beta is only available to MSDN Subscribers now, and selected partners, but there's still a way for everyone to play around with Whidbey: &lt;A href="http://lab.msdn.microsoft.com/express/default.aspx"&gt;the Express SKUs&lt;/A&gt;!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;What is Express?&lt;/STRONG&gt;&amp;nbsp;It's a way for everyone - students, hobbyists, and nay-sayers, to give the Visual Studio product line a go.&amp;nbsp; In fact, VC++ 2005 Express Beta1 is out there, just waiting for you to go download it.&amp;nbsp; In fact, start the &lt;A href="http://go.microsoft.com/fwlink/?LinkId=31593"&gt;download&lt;/A&gt;&amp;nbsp;(you'll have to cough up a valid email address), and then come back here to read more about it.&amp;nbsp; I'll wait.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Welcome back.&lt;/STRONG&gt;&amp;nbsp; The Express lineup, in my opinion, is freakin' cool.&amp;nbsp; It gives everyone an opportunity to see what's great about our product.&amp;nbsp; We've got a rock-solid optimizing compiler, and a brand-spanking new C++/CLI syntax (that I know you're dying to try out).&amp;nbsp; You also get the best of our IDE, including live code browsing, Intellisense, debugging, and the updated Class Viewer.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;You could win an Xbox and Halo 2.&lt;/STRONG&gt;&amp;nbsp; Got your attention yet?&amp;nbsp; Head over to Channel 9's &lt;A href="http://channel9.msdn.com/express/"&gt;Summer of Express&lt;/A&gt; contest for all the details.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;We want bugs.&lt;/STRONG&gt;&amp;nbsp; We want your bugs.&amp;nbsp; Grab all your code, run it against our Beta, and give us all the bugs you find.&amp;nbsp; We've released a new &lt;A href="http://lab.msdn.microsoft.com/productfeedback/"&gt;product feedback center&lt;/A&gt;, where you can file bugs that go &lt;EM&gt;right into our bug database&lt;/EM&gt;.&amp;nbsp; One click, your computer&amp;nbsp;to our doorstep.&amp;nbsp; You can even&amp;nbsp;keep us responsible by tracking the progress of your bug, and&amp;nbsp;bugs filed by other customers.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;What's new?&lt;/STRONG&gt;&amp;nbsp; I'll probably elaborate on this more, and I might find the energy to work up a few samples.&amp;nbsp; I suggest you try out the new Whidbey language, our generics support, and the new Secure CRT, to name a few of our advancements.&amp;nbsp; Don't try out DF (deterministic finalization), though.&amp;nbsp; Despite what the website may say, DF is &lt;STRONG&gt;not&lt;/STRONG&gt; in beta1.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=169174" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Visual C++ .NET 2003, Free.</title><link>http://blogs.msdn.com/arich/archive/2004/04/19/116080.aspx</link><pubDate>Mon, 19 Apr 2004 17:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:116080</guid><dc:creator>arich</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/arich/comments/116080.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=116080</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;Go &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=272BE09D-40BB-49FD-9CB0-4BFA122FA91B&amp;amp;displaylang=en"&gt;here&lt;/A&gt; to get a free command-line version of VC++ .NET 2003.&amp;nbsp; For you misers, I'm sure you could trick the 7.0 IDE (or even the 6.0&amp;nbsp;one)&amp;nbsp;into using these new binaries.&amp;nbsp; This was mentioned on &lt;A href="http://developers.slashdot.org/article.pl?sid=04/04/18/0146234"&gt;Slashdot&lt;/A&gt; on Sunday.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;I'd recommend carefully reading the included EULA if you intend to use this version of VC to create commercial products.&amp;nbsp; I haven't read it myself, but the download page doesn't explicitly say that this can be used for commercial products.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Having said that, this is a great thing to get for free.&amp;nbsp; As far as I can tell, it isn't crippled in any serious way.&amp;nbsp; Full optimizations are available, and there don't appear to be any time limits.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=116080" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Status, aggregate initialization of CLI arrays</title><link>http://blogs.msdn.com/arich/archive/2004/03/17/91343.aspx</link><pubDate>Wed, 17 Mar 2004 17:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:91343</guid><dc:creator>arich</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/arich/comments/91343.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=91343</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;This is a quickie&lt;/STRONG&gt;.&amp;nbsp; I have some other blog posts about interesting material on the horizon, but I'm always wary of posting information without knowing whether I'm going to violate NDA by writing about it.&amp;nbsp; There should be a real flurry of activity when we finally get the Whidbey Beta 1 out our doors, but I don't expect that to happen for months, yet.&amp;nbsp; We're in &amp;#8220;heads-down&amp;#8220; mode in the FE team, hard at work delivering features that we think will excite and engage our users.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;About aggregate initialization&lt;/STRONG&gt;.&amp;nbsp; How do you do it with managed arrays?&amp;nbsp; I held the suspicion that you couldn't do it, that we didn't provide a way to do it in the new syntax.&amp;nbsp; &lt;A href="http://blogs.msdn.com/slippman"&gt;Stan&lt;/A&gt; showed me how:&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; stdcli::language;&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;array&amp;lt;&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&amp;gt;^ numbers = &lt;FONT color=#0000ff&gt;gcnew&lt;/FONT&gt; array&amp;lt;&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&amp;gt;{4, 6, 8, 10};&lt;BR&gt;for ( &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; i=0; i&amp;lt;numbers-&amp;gt;Length; i++ )&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console::WriteLine( numbers[i] );&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;That's it.&lt;/STRONG&gt;&amp;nbsp; Pretty straightforward, pretty simple.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=91343" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>The CLR team is looking for a few good people</title><link>http://blogs.msdn.com/arich/archive/2004/03/06/85226.aspx</link><pubDate>Sat, 06 Mar 2004 21:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:85226</guid><dc:creator>arich</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/arich/comments/85226.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=85226</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;A href="http://blogs.msdn.com/joelpob"&gt;Joel&lt;/A&gt; just posted over on his blog that the CLR team has some open headcount - they're looking to fill a few positions.&amp;nbsp; I can say that working for Microsoft has been one of the most challenging and rewarding experiences of my short life (pay no attention to the fact that I'm posting this on a Saturday), and if you're interested, and you have the skills, I'm sure they'd love to have you.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Check out the details &lt;A href="http://blogs.msdn.com/joelpob/archive/2004/03/05/85007.aspx"&gt;here&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=85226" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category><category domain="http://blogs.msdn.com/arich/archive/tags/et+cetera/default.aspx">et cetera</category></item><item><title>Ain't said nothin' in a while, but some old posts are now available.</title><link>http://blogs.msdn.com/arich/archive/2004/01/12/57949.aspx</link><pubDate>Mon, 12 Jan 2004 19:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57949</guid><dc:creator>arich</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/arich/comments/57949.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=57949</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Where you been?&amp;nbsp; &lt;/STRONG&gt;Yes, it's true, I've been fairly silent.&amp;nbsp; But I assure you, my reason(s) are good.&amp;nbsp; Mostly, I've been skiing in Colorado, or really busy.&amp;nbsp; But I'm back at work, and once I catch up on the email, there will be more tasty posts from yours truly.&amp;nbsp; (And more code snippets, once I figure out where I'm going to host them.)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;You can now use &lt;A href="http://blogs.msdn.com/arich"&gt;http://blogs.msdn.com/arich&lt;/A&gt;.&lt;/STRONG&gt;&amp;nbsp; I think this is&amp;nbsp;a more meaningful URL, however cosmetic.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;My old posts are now available.&lt;/STRONG&gt;&amp;nbsp; They were (finally) ported over from my old blog.&amp;nbsp; If you never bothered to read them, here are their subjects (and permalinks):&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/06/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/06/56812.aspx"&gt;The first introduction&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/06/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/06/56813.aspx"&gt;That crazy GC Heap&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/07/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/07/56814.aspx"&gt;Context-sensitive keywords&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/10/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/10/56815.aspx"&gt;The ref type&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/11/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/11/56816.aspx"&gt;The value type&lt;/A&gt; (note these &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/25/56823.aspx"&gt;corrections&lt;/A&gt;)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/12/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/12/56817.aspx"&gt;Simple stack code&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/17/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/17/56821.aspx"&gt;Branbray on Handles&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/17/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/17/56822.aspx"&gt;PtrToStringChars v2 remix&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;11/25/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/11/25/56823.aspx"&gt;Corrections, explanations, etc.&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Arial size=2&gt;12/04/03 - &lt;A href="http://blogs.msdn.com/arich/archive/2003/12/04/56824.aspx"&gt;Goodbye to my original blog&lt;/A&gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Hopefully, that will satisfy the content mongers for a little while, so I have time to get my bearings. :)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=57949" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category></item><item><title>Pointer to String chars - Everett style</title><link>http://blogs.msdn.com/arich/archive/2003/12/22/45219.aspx</link><pubDate>Mon, 22 Dec 2003 22:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:45219</guid><dc:creator>arich</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/arich/comments/45219.aspx</comments><wfw:commentRss>http://blogs.msdn.com/arich/commentrss.aspx?PostID=45219</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;Garrett asked:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr&gt;
&lt;P&gt;&lt;FONT size=2&gt;If the source text is in a CLR String, and we want to pass(even read-only) to unmanaged code, it appears that there is no way to get a pointer to the String's buffer directly. We have to use the marshalling stuff to get it there, which in itself makes a copy.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;Given that one of managed C++ and CLI/C++ 's goals (imnsho) is to facilitate leveraging existing native c++ code, has any thought been given to this?&lt;/FONT&gt; &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;&lt;FONT face=Arial size=2&gt;&lt;STRONG&gt;Can I get a native pointer to&amp;nbsp;the data in a&amp;nbsp;CLR String?&lt;/STRONG&gt;&amp;nbsp; The short answer is yes, so long as you don't mind a &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;wchar_t&lt;/FONT&gt;*&lt;/FONT&gt; - which is native analog of&amp;nbsp;the actual backing store type for a CLR String (the CLR type &lt;FONT face="Courier New"&gt;System::Char&lt;/FONT&gt;). &amp;nbsp; Even in Everett, we supported doing this.&amp;nbsp; You have to use a special function in order to get at it, located in the header file &lt;FONT face="Courier New"&gt;&lt;VCCLR.H&gt;, &lt;/FONT&gt;&lt;FONT face=Arial&gt;which shipped with Everett.&amp;nbsp; This header file includes a function, &lt;FONT face="Courier New"&gt;PtrToStringChars&lt;/FONT&gt;, which takes a &lt;FONT face="Courier New"&gt;String*&lt;/FONT&gt; and returns a &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;wchar_t __gc&lt;/FONT&gt;*&lt;/FONT&gt;.&amp;nbsp; You can use the returned pointer - called an &amp;#8220;interior pointer&amp;#8221; - to munge with the string data in a fairly intuitive &amp;#8220;native&amp;#8221; way, as in this example code:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Arial size=2&gt;
&lt;BLOCKQUOTE dir=ltr&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face="Courier New" color=#0000ff&gt;#using&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt; &amp;lt;mscorlib.dll&amp;gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New" color=#0000ff&gt;#include&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt; &amp;lt;vcclr.h&amp;gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New" color=#0000ff&gt;using&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt; &lt;SPAN&gt;namespace&lt;/SPAN&gt;&lt;/FONT&gt; System;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face="Courier New" color=#0000ff&gt;int&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt; main(){&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;String *s = S"abcdefg";&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;FONT color=#0000ff&gt;&lt;SPAN&gt;wchar_t&lt;/SPAN&gt; &lt;SPAN&gt;__gc&lt;/SPAN&gt;*&lt;/FONT&gt; pc = PtrToStringChars(s);&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;for&lt;/SPAN&gt;(&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/SPAN&gt; i=0; i&lt;S-&gt;Length; i++){&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;*(pc+i)+=1; &lt;/FONT&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;//increment each character in the string&lt;/FONT&gt;&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;}&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Console::WriteLine(pc);&lt;SPAN&gt;&amp;nbsp;&lt;FONT color=#008000&gt; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;//writes "bcdefgh"&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Yeah, but I wanted to use a native function.&lt;/STRONG&gt;&amp;nbsp; I'm getting there.&amp;nbsp; Now, you can't convert from a &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;__gc&lt;/FONT&gt;*&lt;/FONT&gt; to a &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;__nogc&lt;/FONT&gt;*&lt;/FONT&gt; (&amp;#8220;native pointer&amp;#8220;), but you can convert from a &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;__gc&lt;/FONT&gt;*&lt;/FONT&gt; to another type - &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;__pin&lt;/FONT&gt;*&lt;/FONT&gt; - which has a conversion to a &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;__nogc&lt;/FONT&gt;*&lt;/FONT&gt;:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;BLOCKQUOTE dir=ltr&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;#using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; &amp;lt;mscorlib.dll&amp;gt;&lt;MSCORLIB.DLL&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;#include&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; &amp;lt;vcclr.h&amp;gt;&lt;VCCLR.H&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;using&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;namespace&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; System;&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; unmanagedStrLenFunction(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;wchar_t&lt;/FONT&gt;&lt;FONT size=2&gt; *c){ &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000 size=2&gt;//counts the length of c&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; count=0;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; while&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;(*c){&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;count++;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;c++; &lt;/FONT&gt;&lt;FONT face="Courier New" color=#008000 size=2&gt;//heh&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&amp;nbsp; }&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; return&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; count;&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; main(){&lt;BR&gt;&amp;nbsp; String *s = S"abcdefg";&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; wchar_t&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;__gc&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;* pc = PtrToStringChars(s);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; wchar_t&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;__pin&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;* ppc = pc;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; x = unmanagedStrLenFunction(ppc);&lt;BR&gt;&amp;nbsp; Console::WriteLine(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;__box&lt;/FONT&gt;&lt;FONT size=2&gt;(x)); &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000 size=2&gt;//writes "7"&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;I could have turned the result of &lt;FONT face="Courier New"&gt;PtrToStringChars&lt;/FONT&gt; directly into a &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;wchar_t __pin&lt;/FONT&gt;*&lt;/FONT&gt; directly, but I wanted to make it absolutely clear.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Wow!&amp;nbsp; Pin pointers are cool!&amp;nbsp; I'm going to use them everywhere!&lt;/STRONG&gt;&amp;nbsp; Whoa there, trigger.&amp;nbsp;There are a few things to keep in mind about pin pointers:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;They can be extremely costly.&lt;/STRONG&gt;&amp;nbsp; The pin pointer works by literally pinning the enclosing type down, so the GC collector can't move it around when its doing collections.&amp;nbsp; Do this too often, or keep the pin pointer around for a relatively long time, and you're seriously hurting the performance of the garbage collector - not a good idea.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;They can't be used everywhere.&lt;/STRONG&gt;&amp;nbsp; By design, because of the costliness and lifetime problems involved with pin pointers, they can't be: members of a type, function return types, function parameters, or temporary variables.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Pin pointers only pin objects for their lifetime.&amp;nbsp; &lt;/STRONG&gt;&lt;FONT face=Arial size=2&gt;&lt;FONT face=Arial&gt;&lt;/FONT&gt;&lt;/FONT&gt;This leaves open the possibility of GC holes.&amp;nbsp; That is, you can get a native pointer to the GC, release the pin pointer, and then leave yourself a huge GC hole.&amp;nbsp; For example:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;SPAN&gt;&lt;FONT face=Arial size=2&gt;
&lt;BLOCKQUOTE dir=ltr&gt;
&lt;BLOCKQUOTE dir=ltr&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face="Courier New" color=#0000ff&gt;__gc&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt; &lt;SPAN&gt;class&lt;/SPAN&gt;&lt;/FONT&gt; A{ &lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New" color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;:&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/SPAN&gt; i; &lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;}; &lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New" color=#0000ff&gt;int&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;* gchole(A* a){&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT color=#0000ff&gt; &lt;SPAN&gt;__pin&lt;/SPAN&gt;&lt;/FONT&gt;* p = &amp;amp;(a-&amp;gt;i);&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;return&lt;/FONT&gt;&lt;/SPAN&gt; p; &lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;What's so wrong with that code?&lt;/STRONG&gt;&amp;nbsp; On the surface, it looks pretty benign.&amp;nbsp; But remember that the object passed in (&lt;FONT face="Courier New"&gt;a&lt;/FONT&gt;) is only pinned for the lifetime of the pin pointer &lt;FONT face="Courier New"&gt;p&lt;/FONT&gt;.&amp;nbsp; So, when the function returns, you have a native pointer into the GC heap, which would be safe, except &lt;FONT face="Courier New"&gt;p&lt;/FONT&gt; has been destroyed.&amp;nbsp; So, instead, you have a GC hole.&amp;nbsp; The pointer returned from the function &lt;FONT face="Courier New"&gt;gchole&lt;/FONT&gt; is only going to be valid until the next garbage collection - and who knows when that will happen.&amp;nbsp; In short, don't do this, if you want to avoid unexplainable, untraceable, unreproduceable&amp;nbsp;application crashes.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Back to the original question, what about regular &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;char&lt;/FONT&gt;*&lt;/FONT&gt;'s?&amp;nbsp; &lt;/STRONG&gt;No chance, not without incurring a copy cost (either by using API functions that turn &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;wchar_t&lt;/FONT&gt;*&lt;/FONT&gt;'s into &lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;char&lt;/FONT&gt;*&lt;/FONT&gt;'s, or by marshalling).&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P dir=ltr&gt;&lt;SPAN&gt;&lt;FONT face=Arial&gt;In a future article, I'll describe the new syntax versions of the pinning and interior pointer, and some of the (mostly minor) differences.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=45219" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/arich/archive/tags/articles/default.aspx">articles</category><category domain="http://blogs.msdn.com/arich/archive/tags/post+responses/default.aspx">post responses</category></item></channel></rss>