简体   繁体   中英

Alternate for if-else

Below is my code:

int a = 4;
if (a > 3)
{
    Label1.Text = "Hello";
}
else
{
    Label1.Text = "Hi!";
}
//Alternate for if-else
Label1.Text = (a > 3) ? "Hello" : "Hi!";

As we know, both if-else and alternate for if-else gives out same output. I was wondering, what if i have more than one statement. For Instance

int a = 4;
if (a > 3)
{
    Label1.Text = "Hello";
    Label2.Text = "How are you?";
}
else
{
    Label1.Text = "Hi";
    Label2.Text = "How do you do"";
}

So, is there an alternate for this? I am sure there is something C# has to offer which I can use. Thanks in Advance.

PS I am fairly new to C#

That is probably the best solution. It is very easy to read and see exactly what you're doing. Using the ternary operator even for one statement can sometimes be a bit much to read depending on your code.

There are a huge number of alternatives here. You could say that many OOP design patterns are themselves just alternatives to if statements, but for a simple case like this it is unnecessary to go that far.

The way you have already implemented this is already fine.

Perhaps something like this may help. It abstracts the implementation code away from the if , assuming that is what you would like:

int a = 4;
if (a > 3)
{
    SetLabelToHello();
}
else
{
    SetLabelToHi();
}

// ....

public void SetLabelToHello()
{
    Label1.Text = "Hello";
    Label2.Text = "How are you?";
}

public void SetLabelToHi()
{
    Label1.Text = "Hi";
    Label2.Text = "How do you do";
}

Using methods in this way allows you to easily add more statements to the if condition whilst keeping clean, easily maintainable code.

Move assignment to a function

public int SetLabels(string label1text, string label2text)
{
   try
   {
        Label1.Text = label1text;
        Label2.Text = label2text;

        return 0;
   }
   catch
   {
        return 1;
   }
}


var result = a > 3 ? SetLabels("Hello", "How are you?") : SetLabels("Hi!", "How do you do");

C# is an imperative language, so this is the way it should be written. There is no built in approach to have the double-assignment, because C# does not support multiple return values.

Just keep it like this. It's good.

Tim Schmelter gave an answer before, but there come some DRY principals and down-voted hard.

His answer was ok:

Label1.Text = a > 3 ? "Hello" : "Hi";
Label2.Text = a > 3 ? "How are you?" : "How do you do?";

If you are too much about DRY, then do it like this

bool veryDryCondition = a > 3;
Label1.Text = veryDryCondition ? "Hello" : "Hi";
Label2.Text = veryDryCondition ? "How are you?" : "How do you do?";

Though using simple if is still preferable way in this case.

I am using ?: mostly for better readability, if conditions are unique and I want to keep one-liners (code what stay in one line) for better readability. If conditions are common and there are more than one actions - using if is usually better.

Q: "So, is there an alternate for this?"

A: Yes, there is infinite quantity of alternatives. BUT it dosn't mean that you have to use them... Everything depends on your context.

Here is another weird, possible solution. It works, but does it make sense?

Action action;
int a = 4;

if (a > 3)
{
    action = new Action(() => {
        Label1.Text = "Hello";
        Label2.Text = "How are you?";
    });

}
else
{
    action = new Action(() => {
        Label1.Text = "Hi";
        Label2.Text = "How do you do"";
    });
}

action();

The answer to your question is no, there isn't an alternative / shorter way of writing this. Although there are other ways to do it.

Which then leads on to why do you want to make the code more concise? it is better to strive for readability and better maintenance.

int a = 4;
var aIsGreaterThanThree = (a > 3)
if (aIsGreaterThanThree)
{
   SetLabelsToHello();
}
else 
{
   SetLabelsToHi();
}

void SetLablesToHello()
{  
   Label1.Text = "Hello";
   Label2.Text = "How are you?";
}

void SetLabelsToHi() 
{
   Label1.Text = "Hi";
   Label2.Text = "How do you do";
}

At that point depending on perference you could change the if statement back to the shorthand version or drop the { and } from the if statements.

You want one conditional operator for both labels? That's not possible, but what's wrong with:

Label1.Text = a > 3 ? "Hello" : "Hi!";
Label2.Text = a > 3 ? "How are you?" : "How do you do";

If both labels are not related and the condition might change in future, so that the first label has a different one than the other, then this approach is better. Otherwise stick with the if-else since it doesn't repeat the same condition multiple times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM