In VS2015:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TypeInference
{
public interface IResolvable<T>
{
T Resolve();
}
public class Trivial<T> : IResolvable<T>
{
private T it;
public T Resolve() { return it; }
public Trivial(T the) { it = the; }
}
public class TrivialText : Trivial<string>
{
public TrivialText(string s) : base(s) { }
}
class Program
{
static string f(IResolvable<string> s)
{
return s.Resolve();
}
static void Main(string[] args)
{
string s = "yet another string";
// error CS0305: Using the generic type 'Trivial<T>' requires 1 type arguments
Console.WriteLine(f(new Trivial("this is a string, right?")));
Console.WriteLine(f(new Trivial(s)));
// ok
Console.WriteLine(f(new Trivial<string>("this is a string, right?")));
// OK
Console.WriteLine(f(new TrivialText("this is a string, right?")));
}
}
}
It seems like the type inference here should be obvious. The first two lines are clearly taking a string, but both forms insist I declare the type parameter. This seems contradictory to the statement made here:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-methods
You can also omit the type argument and the compiler will infer it. The following call to Swap is equivalent to the previous call
Am I doing something wrong?
TIA