如何更有效地验证?

时间:2016-08-27 17:08:50

标签: c# performance class if-statement clients

我不知道每个人都称之为什么,但我如何缩短这段代码呢?

我被教导不重复代码,在GetAddress()方法中,if语句我重复同一行两次,一次来自if语句,然后另一行来自else if语句

(_CClientFirstName == "John" || _CClientLastName == "Jenkins" || _CClientAge == 21)

如何使用更少的代码执行此操作?我只是想知道它是否可能,因为如果是的话,我很想知道。此外,请不要回复任何太复杂的事情,因为我只是刚开始,但如果你这样做,那么尽量解释它,谢谢。

class ClientInfo
{
    private string _CClientFirstName = "Default";
    private string _CClientLastName = "Default";
    private int _CClientAge = 99;

    public ClientInfo(string FullName, string LastName, int Age)
    {
        _CClientFirstName = FullName;
        _CClientLastName = LastName;
        _CClientAge = Age;
    }

    public string GetAddress()
    {
        if (_CClientFirstName == "John" || _CClientLastName == "Jenkins" || _CClientAge == 21)
        {
            return $"{_CClientFirstName}'s address is: 67 Smokey Lane, London, B78 9JN, United Kingdom";
        }
        else if (_CClientFirstName == "Matt" || _CClientLastName == "Benks" || _CClientAge == 25)
        {

        }
    }
}

0 个答案:

没有答案