How to set default input value in .Net Console App?

前端 未结 8 1383
时光取名叫无心
时光取名叫无心 2020-12-11 16:01

How can you set a default input value in a .net console app?

Here is some make-believe code:

Console.Write("Enter weekly cost: ");
string inp         


        
相关标签:
8条回答
  • 2020-12-11 16:44

    You can use helper method like this:

    public static string ReadWithDefaults(string defaultValue)
    {
        string str = Console.ReadLine();
        return String.IsNullOrEmpty(str) ? defaultValue : str;
    }
    
    0 讨论(0)
  • 2020-12-11 16:46

    There's a much better way to do this now, check out Readline on nuget: https://www.nuget.org/packages/ReadLine

    1. install-package Readline
    2. var input = ReadLine.Read("Enter weekly cost: ", "135");

    I like to use the console to write interactive tests, and having default values can really help things.

    0 讨论(0)
提交回复
热议问题