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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -