Hopefully my question hasn't been answered yet, I have only found this link but it is not really answering my exact question.
Basically I have a class MyClass
which contains a local member currentSettings
of another class Settings
. MyClass
contains a method DoStuff
which uses information that is contained in any object of a Settings
-object.
Now the situation: The method will never be called from within the class itself. Some other class will contain a MyClass
-object and only from there will the method be called. Now I have two possibilities:
class MyClass
{
Settings currentSettings = new Settings();
public void DoStuff()
{
//do stuff here using things stored in "currentSettings"
}
public void DoStuff(Settings settings)
{
//do stuff here using things stored in "settings"
}
}
class SomeClass
{
MyClass myClass = new MyClass();
...
//call method somewhere, with two different options:
myClass.DoStuff();
myClass.DoStuff(myClass.currentSettings);
}
The second variant of course seems a bit overcomplicated, however, it leaves me the freedom to pass any settings to it which might not be the local member currentSettings
and that I will need in some cases.
I am now concerned about the performance difference between these two choices and if the reference passing might be a significant amount slower than using the local settings, as this method might be called very often (up to a couple hundred times a second).
Edit: I have posted a little performance test in the answers and could not find any significant difference between the two methods.