Alan2
Alan2

Reputation: 24552

How can I return more than one value from a method in C#

I have the following method:

    public static ?? GetType6()
    {
        var name = "x";
        var age = 1;
        return ??
    }

Called like this:

    var ?? = GetType6();

I would like to be able to call that method and get back the name and age. I thought of creating an anonymous object but then if I do that how can I decode it?

Upvotes: 1

Views: 1015

Answers (6)

Kabbalah
Kabbalah

Reputation: 501

You could use anonymous types as you said. Like this.

public static object GetType6()
{
    return new { name = "x", age = 1 };
}

To read the values you have to use reflection.

var foo = GetType6();
var name = foo.GetType().GetProperty("name").GetValue(foo, null);
var age = foo.GetType().GetProperty("age").GetValue(foo, null);

Though it's a very dirty way of accomplishing what you need.

Upvotes: 0

unlimit
unlimit

Reputation: 3752

Use dynamic as return type:

public static dynamic GetType6()
{
    var name = "x";
    var age = 1;
    return new { name = "x", age = 1 };
}

Method call:

var v =  GetType6();

SO question: Variable Return Type of a Method in C#
MSDN: http://msdn.microsoft.com/en-us/library/vstudio/dd264741.aspx

Upvotes: 0

Bhushan Firake
Bhushan Firake

Reputation: 9448

using out keyword , it is possible as below:

public static string GetType6(out int age)
{
   var name = "x";
   var age = 1;
   return name
}
  • The out keyword causes arguments to be passed by reference.
  • This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed.
  • To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.

Note : It is bad practice to use this.

Upvotes: 0

Adrien.C
Adrien.C

Reputation: 209

You can use tuple :

public Tuple<int, int> GetMultipleValue()
{
    return new Tuple<int,int>(1,2);
}

You can have more details here : http://msdn.microsoft.com/en-us/library/system.tuple.aspx

Upvotes: 1

DGibbs
DGibbs

Reputation: 14608

Why don't you just create a type to hold whatever you want?

public static MyType GetType6()
{
   MyType type = new MyType();
   type.name = "x";
   type.age = 1;
   return type;
}

class MyType
{
   public string name {get;set;}
   public int age {get;set;}

   public MyType()
   {
   }
}

Upvotes: 7

Tim Schmelter
Tim Schmelter

Reputation: 460018

The easiest way would be to return a Tuple<string, int> (available since .NET 4):

public static Tuple<string, int> GetType6()
{
    var name = "x";
    var age = 1;
    return Tuple.Create(name, age);
}

You can read the values in this way:

var pair = GetType6();
string name = pair.Item1;
int age = pair.Item2;

Of course more robust, readable and maintainable is to create a class:

class User
{
    public string Name { get; set; }
    public int Age{ get; set; }
}

public static User GetUser()
{
    var name = "x";
    var age = 1;
    return new User{Name = name, Age = age };
}

var user  = GetUser();
string name = user.Name;
int age = user.Age;

Upvotes: 5

Related Questions