Can i use System.Console as a parameter?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TKW

    #1

    Can i use System.Console as a parameter?

    I want to write a function that will print a sentense to FILE or CONSOLE
    look like below

    using System;
    using System.Collecti ons.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace Try
    {
    class Program
    {
    static void Main(string[] args)
    {
    using (FileStream OutputFile = new FileStream("ABC .txt",
    FileMode.Create , FileAccess.Writ e, FileShare.None) )
    {
    using (StreamWriter OutputBodyWrite r = new
    StreamWriter(Ou tputFile))
    {
    PrintLine(Syste m.Console); // *************** ***How can
    i use console as a parameter?

    PrintLine(Outpu tBodyWriter);

    OutputBodyWrite r.Close();
    }
    OutputFile.Clos e();
    }
    } // end Main

    public static void PrintLine(Strea mWriter OutputDevice)
    {
    OutputDevice.Wr iteLine("Wellco me to test program");
    }
    }
    } // end program



    *** PrintLine(Syste m.Console); // *************** ***How can i use console as
    a parameter?

    Is it possible? I am newbie of C#
    If it is possible, but the above program goes to wrong way, let me know too
    Thanks


  • David Paleino

    #2
    Re: Can i use System.Console as a parameter?

    On Tue, 5 Aug 2008 17:29:31 +0800, TKW wrote:
    PrintLine(Syste m.Console); // *************** ***How can
    i use console as a parameter?
    >
    ...
    >
    public static void PrintLine(Strea mWriter OutputDevice)
    {
    OutputDevice.Wr iteLine("Wellco me to test program");
    }
    Why don't you just do:

    System.Console. WriteLine();

    ?

    --
    . ''`. Debian maintainer | http://wiki.debian.org/DavidPaleino
    : :' : Linuxer #334216 --|-- http://www.hanskalabs.net/
    `. `'` GPG: 1392B174 ----|---- http://snipr.com/qa_page
    `- 2BAB C625 4E66 E7B8 450A C3E1 E6AA 9017 1392 B174

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Can i use System.Console as a parameter?

      TKW <macneed@yahoo. com.hkwrote:
      I want to write a function that will print a sentense to FILE or CONSOLE
      look like below
      Pass Console.Out, which is a TextWriter that writes to the console, and
      change the parameter type of PrintLine from StreamWriter to TextWriter.
      (StreamWriter derives from TextWriter, so you'll still be compatible.)

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      Working...