This line of code:
SettingsInstance = SomeOtherSettingsInstance;
does not copy anything inside the objects, instead it overwrites the reference stored in SettingsInstance with the reference stored in SomeOtherSettingsInstance.
The object itself is none the wiser.
Basically, after you have executed the first of the 3 last lines, you have this scenario:
SomeOtherSettingsInstance -----> Object 1 in memory of type Settings
SettingsInstance --------------> Object 2 in memory of type Settings
^
|
+- References
After you've executed the third line, this is how it looks:
SomeOtherSettingsInstance --+--> Object 1 in memory of type Settings
/
SettingsInstance ---------+ Object 2 in memory of type Settings
Now you have two references to the first object, one through each variable, and you've left the new object you just created to rot for the garbage collector to come pick it up later.
If you wish to copy the internals, then yes, you have to copy one property at a time.
I regularly create cloning support like this:
public Settings Clone()
{
Settings clone = CreateCloneInstance();
CloneTo(clone);
return clone;
}
protected virtual Settings CreateCloneInstance()
{
return new Settings();
}
public virtual void CloneTo(Settings clone)
{
clone.RootFolder = RootFolder;
... + any other properties you might have
}
In your scenario, you want to hook up an event before copying things, so you would call it like this:
public Settings SettingsInstance = new Settings();
SettingsInstance.SettingsChanged += SettingsInstance_SettingsChanged;
SomeOtherSettingsInstance.CloneTo(SettingsInstance);
The reason I implement cloning support like that is due to object hierarchies. If that is not an issue for you (you're not going to inherit from Settings), you can just do this:
public Settings Clone()
{
Settings clone = new Settings();
CloneTo(clone);
return clone;
}
public void CloneTo(Settings clone)
{
clone.RootFolder = RootFolder;
... + any other properties you might have
}