IF Statement multiple conditions, same statement

前端 未结 10 2029
孤城傲影
孤城傲影 2021-02-02 12:23

Hey all, looking to reduce the code on my c# if statements as there are several repeating factors and was wondering if a trimmer solution is possible.

I currently have 2

10条回答
  •  被撕碎了的回忆
    2021-02-02 12:49

    Pretty old question but check this for a more clustered way of checking conditions:

    private bool IsColumn(string col, params string[] names) => names.Any(n => n == col);
    

    usage:

    private void CheckColumn()
    {
         if(!IsColumn(ColName, "Column A", "Column B", "Column C"))
        {
         //not A B C column
        }
    
    }
    

提交回复
热议问题