.nettupleskeyvaluepair

What is the difference between Tuple and keyvaluepair


I am not understanding the difference between tuple and keyvaluepair. i was looking into this article http://www.dotnetperls.com/tuple-keyvaluepair. it seems like tuple is better than keyvaluepair but some of the developers saying strictly do not use tuple i wonder why?


Solution

  • I think those programmers are saying not to use Tuples because on a public API, they are less effective.

    Lets say you have a tuple like

    Tuple<int,string,string,int>
    

    then how will you identify that what are these values later or what they are representing. I think thats why programmers say that. Like in this case the consumer has to either guess or look up documentation to know what you mean.

    The MSDN says:

    A tuple is a data structure that has a specific number and sequence of elements.

    Tuples are commonly used in four ways:

    • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
    • To provide easy access to, and manipulation of, a data set.
    • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).
    • To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple object as the method argument, you can supply the thread’s startup routine with three items of data.