Given the following code, can someone explain why I can pass a COM object as a value parameter but not as a reference parameter?
private void TestRelease()
{
Excel.Workbook workbook = excel.ActiveWorkbook;
ReleaseVal(workbook); // OK
ReleaseRef(ref workbook); // Fail
}
private void ReleaseVal(Object obj)
{
if (obj != null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}
private void ReleaseRef(ref Object obj)
{
if (obj != null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}