To demonstrate the problem I've prepared this code:
public interface IParser<T>
{
bool TryParse(string s, out T result);
}
public sealed class TimeSpanParser : IParser<TimeSpan>
{
public bool TryParse(string s, out TimeSpan result)
{
result = TimeSpan.Zero;
return true;
}
}
public static class ParserService
{
public static T Parse<TParser, T>(string s)
where TParser : IParser<T>, new()
{
var parser = new TParser();
parser.TryParse(s, out var result);
return result;
}
}
The way to call ParserService.Parse
is
var r = ParserService.Parse<TimeSpanParser, TimeSpan>("");
As you can see I need to specify TimeSpan
type argument. But definition of the Parse
method is
public static T Parse<TParser, T>(string s)
where TParser : IParser<T>, new()
So in my opinion the compiler can infer T
automatically by TParser
. When I pass TimeSpanParser
as type argument it's known that TimeSpanParser
is IParser<TimeSpan>
so T
is TimeSpan
. Is it possible to not specify T
?
Like this
var r = ParserService.Parse<TimeSpanParser>("");