I know in C#, by default, referenced type variables are passed by reference to a method. I have a function which sometimes I just need its return value and sometimes both return value and the changes on the input object are needed outside of the function.
Then I wrote it as the following:
public static List<RuleSet> Load(XmlDocument dom= null)
{
if (dom == null)
{
dom = new XmlDocument();
}
string path = ...
dom.Load(path);
List<RuleSet> ruleSets = new List<RuleSet>();
// load the dom with file and read rules
return ruleSets;
}
Sometimes I need the dom
outside of the function and I call it Load(dom)
and when I just need the return value I call it as Load()
.
However, I know if the parameter was declared with ref
I couldn't set a default value for it and there must be a reason for if, but now I did it with no problem.
Then I suspect my approach to the problem (my function above) is correct or not, or could it be implemented in a better way?