Can i use System.Console as a parameter?

T

TKW

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

using System;
using System.Collections.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.Write, FileShare.None))
{
using (StreamWriter OutputBodyWriter = new
StreamWriter(OutputFile))
{
PrintLine(System.Console); // ******************How can
i use console as a parameter?

PrintLine(OutputBodyWriter);

OutputBodyWriter.Close();
}
OutputFile.Close();
}
} // end Main

public static void PrintLine(StreamWriter OutputDevice)
{
OutputDevice.WriteLine("Wellcome to test program");
}
}
} // end program



*** PrintLine(System.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
 
J

Jon Skeet [C# MVP]

TKW said:
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.)
 
D

David Paleino

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

...

public static void PrintLine(StreamWriter OutputDevice)
{
OutputDevice.WriteLine("Wellcome to test program");
}

Why don't you just do:

System.Console.WriteLine();

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top