How to return multiple values from a method in C#

3 Answers

0 votes
using System;
 
class Program
{
    static void Method(out int n, out string s, out float f) {
        n = 847;
        s = "C# programming";
        f = 459.234f;
    }
    static void Main()
    {
        int n;
        string s;
        float f;
         
        Method(out n, out s, out f);
         
        Console.WriteLine(n);
        Console.WriteLine(s);
        Console.WriteLine(f);
    }
}
 
 
 
/*
run
 
847
C# programming
459.234
 
*/

 



answered Feb 18, 2017 by avibootz
edited Apr 24, 2024 by avibootz
0 votes
using System;
using System.Collections.Generic;

class Program
{
    static KeyValuePair<int, int> GetNumbers() {
        return new KeyValuePair<int, int>((int)Math.Pow(2, 3),
                                              (int)Math.Pow(2, 5));
    }
    
    public static void Main(string[] args)
    {
        var keyvalue = GetNumbers();
 
        Console.WriteLine(keyvalue.Key);
        Console.WriteLine(keyvalue.Value);
    }
}


 
/*
run:
      
8
32
  
*/

 



answered Feb 18, 2017 by avibootz
edited Apr 24, 2024 by avibootz
0 votes
using System;
 
class Program
{
    static Tuple<int, int, int> m() { 
        int a = 34, b = 982, c = 5; 
 
        return Tuple.Create(a, b, c);
    } 
     
    static void Main() {
        var result = m(); 
         
        Console.WriteLine("{0}, {1}, {2}", result.Item1, result.Item2, result.Item3);
    }
}
 
 
 
/*
run:
 
34, 982, 5
 
*/

 



answered Apr 24, 2024 by avibootz

Related questions

1 answer 148 views
2 answers 231 views
3 answers 251 views
1 answer 177 views
1 answer 182 views
1 answer 175 views
175 views asked Jan 2, 2022 by avibootz
...