Return multiple values to a method caller

前端 未结 26 2301
故里飘歌
故里飘歌 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:36

    Here are basic Two methods:

    1) Use of 'out' as parameter You can use 'out' for both 4.0 and minor versions too.

    Example of 'out':

    using System;
    
    namespace out_parameter
    {
      class Program
       {
         //Accept two input parameter and returns two out value
         public static void rect(int len, int width, out int area, out int perimeter)
          {
            area = len * width;
            perimeter = 2 * (len + width);
          }
         static void Main(string[] args)
          {
            int area, perimeter;
            // passing two parameter and getting two returning value
            Program.rect(5, 4, out area, out perimeter);
            Console.WriteLine("Area of Rectangle is {0}\t",area);
            Console.WriteLine("Perimeter of Rectangle is {0}\t", perimeter);
            Console.ReadLine();
          }
       }
    }
    

    Output:

    Area of Rectangle is 20

    Perimeter of Rectangle is 18

    *Note:*The out-keyword describes parameters whose actual variable locations are copied onto the stack of the called method, where those same locations can be rewritten. This means that the calling method will access the changed parameter.

    2) Tuple<T>

    Example of Tuple:

    Returning Multiple DataType values using Tuple<T>

    using System;
    
    class Program
    {
        static void Main()
        {
        // Create four-item tuple; use var implicit type.
        var tuple = new Tuple<string, string[], int, int[]>("perl",
            new string[] { "java", "c#" },
            1,
            new int[] { 2, 3 });
        // Pass tuple as argument.
        M(tuple);
        }
    
        static void M(Tuple<string, string[], int, int[]> tuple)
        {
        // Evaluate the tuple's items.
        Console.WriteLine(tuple.Item1);
        foreach (string value in tuple.Item2)
        {
            Console.WriteLine(value);
        }
        Console.WriteLine(tuple.Item3);
        foreach (int value in tuple.Item4)
        {
            Console.WriteLine(value);
        }
        }
    }
    

    Output

    perl
    java
    c#
    1
    2
    3
    

    NOTE: Use of Tuple is valid from Framework 4.0 and above.Tuple type is a class. It will be allocated in a separate location on the managed heap in memory. Once you create the Tuple, you cannot change the values of its fields. This makes the Tuple more like a struct.

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

    There is many way; but if you don't want to create a new Object or structure or something like this you can do like below after C# 7.0 :

     (string firstName, string lastName) GetName(string myParameter)
        {
            var firstName = myParameter;
            var lastName = myParameter + " something";
            return (firstName, lastName);
        }
    
        void DoSomethingWithNames()
        {
            var (firstName, lastName) = GetName("myname");
    
        }
    
    0 讨论(0)
  • 2020-11-21 22:39

    You can also use an OperationResult

    public OperationResult DoesSomething(int number1, int number2)
    {
    // Your Code
    var returnValue1 = "return Value 1";
    var returnValue2 = "return Value 2";
    
    var operationResult = new OperationResult(returnValue1, returnValue2);
    return operationResult;
    }
    
    0 讨论(0)
  • 2020-11-21 22:40

    If you mean returning multiple values, you can either return a class/struct containing the values you want to return, or use the "out" keyword on your parameters, like so:

    public void Foo(int input, out int output1, out string output2, out string errors) {
        // set out parameters inside function
    }
    
    0 讨论(0)
  • 2020-11-21 22:43

    Mainly two methods are there. 1. Use out/ref parameters 2. Return an Array of objects

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

    You can use three different ways

    1. ref / out parameters

    using ref:

    static void Main(string[] args)
    {
        int a = 10;
        int b = 20;
        int add = 0;
        int multiply = 0;
        Add_Multiply(a, b, ref add, ref multiply);
        Console.WriteLine(add);
        Console.WriteLine(multiply);
    }
    
    private static void Add_Multiply(int a, int b, ref int add, ref int multiply)
    {
        add = a + b;
        multiply = a * b;
    }
    

    using out:

    static void Main(string[] args)
    {
        int a = 10;
        int b = 20;
        int add;
        int multiply;
        Add_Multiply(a, b, out add, out multiply);
        Console.WriteLine(add);
        Console.WriteLine(multiply);
    }
    
    private static void Add_Multiply(int a, int b, out int add, out int multiply)
    {
        add = a + b;
        multiply = a * b;
    }
    

    2. struct / class

    using struct:

    struct Result
    {
        public int add;
        public int multiply;
    }
    static void Main(string[] args)
    {
        int a = 10;
        int b = 20;
        var result = Add_Multiply(a, b);
        Console.WriteLine(result.add);
        Console.WriteLine(result.multiply);
    }
    
    private static Result Add_Multiply(int a, int b)
    {
        var result = new Result
        {
            add = a * b,
            multiply = a + b
        };
        return result;
    }
    

    using class:

    class Result
    {
        public int add;
        public int multiply;
    }
    static void Main(string[] args)
    {
        int a = 10;
        int b = 20;
        var result = Add_Multiply(a, b);
        Console.WriteLine(result.add);
        Console.WriteLine(result.multiply);
    }
    
    private static Result Add_Multiply(int a, int b)
    {
        var result = new Result
        {
            add = a * b,
            multiply = a + b
        };
        return result;
    }
    

    3. Tuple

    Tuple class

    static void Main(string[] args)
    {
        int a = 10;
        int b = 20;
        var result = Add_Multiply(a, b);
        Console.WriteLine(result.Item1);
        Console.WriteLine(result.Item2);
    }
    
    private static Tuple<int, int> Add_Multiply(int a, int b)
    {
        var tuple = new Tuple<int, int>(a + b, a * b);
        return tuple;
    }
    

    C# 7 Tuples

    static void Main(string[] args)
    {
        int a = 10;
        int b = 20;
        (int a_plus_b, int a_mult_b) = Add_Multiply(a, b);
        Console.WriteLine(a_plus_b);
        Console.WriteLine(a_mult_b);
    }
    
    private static (int a_plus_b, int a_mult_b) Add_Multiply(int a, int b)
    {
        return(a + b, a * b);
    }
    
    0 讨论(0)
提交回复
热议问题