Return multiple values to a method caller

前端 未结 26 2299
故里飘歌
故里飘歌 2020-11-21 21:57

I read the C++ version of this question but didn\'t really understand it.

Can someone please explain clearly if it can be done and how?

相关标签:
26条回答
  • 2020-11-21 22:50

    Just use in OOP manner a class like this:

    class div
    {
        public int remainder;
    
        public int quotient(int dividend, int divisor)
        {
            remainder = ...;
            return ...;
        }
    }
    

    The function member returns the quotient which most callers are primarily interested in. Additionally it stores the remainder as a data member, which is easily accessible by the caller afterwards.

    This way you can have many additional "return values", very useful if you implement database or networking calls, where lots of error messages may be needed but only in case an error occurs.

    I entered this solution also in the C++ question that OP is referring to.

    0 讨论(0)
  • 2020-11-21 22:52
    <--Return more statements like this you can --> 
    
    public (int,string,etc) Sample( int a, int b)  
    {
        //your code;
        return (a,b);  
    }
    

    You can receive code like

    (c,d,etc) = Sample( 1,2);
    

    I hope it works.

    0 讨论(0)
  • 2020-11-21 22:52

    A method taking a delegate can provide multiple values to the caller. This borrows from my answer here and uses a little bit from Hadas's accepted answer.

    delegate void ValuesDelegate(int upVotes, int comments);
    void GetMultipleValues(ValuesDelegate callback)
    {
        callback(1, 2);
    }
    

    Callers provide a lambda (or a named function) and intellisense helps by copying the variable names from the delegate.

    GetMultipleValues((upVotes, comments) =>
    {
         Console.WriteLine($"This post has {upVotes} Up Votes and {comments} Comments.");
    });
    
    0 讨论(0)
  • 2020-11-21 22:53

    You cannot do this in C#. What you can do is have a out parameter or return your own class (or struct if you want it to be immutable).

    Using out parameter
    public int GetDay(DateTime date, out string name)
    {
      // ...
    }
    
    Using custom class (or struct)
    public DayOfWeek GetDay(DateTime date)
    {
      // ...
    }
    
    public class DayOfWeek
    {
      public int Day { get; set; }
      public string Name { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-21 22:54

    You either return a class instance or use out parameters. Here's an example of out parameters:

    void mymethod(out int param1, out int param2)
    {
        param1 = 10;
        param2 = 20;
    }
    

    Call it like this:

    int i, j;
    mymethod(out i, out j);
    // i will be 20 and j will be 10
    
    0 讨论(0)
  • 2020-11-21 22:56

    You could use a dynamic object. I think it has better readability than Tuple.

    static void Main(string[] args){
        var obj = GetMultipleValues();
        Console.WriteLine(obj.Id);
        Console.WriteLine(obj.Name);
    }
    
    private static dynamic GetMultipleValues() {
        dynamic temp = new System.Dynamic.ExpandoObject();
        temp.Id = 123;
        temp.Name = "Lorem Ipsum";
        return temp;
    }
    
    0 讨论(0)
提交回复
热议问题