How to extend generic class returning generic value in C#?

pizycki

I've having a problem extending the generic Stack class with a simple Clone method. This my approach:

class Program
{
    static void Main(string[] args)
    {
        Stack<string> myStack = new Stack<string>();
        myStack.Push("hello!");

        var stackClone = myStack.Clone();

    }
}

public static class StackMixin<T>
{
    public static Stack<T> Clone<T>(this Stack<T> stackToClone)
    {
        if (stackToClone == null)
            throw new ArgumentNullException();

        IEnumerable<T> reversedStackToClone = stackToClone.Reverse<T>();
        Stack<T> cloneStack = new Stack<T>(reversedStackToClone);
        return cloneStack;
    }
}

I get the following error:

System.Collections.Generic.Stack<string> does not contain a definition for 'Clone' and no extension method 'Clone' accepting a first argument of type System.Collections.Generic.Stack<string> could be found (are you missing a using directive or an assembly reference?)

I've been googling for the solution or the explenation, but has ended up with no luck. (This is what I've: tried https://www.google.pl/#q=generic+extension+method+c%23)

Iłya Bursov

As per comments: change public static class StackMixin<T> to public static class StackMixin

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to extend generic class to be also generic

From Dev

C# Extend Generic Class in Method call

From Dev

C# Extend Generic Class in Method call

From Dev

Extend a generic class in swift

From Dev

How to extend from AsyncTask as a generic base class?

From Dev

extend Comparable generic in a final class

From Dev

Modifying value type in generic class implementing generic interface C#

From Dev

How to extend Generic abstract Class with <E extends Comparable<E>>

From Dev

Returning an Array from a generic class

From Dev

How to extend a Generic Collection in Delphi?

From Dev

Null value for generic class

From Dev

Extending abstract method with generic parameter (that must extend generic class) in Java

From Dev

Is it possible to extend a generic class while restricting the generic type?

From Dev

Is it possible to extend a generic class using extension methods?

From Dev

c# - How to change the generic specific class

From Dev

How to return class type of a generic class with generic?

From Dev

How to extend a class that is returning this in their methods

From Dev

How to return `this` in the generic class?

From Dev

How to specify a generic class?

From Dev

C++: Non generic method in generic class?

From Dev

Generic Method with a Generic Class in c#

From Dev

How can I extend a non generic interface into a generic one?

From Dev

How do I correctly restrict a generic interface to extend Number class in Java and be able to run it in another class?

From Dev

Returning a generic object on Java from abstract class

From Dev

Generic ObjectPool - how to return a generic class?

From Dev

How to extend type that accepts generic elements of array?

From Java

React and Typescript: How to extend generic component props?

From Dev

How to correctly extend and implement Java generic interfaces

From Dev

How to extend type that accepts generic elements of array?

Related Related

  1. 1

    How to extend generic class to be also generic

  2. 2

    C# Extend Generic Class in Method call

  3. 3

    C# Extend Generic Class in Method call

  4. 4

    Extend a generic class in swift

  5. 5

    How to extend from AsyncTask as a generic base class?

  6. 6

    extend Comparable generic in a final class

  7. 7

    Modifying value type in generic class implementing generic interface C#

  8. 8

    How to extend Generic abstract Class with <E extends Comparable<E>>

  9. 9

    Returning an Array from a generic class

  10. 10

    How to extend a Generic Collection in Delphi?

  11. 11

    Null value for generic class

  12. 12

    Extending abstract method with generic parameter (that must extend generic class) in Java

  13. 13

    Is it possible to extend a generic class while restricting the generic type?

  14. 14

    Is it possible to extend a generic class using extension methods?

  15. 15

    c# - How to change the generic specific class

  16. 16

    How to return class type of a generic class with generic?

  17. 17

    How to extend a class that is returning this in their methods

  18. 18

    How to return `this` in the generic class?

  19. 19

    How to specify a generic class?

  20. 20

    C++: Non generic method in generic class?

  21. 21

    Generic Method with a Generic Class in c#

  22. 22

    How can I extend a non generic interface into a generic one?

  23. 23

    How do I correctly restrict a generic interface to extend Number class in Java and be able to run it in another class?

  24. 24

    Returning a generic object on Java from abstract class

  25. 25

    Generic ObjectPool - how to return a generic class?

  26. 26

    How to extend type that accepts generic elements of array?

  27. 27

    React and Typescript: How to extend generic component props?

  28. 28

    How to correctly extend and implement Java generic interfaces

  29. 29

    How to extend type that accepts generic elements of array?

HotTag

Archive