سؤال

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