c# - Create a Generic method that will write to a file or write to console output -


i have 2 functions identical in body, 1 writes file , 1 writes console.
there way make file/console generic pass function?

public void watchwindow() {      var max = _collengths.max();     (var rowindex = 0; rowindex < max; ++rowindex)     {         (var colindex = 0; colindex < windowwidth; ++colindex)         {             if (rowindex < _collengths[colindex])                 console.write("{0} ", _actualwindow[colindex]rowindex].symbolname);             else                 console.write("   ");          }          console.writeline();      }      console.writeline(); }  public void printwindow(streamwriter file) {      var max = _collengths.max();     (var rowindex = 0; rowindex < max; ++rowindex)     {         (var colindex = 0; colindex < windowwidth; ++colindex)         {              if (rowindex < _collengths[colindex])                  file.write("{0} ", _actualwindow[colindex][rowindex].symbolname);              else                  file.write("   ");          }          file.writeline();      }      file.writeline(); } 

a streamwriter textwriter. write* static methods of console write console.out. console.out textwriter. common "thing" writing on textwriter.

public void writetofile(streamwriter file) {     printwindow(file); }  public void writetoconsole() {     printwindow(console.out); }  public void printwindow(textwriter writer) {      var max = _collengths.max();     (var rowindex = 0; rowindex < max; ++rowindex)     {         (var colindex = 0; colindex < windowwidth; ++colindex)         {             if (rowindex < _collengths[colindex])                 writer.write("{0} ", _actualwindow[colindex][rowindex].symbolname);             else                 writer.write("   ");         }         writer.writeline();     }     writer.writeline(); } 

you can use writetofile/writetoconsole methods (or directly printwindow method, rename more apt, writetotextwriter)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -