c# - Writing a generic method and the compiler expects T to be a known type -
this question has answer here:
i writing generic extension method. compiler not accept generic parameter. compiler message
the type or namespace name 't' not found (are missing using directive or assembly reference?
can point out should differently? here code
public static class ienumerableextensionmethods { public static stack<t> tostack(this ienumerable<t> source) { var reversed = source.reverse(); if(source icollection<t>) return new stack<t>(reversed); var returnstack = new stack<t>(reversed.count); foreach (t item in reversed) returnstack.push(item); return returnstack; } }
your method signature needs type parameter:
public static stack<t> tostack<t>(this ienumerable<t> source)
as sidenote, won't compile once fix that. need call count()
method on ienumerable
:
var returnstack = new stack<t>(reversed.count());
also @servy points out in comments, check whether icollection<t>
isn't necessary; there's stack<t>
constructor takes ienumerable<t>
. use:
new stack<t>(source.reverse());
instead.
Comments
Post a Comment