如果不使用变量,则将 Word Interop 调用分配给变量的优势

Advantage to assigning a Word Interop call to a variable if it isn't used

假设我接到这样的电话

Document document = application.Documents.Open(...)

然后我可以参考文档并对其进行操作,就像我可以使用任何其他变量一样。例如,

int count = document.Words.Count;

但是,如果我不想对结果做任何事情,将其分配给变量有什么好处吗? IE。这是

Document document = application.Documents.Open(...)

优于

application.Documents.Open(...)

将其分配给变量的主要好处是您可以对其调用 ReleaseComObject。参见 。

另见 https://ausdotnet.wordpress.com/category/technical/com-interop/