Consider the following example, where we have a list whose generic parameter is a Tuple with a type that is defined in a using
statement at the top.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCustomName = String;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
List<Tuple<MyCustomName>> someList = new List<Tuple<MyCustomName>>();
MyCustomName someString = "Hello world";
someList.Add(new Tuple<MyCustomName>(someString));
}
}
}
The Add
causes the following compiler error:
The type arguments for method 'System.Collections.Generic.List<System.Tuple<String>>.Add(System.Tuple<String>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
This error is at least misleading in one regard, you cannot explicitly specify the type arguments on Add since it is not itself a generic method.
However, given that, to my understanding, the using
statement is not really doing anything much more sophisticated than find and replace, I do not see why the compiler cannot handle this. Why is this error happening and is there any way around other (other than obviously ditching the using
).
Also, meta-question: what is the correct tag for type deceleration using
?
For me, this line causes the first problem:
using MyCustomName = String;
This one works:
using MyCustomName = System.String;