J
Jeff Johnson
I've never written a generic method, and I've never looked into them too
closely. Now I'm reading a C# book and a generic method showed up in a code
sample which got me to thinking about them. Here's the method:
private static void Print<T>(IEnumerable<T> items)
{
foreach (T item in items)
{
Console.WriteLine(item);
}
}
Pretty simple stuff. Now, here's my question: in order to take a generic
parameter, does the method itself HAVE to be declared generic? In other
words, couldn't it just be
private static void Print(IEnumerable<T> items)
?
If the <T> is required after the method name, why? Is it just a case of
"because that's what the C# designers decided" or is there a deeper reason?
closely. Now I'm reading a C# book and a generic method showed up in a code
sample which got me to thinking about them. Here's the method:
private static void Print<T>(IEnumerable<T> items)
{
foreach (T item in items)
{
Console.WriteLine(item);
}
}
Pretty simple stuff. Now, here's my question: in order to take a generic
parameter, does the method itself HAVE to be declared generic? In other
words, couldn't it just be
private static void Print(IEnumerable<T> items)
?
If the <T> is required after the method name, why? Is it just a case of
"because that's what the C# designers decided" or is there a deeper reason?