我遇到了一个问题,通过一个简单的克隆方法来扩展泛型Stack类。这是我的方法:如何扩展在C#中返回泛型值的泛型类?
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;
}
}
我得到以下错误:
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?)
我一直在使用Google的溶液或explenation,但已经结束了,没有运气。 (这就是我:试过 https://www.google.pl/#q=generic+extension+method+c%23)
变化'公共静态类StackMixin''到公共静态类StackMixin' –
我没有打对... 谢谢! – pizycki
@IlyaBursov - 你应该把你的评论作为答案。 – Enigmativity