The PrintStackContents method:
public static void PrintStackContents<T>(Stack<T> s)
{
while(s.Count != 0)
{
Console.WriteLine(s.Pop());
}
}
Is a
Generic
routine: you have defined it to work for any class by adding the
<T>
after the method name. Within the method, you can now use "
T
" and the compiler with substitute the class type when you call the method.
Effectively, what you could have done is write a separate method for each type:
public static void PrintStackContentsInt(Stack<int> s)
{
while(s.Count != 0)
{
Console.WriteLine(s.Pop());
}
}
public static void PrintStackContentsString(Stack<string> s)
{
while(s.Count != 0)
{
Console.WriteLine(s.Pop());
}
}
And so on: But by using the
Generic
syntax, you only have to code and maintain a single method, which will work the same for them all.
Anonymous types are more complex, and are probably best left until latter when you start to learn about Linq, but a brief description:
An anonymous type is a class with no name: you do not define it directly with a
class
statement, instead it's structure is inferred from the data from which it is created. They are frequently used to return the results of a Linq query.