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 typeSystem.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)
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.
Comments