Switch Case on type of object (C#)
        Posted  
        
            by Sem Dendoncker
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Sem Dendoncker
        
        
        
        Published on 2009-04-02T09:07:04Z
        Indexed on 
            2010/05/09
            10:08 UTC
        
        
        Read the original article
        Hit count: 189
        
If you want to switch a type of object, what is the best way to do this?
ex:
private int GetNodeType(NodeDTO node)
{
    switch (node.GetType())
    { 
        case typeof(CasusNodeDTO):
            return 1;
        case typeof(BucketNodeDTO):
            return 3;
        case typeof(BranchNodeDTO):
            return 0;
        case typeof(LeafNodeDTO):
            return 2;
        default:
            return -1;
    }
}
I know this doesn't work that way, but I was wondering how you could solve this. Is an if then else else else statement appropriate in this case? Or do you use this switch and add .ToString() to the types?
Kind regards, Sem
© Stack Overflow or respective owner