I have a simple value assignment, that checks if the new value is empty or not:

string newVal;
string val = (newVal = Console.ReadLine()) != "" ? newVal : "Default";

Is there a shorter way for achieving the same result?

有帮助吗?

解决方案

string val = Console.ReadLine();
if (val == "") val = "Default";

Not only shorter, but also simpler and easier to comprehend. If you can stand a bit more "clutter", it's probably better style to put the conditional block on its own line:

string val = Console.ReadLine();
if (val == "")
{
    val = "Default";
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top