Dictionary with delegate or switch?

Posted by Samvel Siradeghyan on Stack Overflow See other posts from Stack Overflow or by Samvel Siradeghyan
Published on 2010-05-24T11:49:00Z Indexed on 2010/05/24 12:01 UTC
Read the original article Hit count: 417

Hi,
I am writing a parser, which call some functions dependent on some value.

I can implement this logic with simple switch like this

switch(some_val)   
{   
    case 0:   
        func0();   
        break;   
    case 1:   
        func1();   
        break;   
}     

or with delegates and dictionary like this

delegate void some_delegate();   
Dictionary<int, some_delegate> some_dictionary = new Dictionary<int, some_delegate>();   
some_dictionary[0] = func0;   
some_dictionary[1] = func1;   

some_dictionary[some_value].Invoke();    

Are this two methods equal and which is preferred?
Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about dictionary