To make sure that a COM object that I need to create is freed/released as soon I didn't need it anymore is simple with the using
statement.
using (dynamic a = new MyFooCOMApplication())
{
...
}
Now suppose I have a Foo COM object. This Foo object has a COM enumeration called Fields, and Fields itself return item of type Field. Assume that the Field object has a property Value, I can write code like:
a.Fields("Test").Value
Or in other words:
a.Fields.Item("Test").Value = 0;
With this statement two temporary objects are created. One of type Fields, one of type Field.
Now my question: Does C# implicit uses a using clause for the both temporary objects or must I write:
using (dynamic fields=a.Fields)
{
using (dynamic field=fields.Item("Test"))
{
field.Value = 0;
}
}
Just to clarify:
- I use an external COM server. So freeing the objects is absolutely required.
- Calling the GC.Collect isn't "what I feel" a good style. Even if hundreds of programmers are using it.
- The code itself is again inside an VSTO Addin of Outlook. So I want to work as clean as possible.