How can I implement the following C # code? Sample C ++ Code:

// перегруженный оператор ввода, для ввода значений массива с клавиатуры istream &operator>> (istream & input, Array &obj) { for (int ix = 0; ix < obj.size; ix++) input >> obj.ptr[ix]; // заполняем массив объекта obj return input; // позволяет множественный ввод, типа cin >> x >> y >> z >> ... } 
  • And what is your difficulty? - Alexey Shimansky
  • I just wanted to enter the values ​​of the array from the keyboard right away I found a method in C ++ that I could not do in C # - user250081
  • one
    Why not “find and not think,” but “read and do”? The syntax for overloading a binary operator is such a public static возвращаемый_тип operator op(тип_параметра1 операнд1, тип_параметра2 операнд2) { // операции } ..... all - Alexey Shimansky
  • I did. But a little krevoi. For example, with ++ to overload the input operator squeezes >>, but in C # I don’t know what to write instead. ReadLine or what? - user250081
  • one
    Right - no way, in C # there is no istream . And idiomatic input does not occur with the << operator, so you cannot enter an arbitrary type with << . Tell us better what problem you are solving. - VladD

1 answer 1

IMHO, the closest thing you can think of is to make an extension method like this:

 public static TextReader Input(this TextReader input, string[] arr) { for (int i = 0; i < arr.Length; i++) arr[i] = input.ReadLine(); return input; } 

Now you can write like this:

 var arr1 = new string[3]; var arr2 = new string[2]; using (var reader = new StreamReader("test.txt")) { // Множественный ввод reader.Input(arr1).Input(arr2); } // Ввод из консоли Console.In.Input(arr1); 

If desired, you can make Split input lines by a space, etc.