I'm a beginner in C# so please be patient with me. :) I'm practicing to write a simple C# code to understand how this language works. I have tried to read characters, integers and strings from console but between different tries/methods the buffer never empties. I would like to clear the buffer of console between each methods, for example:
using System;
namespace ElsoKonzolAppom
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Write something!");
string text1 = Console.ReadLine();
Console.WriteLine("Your text:" text1);
//here, I would like to clear the buffer
int number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your first number:" number1);
//here, I would like to clear the buffer
int number2 = Console.Read();
Console.WriteLine("Your second number:" number2);
}
}
}
Probably this is something very trivial that I haven’t mastered yet.
CodePudding user response:
As far i understood you want to clear input buffer? There is no method in console class for it. Try this:
while(Console.KeyAvailable)
Console.ReadKey(false); // skips previous inputs
Console.ReadKey(); // reads a new char
Use Console.ReadKey(true)
if you don't want to print skipped chars.
If you want to clear the screen use:
Console.Clear()
Regards
CodePudding user response:
you can use Console.ReadKey(false); // true = hide input