Search code examples
c#objectcastingref

Casting an object reference in c#


If I have a function that takes a reference to an object. How do I cast the parameter to avoid a type mismatch?

Dictionary<string, string> mySettings = new Dictionary<string, string>();
.
.
saveSettings(ref mySettings);
.
.
void saveSettings(ref object)
{
}

The call to saveSettings results in the following error message:

cannot convert from 'ref Dictionary' to 'ref object'

I'm not looking for a workaround, I've done that, I would like to know if this direct approach is possible.


Solution

  • var objectMySettings = mySettings as object;
    if (objectMySettings != null)
        saveSettings(ref objectMySettings);