Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?

前端 未结 13 1891
我在风中等你
我在风中等你 2020-12-02 04:55

With the addition of the Tuple class in .net 4, I have been trying to decide if using them in my design is a bad choice or not. The way I see it, a Tuple can be a shortcut

13条回答
  •  甜味超标
    2020-12-02 05:26

    Similar to keyword var, it is intended as a convenience - but is as easily abused.

    In my most humble opinion, do not expose Tuple as a return class. Use it privately, if a service or component's data structure requires it, but return well-formed well-known classes from public methods.

    // one possible use of tuple within a private context. would never
    // return an opaque non-descript instance as a result, but useful
    // when scope is known [ie private] and implementation intimacy is
    // expected
    public class WorkflowHost
    {
        // a map of uri's to a workflow service definition 
        // and workflow service instance. By convention, first
        // element of tuple is definition, second element is
        // instance
        private Dictionary> _map = 
            new Dictionary> ();
    }
    

提交回复
热议问题