Thursday, February 17, 2011

Is it OK to return a KeyValuePair from a .NET function?

I was just working on a function that I needed to return two values, one being a column name and one being the value of that column for that current row. I am returning KeyValuePair(of String,Object). I was wondering if this is a good idea or does it make it hard to read/use?

From stackoverflow
  • If it genuinely is a key-value pair, then that seems a pretty reasonable thing to do. .NET 4.0 will include a proper Tuple class for cases where there isn't a key-value relationship.

    The alternative is to use out/ref parameters, letting the caller decide whether or not to keep the values together - but I prefer the KeyValuePair approach when there's an obvious relationship and the caller is likely to want to keep them combined.

    Wolfbyte : If there is no KVP relationship then just create your own simple class encapsulating what you are returning. In the sample provided KeyValuePair seems OK.

0 comments:

Post a Comment