c# - Any way to make this more generic? -


i have these 2 functions, used search folder within hierarchy:

public folder<t> find (string path)   {   folder<t> result = null;    if (this.path != path &&       childrendict != null)     {     foreach (keyvaluepair<long, folder<t>> child in childrendict)       {       result = child.value.find (path);       }     }    else     {     result = this;     }    return result;   }  public folder<t> find (long id)   {   folder<t> result = null;    if (this.id != id &&       childrendict != null)     {     foreach (keyvaluepair<long, folder<t>> child in childrendict)       {       result = child.value.find (id);       }     }    else     {     result = this;     }    return result;   } 

as can see, similar 1 another. how can re-structure them don't have same code several times, 1 per each property might want use find them?

create method condition parameter logic:

protected folder<t> find(func<folder<t>, bool> condition) {     folder<t> result = null;     if(!condition(this) && childrendict != null) {         foreach(var child in childrendict) {             result = child.value.find(condition);         }     } else {         result = this;     }     return result; } 

rewrite public find methods as:

public folder<t> find(string path) {    return find(f => f.path == path); } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -