.Net 4.0 Optimized code for refactoring existing “if” conditions and “is” operator

后端 未结 7 771
遥遥无期
遥遥无期 2021-01-06 18:02

I have following C# code. It works fine; but the GetDestination() method is cluttered with multiple if conditions by using is operator.

In

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 18:40

    Role should have a virtual function that would return destination:

    public virtual string GetDestination()
    {
         return "Home";
    }
    

    And all the classes should override this function and return the correct string. Then in the code you would have:

    var role = new Accountant();
    string destination = role.GetDestination();
    

    I hope that helps. There may be typos, I am writing from head.

提交回复
热议问题