title: RCW & reference counting when using COM interop in C#
body: <p>I have an application that uses Office interop assemblies. I am aware about the "Runtime Callable Wrapper (RCW)" managed by the runtime. But I am not very sure how the reference count gets incremented. MSDN says,</p>
  
  <blockquote>
    <p>RCW keeps just one reference to the
    wrapped COM object regardless of the
    number of managed clients calling it.</p>
  </blockquote>
  
  <p>If I understand it correctly, on the following example,</p>
  
  <pre><code>using Microsoft.Office.Interop.Word;
  
  static void Foo(Application wrd)
  {
      /* .... */
  }
  
  static void Main(string[] args)
  {
      var wrd = new Application();
      Foo(wrd);
      /* .... */
  }
  </code></pre>
  
  <p>I am passing the instance <code>wrd</code> to another method. But this doesn't increment the internal reference count. So I am wondering on what scenarios the reference count gets incremented? Can anyone point out a scenario where the reference count gets incremented?</p>
  
  <p>Also I read some blog which says avoid using double dots when programming with COM objects. Something like, <code>wrd.ActiveDocument.ActiveWindow</code>. The author claims that compiler creates separate variables to hold the values which will increment the reference counter. IMHO, this is wrong and the first example proves this. Is that correct?</p>
  
  <p>Any help would be great!</p>