Switch vs Dictionary logic I'm arguing here with a colleague over two implementations of how to handle a processing logic:

VERSION 1:

public class SomeManager {
    private readonly Dictionary&lt;Type, Action&lt;IData&gt;&gt; dataHandlers; 

    public SomeManager() {
        dataHandlers = new Dictionary&lt;Type, Action&lt;IData&gt;&gt; {
            {typeof(DataTypeOne), HandleDataTypeOne},
            {typeof(DataTypeTwo), HandleDataTypeTwo},
            {typeof(DataTypeThree), HandleDataTypeThird}
            //... and so on 
        };
    }

    //... could be other code here          

    public void Process(IData data) {
        var dataType = data.GetType();

        if (!dataHandlers.ContainsKey(dataType )) {
            Log.Error(string.Format("Unrecognized data type: {0}", dataType ));
            return;
        }

        dataHandlers[dataType ](data);
    }

    private void HandleDataTypeOne(IDispatchData data) {
        //...specific handling here
    }

    private void HandleDataTypeTwo(IData data) {
        //...specific handling here
    }

    private void HandleDataTypeThree(IData data) {
        //...specific handling here
    }
} 


VERSION 2:

public class SomeManager {
    public SomeManager() {
    }

    //... could be other code here          

    public void Process(IData data) {
        if (data is DataTypeOne) {
            HandleDataTypeOne(data);
        } else if (data is DataTypeTwo) {
            HandleDataTypeTwo(data);                
        } else if (data is DataTypeThree) {
            HandleDataTypeThree(data);      
        //other cases here           
        } else {
           Log.Error(string.Format("Unrecognized data type: {0}", data.GetType()));
        }
    }

    private void HandleDataTypeOne(IDispatchData data) {
        //...specific handling here
    }

    private void HandleDataTypeTwo(IData data) {
        //...specific handling here
    }

    private void HandleDataTypeThree(IData data) {
        //...specific handling here
    }
} 


In our case, performance is not an issue. 

We are talking here about a specific scenario of 5 data types.
Both approaches are acceptable, but the one using a Dictionary feels cleaner to me, and brings some advantages at no significant cost.


A Dictionary will detect a duplicate key, whereas it could be easily overlooked in an if-else construct, leading to a possibly baffling bug
It could be modified in run-time, eg. for debugging purposes.
It's more flexible: it could be injected from outside, easily refactored towards a DI-based approach etc.


This being said, it could be overengineering for simple cases. It's hard to say without knowing broader context. All info you've provided is that there's 5 cases, which is quite a few already, and it makes me guess there's a chance for more to come; if all I knew was that there's 2 or 3, I would say keep it simple.