I want to make an interface in C# that defines a method that always returns an object of the implementing class, thus:
public interface IParser {
IParser Parse(string s);
}
public class Parser : IParser {
public Parser Parse(string s) {
return new Parser(s);
}
}
Can I force an implementing class to return an object of its own type? If possible, then how? Or is generics the answer here?
NB: The code is just an illustration, not something that's supposed to run.