Вопрос

if (msg.Body.Contains("logging"))
{
    enableLogs = !enableLogs;
    sendMsg(enableLogs == true ? "Logging enabled." : "Logging disabled.");
}
else if (msg.Body.Contains("afk"))
{

}

How would I go about turning this into a ternary if statement? It's not necessary if it won't work out nice, trying to make my program cleaner.

Это было полезно?

Решение

Ternary statements are strictly for assignments, not for else-if or multi-decision logic. It would only work if both logic paths returned an instance of a type. :)

var result = booleanValue ? logicPathForTrue : logicPathForFalse;

A "good" (academic) use of ternary:

// Assign something to result. If something is null, assign new Something to result
Something result = something == null ? new Something() : something;

Ternary operations require variable assignment. This compiles:

var something = true ? new object() : null;

This does not compile:

true ? new object() : null;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top