4

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.

1
  • There's a few old questions that say that you can't, but I don't know if things have changed. The suggestion there is to make another method without the generic parameter that just calls the int verion. Commented Feb 10, 2020 at 10:15

5 Answers 5

7

You can't specify a default type for T however you can simply overload the newStack method to get the behaviour you want. The overloaded method will automatically create a new stack of type int.

Your code would look something like this:

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;
}

public static Stack<int> newStack(int length)
{
    // Call our original new stack method but pass
    // integer as the type T
    return newStack<int>(length);
}

Now you can call the second method if you just want to create a new stack of type integer like this:

newStack(5);

Otherwise you can create stacks of other types with:

newStack<string>(5);
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer. You answered first so I will accept your answer
1

Add a method newStack without the generics:

 public static Stack<int> newStack(int length) => newStack<int>(length);

The compiler is smart enough to select the correct method.

Comments

1

You could either bear with the generic formalism and use the int type when you need it:

Stack<int> myStack = newStack<int>(5);

but if you really want to, you can define another method without the type parameter that calls the generic version:

public static Stack<int> newStack(int length)
{
    return newStack<int>(length);
}
Stack<int> myStack = newStack(5);

Comments

1

Short answer, you can't, but you could use a, non-generic overload:

public static Stack<int> newStack(int length)
{
    return newStack<int>(length);
}

Comments

0

I think you can use overloading.

public static Stack<int> newStack(int length)
{
     return newStack<int>(length);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.