'Menu customization questions in C#
I am trying to create a menu in C# Console app and I am trying to make it like this:
[1] Login
[2] Register
[3] Exit
But as you can see, I am manually inserting the number inside of the bracket, how would I make it so everytime I do
Console.WriteLine(“test”)
It would do it on its own like this:
[1] test
This has been frustrating me for a little bit.
Solution 1:[1]
You can write your own method, which prints this indexes automatically.
public static class CustomConsole
{
public static int Index {get;private set;} = 1;
public static void WriteLine(String message)
{
Console.WriteLine($"[{Index++}] {message}");
}
}
Usage:
CustomConsole.WriteLine("Login");
CustomConsole.WriteLine("Register");
CustomConsole.WriteLine("Exit");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
