Transfer of Console as parameter C#



  • (The title of the topic is incorrect, for which I apologize, I didn't know what else to call)

    There's a function. Read()as a parameter, it accepts a class object StreamReader♪ But I sometimes need to read the data from the console and I'd like to transfer the consol flow as a parameter to read the information.

    class A
    {
        public void Read(StreamReader str)
        {
            //тут код чтения в стиле
            string s = str.ReadLine();
            ...
        }
        ...
    }
    class Proga
    {
        public static void Main()
        {
            A a = new A();
            a.Read(/*тут поток, который передаю*/);
        }
    }
    


  • Try and replace StreamReader more general TextReader:

    class A
    {
        public void Read(TextReader str)
        {
            //тут код чтения в стиле
            string s = str.ReadLine();
            ...
        }
        ...
    }
    

    Then you can use it. https://msdn.microsoft.com/en-us/library/system.console.in(v=vs.110).aspx :

    class Proga
    {
        public static void Main()
        {
            A a = new A();
            a.Read(Console.In);
        }
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2