I have following class
public abstract class Rule { protected Rule() { Nodes = new List<Node>(); } public List<Node> Nodes { get; private set; } public int NumberOfElements { get; set; } public abstract Result<Rule> Validate(IEnumerable<Node> validUnits); }
now, also i have :
public class MinNumberOfCredits : Rule { public override string ToString() { return string.Format(NumberOfElements); } public override Result<Rule> Validate(IEnumerable<Node> validUnits) { var totalCredits = validUnits.Sum(x => x.Credits); return NumberOfElements > totalCredits ? new Result<MinNumberOfCredits>(ResultMessage.Fail, NumberOfElements) : new Result<MinNumberOfCredits>(); } }
the problem is that my return type is Result<Rule> but in specific class i must return Result<MinNumberOfCredits>.
In C# you cannot have methods having the same name and parameters but different return types, so what you have in mind does just not work. There a various possible approaches to solve your problem, but they depend on your exact use case:
Rulea generic typeRule<T>and change the return type ofValidatetoResult<T>.Result<Rule>andResult<MinNumberOfCredits>into an interface and return that one.I’m aware that all of those solutions will require further modifications. I just wanted to give you a direction to go.