CTE Solutions
 Search
Thursday, March 11, 2010 ..:: Community Forum ::.. Register  Login
 Community Forum Minimize
     
  .NET Development  MCTS 70-536 Study Group  Returning two values from method  

 Returning two values from method 
 
Khayralla
12 posts
Returning two values from method
Posted: 28 May 09 9:55 AM (United States)
 

http://dotnetperls.com/Content/KeyValuePair.aspx

Returning two values from method
Often, you need to return two separate values from a method. You can do this very easily with KeyValuePair. You must specify the exact type in the return value, and then return the new KeyValuePair in the method body. This is clearer than a two-element array. Consider out or ref parameters instead.

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
Console.WriteLine(GetNames());
// [William, Gates]
}

static KeyValuePair GetNames()
{
//
// Gets collection of first and last name.
//
string firstName = "William";
string lastName = "Gates";
return new KeyValuePair(firstName, lastName);
}
}
phenry
65 posts
Re: Returning two values from method
Posted: 28 May 09 10:45 AM (Canada)
 
To be honest, at the risk of adding more code/more classes, I would sooner create another specialized object to return the values I want. Chances are, if I'm returning two values, there's a good chance that "object" will be used elsewhere.

The advantage of this solution is, how often does "two" stay two? If you use KeyValuePair, you're tied to two, but if next week you need to add another value, you just add it to your custom class.

But then again, when you're under the gun and the deadline is in hours, and you have to get the code done, KeyValuePair is indeed a fast way (just remember to put a ///todo refactor when time allows comment.
Khayralla
12 posts
Re: Returning two values from method
Posted: 29 May 09 10:15 AM (United States)
 
Absolutely I agree with you, but sometimes still we need it.
 
   .NET Development  MCTS 70-536 Study Group  Returning two values from method
 
 
   

Search   

Copyright 2008 Ottawa .NET Community   Terms Of Use  Privacy Statement
Portal engine source code is copyright 2002-2010 by DotNetNuke. All Rights Reserved