Directly you cannot do this with the infrastructure . NET provides. Either you have to access the console directly from the API of the operating system, which is quite complicated, or you have to resort to tricks (for example parse what was typed, but one has to type right and a lot of things can go wrong, don’t do this).
Better yet, you can do the right thing and not put this requirement in your software, it is bad from the point of view of UX, the right thing is to ask for the data separately, including asking for the text one at a time, this way is asking for something to go wrong. And it looks like you’re not worried about mistakes because this conversion will potentially break the application, that’s not how it’s done. Something like this is more right:
using static System.Console;
public class Program {
public static void Main() {
Write("Informe o comprimento em metros: ");
if (!double.TryParse(ReadLine(), out var comprimento)) return;
Write("Informe a largura em metros: ");
if (!double.TryParse(ReadLine(), out var largura)) return;
WriteLine($"A area é de {largura * comprimento} m²");
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
I had it closed if the data isn’t valid, but it could give an error message, ask again, or do whatever you think is best. If you go the way of trying to interpret two data in a row only a lot can go wrong and your code would be too complex to handle all cases, then make it simple, including because it is more user friendly.
This would help you? C# - Using Argument Array or this question: How to read three values in the same line? C#
– Luiz Augusto
you can enter all the values separated by comma and then do the
Split
. has several examples here on the site, such as: https://answall.com/q/38521/57220– Ricardo Pontual
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero