I know in C#, by default, objects are passed by reference to a method then the changes reflect on the object outside the method. I have a function which sometimes I just need its return value and sometimes both return value and the input object are needed outside 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 returned 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 that, While 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 better way?
Aucun commentaire:
Enregistrer un commentaire