-2

I am looking for the built-in equivalent of this function:

public static string ask(string prompt) {
    Console.Write(prompt);
    return Console.ReadLine();
}

I think it is tedious to copy-paste this in every console app, and creating a new class / module just for this tiny utility seems overkill.


I ask because the Google search: c# ask for input and print message in the same line of code does not give me any meaningful results.

Caridorc
  • 6,222
  • 2
  • 31
  • 46
  • 3
    It is tedious to write these two lines of code, so you would like them to be in a single line? Why not create your own class then? That's why people have "Core" assemblies where they place stuff which is reused between projects. As a side note, [method names in C# are PascalCased by convention](https://msdn.microsoft.com/en-us/library/vstudio/ms229043(v=vs.100).aspx). – vgru Nov 06 '15 at 16:36
  • 1
    It looks like you've already encapsulated these two lines of code into a single function call. If calling a function is "too tedious" then perhaps programming isn't really your calling... – David Nov 06 '15 at 16:41
  • @Groo That is my answer, not a class for just that function, but a class for many utility functions. Very convenient, post that as an answer and I will accept it. – Caridorc Nov 06 '15 at 16:45
  • @David :) I mean writing the function down in each module may be tedious, as importing a class just for it would be :) – Caridorc Nov 06 '15 at 16:45
  • control-c control-v ? – Joe Nov 06 '15 at 16:51
  • @Joe I do not think that copy-pasting is a good software engineering practice. – Caridorc Nov 06 '15 at 16:52
  • 1
    *"I am looking for the built-in equivalent of this function"* It doesn't exist. – Ron Beyer Nov 06 '15 at 16:57
  • @RonBeyer that should be an answer, being sure that it does not exist is as good as being sure that it exists. I just wanted to be sure that I was not reinventing the wheel. – Caridorc Nov 06 '15 at 16:59
  • @Caridorc it is already an answer. – mariocatch Nov 06 '15 at 17:00
  • @mariocatch in fact the first part of your answer is good, I downvoted bacause of the second part: _You'll have to get used to writing those two lines of code. It won't take much time, don't worry._ as I may want to use an utility class and not get used to it. – Caridorc Nov 06 '15 at 17:02
  • @Caridorc I'd say that mariocatch's answer, in a round-about way, says the same thing. Accept that one. – Ron Beyer Nov 06 '15 at 17:02
  • Edited my answer to state the fact about the BCL. – mariocatch Nov 06 '15 at 17:03
  • @David We are here to discuss **ideas** not attack each other, I do not like yours _If calling a function is "too tedious" then perhaps programming isn't really your calling_ also because I never said that calling a function was too tedious for me. – Caridorc Nov 06 '15 at 17:20

1 Answers1

3

There is no built-in shortcut for printing something and then asking for input in the same function.

mariocatch
  • 8,305
  • 8
  • 50
  • 71
  • @Groo 's idea about a single `Core` class containing all the utilities you want is real cool, much better than writing more code for the same result. – Caridorc Nov 06 '15 at 16:54
  • Yes writing classes to give you functionality is a core component of OOP. However, this type of requested functionality isn't something worth writing a class for in every application you'll write. It's something you typically write a method for if your application uses numerous times... it's not something I'd build into an assembly and reference in future projects. – mariocatch Nov 06 '15 at 16:57
  • Perfect correction that makes this answer coincise and to the point. You may briefly mention a `Core` class to group utility functions just for completeness though. Future visitors will be more happy to be given an alternative. – Caridorc Nov 06 '15 at 17:05