C
Carl Johansson
Why won’t my generic method compile?
private static void PrintTable<T>(Table<T> table, string text)
{
Console.WriteLine(text);
foreach (T record in table)
Console.WriteLine(record);
}
The complier reports the following error:
“The type 'T' must be a reference type in order to use it as parameter
'TEntity' in the generic type or method
'System.Data.Linq.Table<TEntity>.”
Looking at the definition of the Table<TEntity> class, it is clear
that it has a constraint which says the type parameter must be a class
(“where TEntity : class”). However, how does the compiler know
beforehand that I will be calling the “PrintMethod<T>()” method
passing on a non class (which I won’t!)?
Regards Carl Johansson
private static void PrintTable<T>(Table<T> table, string text)
{
Console.WriteLine(text);
foreach (T record in table)
Console.WriteLine(record);
}
The complier reports the following error:
“The type 'T' must be a reference type in order to use it as parameter
'TEntity' in the generic type or method
'System.Data.Linq.Table<TEntity>.”
Looking at the definition of the Table<TEntity> class, it is clear
that it has a constraint which says the type parameter must be a class
(“where TEntity : class”). However, how does the compiler know
beforehand that I will be calling the “PrintMethod<T>()” method
passing on a non class (which I won’t!)?
Regards Carl Johansson