Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# support multiple return values?

This is a very basic question, and if what I am thinking of doing is complicated/involved, then I don't expect you to go into detail... I've read that this may involve structs or hash or some other scary procedure I've not gotten to yet. If so, I'm sure it'll get me soon.

Working on learning classes, methods, and return values.

I'd like to have my class/method return Current Hour and Minute. Simple enough, really. Is this constructed correctly, or properly?

class MyClass
{
    public int GetHour (int hr, int min)
    {
        DateTime dt = DateTime.Now;
        int hour = dt.Hour;
        int minute = dt.Minute;

        return hour;
        return minute;

    }
}

And, calling it from Main(): Getting some errors (No overload for method and Unreachable code detected)

static void Main ( string[] args )
{
    MyClass mc = new MyClass ();
    Console.WriteLine ("Hour: {0} \n Minute: {1}", mc.GetHour());   
    Console.ReadLine ();
}

Question is: Am I Close?

like image 714
DonG Avatar asked Oct 21 '10 22:10

DonG


2 Answers

As mentioned by @ChaosPandion, in that specific case you would return a DateTime struct.

In general, however, you would have the following options:

Using out parameters

This is a simple way that will usually always work. However, it is a bit clunky, as the result is returned where you usually would expect the function arguments to be passed and the method signature might get lengthy and hard to refactor.

public void GetTime(out int hr, out int min) 
{ 
    DateTime dt = DateTime.Now;
    hr = dt.Hour;
    min = dt.Minute;
}

static void Main(string[] args)
{
     // declare variables for out parameters first
     int hour, minute;
     GetTime(out hour, out minute);
}

Using an array

This is a simple method that works well if the values to be returned have the same type.

public int[] GetTime() 
{ 
    DateTime dt = DateTime.Now;
    return new[] { dt.Hour, dt.Minute};
}

Using a property bag (A property bag is a simple class which only has properties)

This is very convenient and allows easy modification of the type and number of returned values later on without changing the method signature.

class A
{ 
     int Prop1 { get; set; }
     int Prop2 { get; set; }
}

public A SomeMethod()
{
    return new A() { Prop1 = 1, Prop2 = 2 }
}    

Using a Tuple

In C# 4.0 (requires VS 2010) you can use the Tuple<T1, T2, ...> class:

public Tuple<int, int> GetTime() 
{ 
    DateTime dt = DateTime.Now;
    return Tuple.Create(dt.Hour, dt.Minute);
}

C# 7.0 Tuples

C# 7.0 adds support for multiple return values. You can write code like this to return an implicitly created tuple:

(string, string, string) LookupName(long id) // tuple return type
{
    ... // retrieve first, middle and last from data storage
    return (first, middle, last); // tuple literal
}

The tuple elements are names Item1, Item2, etc by default, but you can also specify names, e.g.

(string first, string middle, string last) LookupName(long id) // tuple return type
{
    ... // retrieve first, middle and last from data storage
    return (first, middle, last); // tuple literal
}

and then access the tuple elements via those names:

var names = LookupName(id);
WriteLine($"found {names.first} {names.last}.");
like image 52
Dirk Vollmar Avatar answered Oct 20 '22 22:10

Dirk Vollmar


C# does not support multiple return values so in this case you should return a DateTime struct which is the idiomatic approach. The client code can simply ignore the properties that they don't care about. You could create your own simple struct but it really isn't worth the effort.

like image 22
ChaosPandion Avatar answered Oct 20 '22 23:10

ChaosPandion