C
C#2y ago
yoplaya

❔ Cleanest way to initialize string outside of conditional statement where it's assigned?

string str = "" if(something) { str = "a" } else { str = "b" } Is this the cleanest way? or is there a better way to expose the str outside of conditional scope without this dirty empty string initialization
5 Replies
Aaron
Aaron2y ago
you can just do this
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
string s;
if (true)
{
s = "a";
}
else
{
s = "b";
}
s
string s;
if (true)
{
s = "a";
}
else
{
s = "b";
}
s
Result: string
a
a
Compile: 370.540ms | Execution: 34.710ms | React with ❌ to remove this embed.
Playboi17
Playboi172y ago
var str = condition ? “This value if true” : “This value if false”;
Aaron
Aaron2y ago
a ternary works too in this case, I assumed you had more stuff going on in the if
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.