简体   繁体   English

如何简化IF ELSE语句C#?

[英]How to simplify the IF ELSE statement C#?

if(currency.equal("CND"))
       if (string.IsNullOrEmpty(member.LastName))
                    {
                        return $"{member.FirstName}".Trim();
                    }
                    else
                    {
                        return $"{member.LastName} {member.FirstName}".Trim();
                    }

                else
                    if (string.IsNullOrEmpty(member.LastName))
                    {
                        return $"{member.FirstName}".Trim();
                    }
                    else
                    {
                        return $"{member.FirstName} {member.LastName}".Trim();
                    }

I need to simplify this statement shorter but im not sure how? 我需要简化此语句,但我不确定如何? Im newbie in this stuff i might need some help on it.. any suggestion? 我是新手,我可能需要一些帮助。

This does not necessarily simplifies it, it simply makes it a one-liner 这并不一定要简化它,而只是使其成为单线

return $"{(!member.LastName.IsNullOrEmpty() ? member.LastName : "")}{member.FirstName}".Trim();

However, for better clarity and readibility the if/else block is perfectly fine... 但是,为了更好的清晰度和可读性, if/else块非常好...

        if (string.IsNullOrEmpty(member.LastName))
        {
            return $"{member.FirstName}".Trim();
        }
        else
        {
            return $"{member.LastName} {member.FirstName}".Trim();
        }

I would definitely prefer the if...else block to using a one-line string interpolation 我绝对更喜欢if...else块而if...else使用单行字符串插值

You can move some of your code around to cut out the duplication. 您可以移动一些代码以减少重复。 If the last name is missing, the first name is the only one to print, so test for that first. 如果缺少姓氏,则只有名字可以打印,因此请先测试该名字。 Then test for the condition that prints out the full name in the preferred order. 然后测试以首选顺序打印出全名的条件。

if (string.IsNullOrEmpty(member.LastName))
{
    return member.FirstName.Trim();
}
else
{
    return currency.equal("CND")
        ? $"{member.LastName} {member.FirstName}".Trim()
        : $"{member.FirstName} {member.LastName}".Trim();
}

You could rewrite it as a nested ternary operation too, though it's no shorter really and whether it's more readable depends on the person reading it... 您也可以将其重写为嵌套的三元操作,尽管它实际上并不更短,并且是否可读性取决于阅读它的人...

return (string.IsNullOrEmpty(member.LastName)
        ? member.FirstName
        : currency.equal("CND")
          ? $"{member.LastName} {member.FirstName}"
          : $"{member.FirstName} {member.LastName}").Trim();

This is definitely over-engineered. 这肯定是过度设计的。 Not shorter, but intent is clearer and it is easily extensible. 不短,但意图更清晰,并且易于扩展。

public static string GetDisplayName(Member member, string currency)
{
    return string.Join(" ", GetDisplayNameParts(member, currency));
}

public static IEnumerable<string> GetDisplayNameParts(Member member, string currency)
{
    switch (currency)
    {
        case "CND":
            yield return member.LastName ?? ""
            yield return member.FirstName ?? ""
            yield break;

        default:
            yield return member.FirstName ?? ""
            yield return member.LastName ?? ""
            yield break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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