I am working with my own custom stacks class, and I am trying to create a generic method for creating a new stack(a generic object). I work a lot with stacks that are type int, and only occasionally need other types. Can I set the defult Type of T to int?
Here is the method:
public static Stack<T> newStack<T>(int length)
{
Stack<T> s = new Stack<T>();
for (int i = 0; i < length; i++)
{
Console.WriteLine("print num");
string input = Console.ReadLine();
T value = (T)Convert.ChangeType(input, typeof(T));
s.Push(value);
}
return s;
}
I want to use the method like this:
newStack(5);//defult int type
and for any other type
newStack<string>(5)
Can anybody help? Thanks.