title: Worksheet.Name causes OutOfMemoryException
body: <h1>.Net4 C# VSTO4 Excel Add-In:</h1>
  
  <p>The following function is called very often, let's say every second:</p>
  
  <pre><code>   /// &lt;summary&gt;
      /// Gets a unique identifier string of for  the worksheet in the format [WorkbookName]WorksheetName
      /// &lt;/summary&gt;
      /// &lt;param name="workbook"&gt;The workbook.&lt;/param&gt;
      /// &lt;param name="worksheet"&gt;The worksheet.&lt;/param&gt;
      /// &lt;returns&gt;
      /// A unique worksheet identifier string, or an empty string.
      /// &lt;/returns&gt;
      public static string GetWorksheetUniqueIdentifier(Workbook workbook, dynamic worksheet)
      {
          if (workbook == null) return string.Empty;
          if (worksheet == null) return string.Empty;//Note: Worksheet can also be a diagram!
  
          return string.Format("[{0}]{1}", workbook.Name, worksheet.Name);
      }
  </code></pre>
  
  <p><strong>After a while, I am getting the following exception:</strong> </p>
  
  <pre><code>System.OutOfMemoryException
     at System.Collections.Generic.Dictionary`2.Resize()
     at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
     at Microsoft.CSharp.RuntimeBinder.Semantics.SYMTBL.InsertChildNoGrow(Symbol child)
     at Microsoft.CSharp.RuntimeBinder.Semantics.SymFactoryBase.newBasicSym(SYMKIND kind, Name name, ParentSymbol parent)
     at Microsoft.CSharp.RuntimeBinder.Semantics.SymFactory.CreateLocalVar(Name name, ParentSymbol parent, CType type)
     at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.PopulateLocalScope(DynamicMetaObjectBinder payload, Scope pScope, ArgumentObject[] arguments, IEnumerable`1 parameterExpressions, Dictionary`2 dictionary)
     at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCore(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject&amp; deferredBinding)
     at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject&amp; deferredBinding)
     at Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(DynamicMetaObjectBinder action, RuntimeBinder binder, IEnumerable`1 args, IEnumerable`1 arginfos, DynamicMetaObject onBindingError)
     at Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
     at System.Dynamic.DynamicMetaObject.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
     at System.Dynamic.InvokeMemberBinder.Bind(DynamicMetaObject target, DynamicMetaObject[] args)
     at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
     at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
     at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
     at CallSite.Target(Closure , CallSite , Object , Object )
     at TestAddIn.ExcelAccessor.GetWorksheetUniqueIdentifier(Workbook workbook, Object worksheet)
     at TestAddIn.ExcelAccessor.GetCurrentWorksheetUniqueIdentifier()
     at TestAddIn.ExcelAccessor.timerExcelObserver_Tick(Object sender, EventArgs e)--------------------------------------------------------------------------------------------------------
  </code></pre>
  
  <p>Calling code is:</p>
  
  <pre><code>  private static Timer timerExcelObserver = new Timer();
  
  ...
  
    timerExcelObserver.Tick += new EventHandler(this.timerExcelObserver_Tick);
    timerExcelObserver.Interval = 1000;
    timerExcelObserver.Start();
  
  ...
  
    private void timerExcelObserver_Tick(object sender, EventArgs e)
    { 
      ...
      var updatedWorksheetIdentifierString = GetCurrentWorksheetUniqueIdentifier();
      ...
    }
  
    public static string GetCurrentWorksheetUniqueIdentifier()
    {
        return GetWorksheetUniqueIdentifier(ExcelApplication.ActiveWorkbook, ExcelApplication.ActiveSheet);
    }
  </code></pre>
  
  <hr>
  
  <p><strong>I have no idea why I am getting an exception!</strong></p>
  
  <p>Can it maybe help to take a "using" in GetWorksheetUniqueIdentifier?</p>
  
  <pre><code>using(worksheet)
  {
     return string.Format("[{0}]{1}", workbook.Name, worksheet.Name);
  }
  </code></pre>
  
  <p>Does anyone have an answer?</p>