I am having trouble releasing Excel Interop Com Objects which is causing my c# application to crash when I attempt to save and then close an Excel workbook created via Excel Interop. I feel the issue is that in some cases I am using ‘2 dots’ with excel interop COM objects which from what I’ve read is not allowed. I have eliminated 2 dots from most lines of code, but I am having troulbe figuring out a way to recreate the following lines of code so that they only use one dot. If anyone has any suggestions I would greatly appreciate it.
workbook = (Excel.Workbook)app.Workbooks.Open(startForm.excelFileLocation,);
workbook = (Excel.Workbook)app.Workbooks.Add(1);
workSheet_range.Font.Color = System.Drawing.Color.FloralWhite.ToArgb();
workSheet_range.Font.Bold = font;
workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb();
Using two dots is not “disallowed” but it certainly can have performance impacts, especially when running in a tight loop.
Each “dot” is a COM call to the Excel library, which can be significantly slower than normal CLR object access. In general, you want to reduce the number of COM calls to as few as possible.
Reducing from two dots to one by splitting into two lines is not going to have any impact unless you reuse the same variable. For example, changing
to
will have ZERO impact on performance, and may even be “optimized” back to the original single-liner if you don’t re-use the
interiorvariable.However, changing
will have one fewer call to
workSheet_range.Fontso you’ll see an incremental benefit.BOTTOM LINE
I wouldn’t be too concerned about changing every two-dot call but instead use a good profiling tool to determine where your code is spending the most time, then tackle that area first.