2

I want to return age values from a method in C# like the following

private int UpdateAges( int age1,  int age2,  int age3,  int age4)
{
    age1++;
    age2++;
    age3++;
    age4++;
    return (age1, age2, age3, age4);
}

and I can use it like the following

MessageBox.Show(UpdateAges(45, 30, 45, 30).ToString());
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 4
    iEnumerable, tuple, your own class... There are plenty of options. – Zohar Peled Sep 27 '18 at 04:39
  • 3
    Replace 'private int' with 'private (int,int,int,int)' – Access Denied Sep 27 '18 at 04:42
  • 2
    If you dont have team leader or someone to guide you, I would suggest reading programming books, read open source (c#/java) code and learn how to write code. On your case, what do you do with result? Do you use the values? Or Pass them forward to another function? True answer require more information, technical answer would be Tuple or create "holder class" and return 1 class that hold these 4 int values – ilansch Sep 27 '18 at 04:54
  • 1
    Take a look at [this answer](https://stackoverflow.com/a/30632338/7461634). – Emaro Sep 27 '18 at 07:39

9 Answers9

5

Try this:

private Tuple<int, int, int, int> UpdateAges( int age1,  int age2,  int age3,  int age4)
{
    age1++;
    age2++;
    age3++;
    age4++;

    return Tuple.Create(age1, age2, age3, age4);
}

See also: about C# 7 new included Tuples syntax

Ilya
  • 4,583
  • 4
  • 26
  • 51
3

If you just want to show the result in a message box, returns a string:

private string UpdateAges(int age1, int age2, int age3, int age4)
{
    age1++;
    age2++;
    age3++;
    age4++;
    return "age1 = " + age1 + ", age2 = " + age2 + ", age3 = " + age3 + ", age4 = " + age4;
}

MessageBox.Show(UpdateAges(45, 30, 45, 30));

If you also want to update the age values after the message box shown, add ref keyword for each argument.

private string UpdateAges(ref int age1, ref int age2, ref int age3, ref int age4)
{
    age1++;
    age2++;
    age3++;
    age4++;
    return "age1 = " + age1 + ", age2 = " + age2 + ", age3 = " + age3 + ", age4 = " + age4;
}

MessageBox.Show(UpdateAges(ref age1, ref age2, ref age3, ref age4));
shingo
  • 18,436
  • 5
  • 23
  • 42
2

Refer this - How to Return Multiple Values From a Function in C#

There are few ways to return multiple values from a function.

  1. Reference parameters
  2. Output parameters
  3. Returning an Array
  4. Returning an object of class/struct type -- This is way we mostly follow
  5. Returning a Tuple

Ref- From C# 7, you return more than one value from a method thanks to tuple types and tuple literals.

(int, int, int, int) UpdateAges(int age1,  int age2,  int age3,  int age4) // tuple return type
{   
    age1++;
    age2++;
    age3++;
    age4++;

    return (age1, age2, age3, age4);
}

Use:

var values = UpdateAges(1, 2, 2, 3);

Using tuples:

private Tuple<int, int, int, int> UpdateAges(int age1, int age2, int age3, int age4)
{
    age1++;
    age2++;
    age3++;
    age4++;

    return Tuple.Create(age1, age2, age3, age4);
}
Emaro
  • 1,397
  • 1
  • 12
  • 21
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • _Named tuples_ are even sweeter. https://blogs.msdn.microsoft.com/dotnet/2017/03/09/new-features-in-c-7-0/ –  Sep 27 '18 at 05:12
1

You can solve this problem in many ways.

  1. Using Tuple

    private Tuple<int, int, int, int> UpdateAges(int age1, int age2, int age3, int age4)
    {
        return Tuple.Create(++age1, ++age2, ++age3, ++age4);
    }
    
  2. Using List

    private List<int> UpdateAges(int age1, int age2, int age3, int age4)
    {
        return new List(){++age1, ++age2, ++age3, ++age4};
    }
    
  3. Using Arrays

    private int[] UpdateAges(int age1, int age2, int age3, int age4)
    {
        var results = new int[4];
        results[0] = ++age1;
        results[1] = ++age2;
        results[2] = ++age3;
        results[3] = ++age4;
    }
    
  4. Using Reference parameters

    private List<int> UpdateAges(ref int age1, ref int age2, ref int age3, ref int age4)
    {
        age1++;
        age2++;
        age3++;
        age4++;
    }
    
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
Tejus
  • 694
  • 1
  • 7
  • 19
1

How to do it with a List:

private static List<int> UpdateAges(int age1, int age2, int age3, int age4)
{
    return new List<int> { ++age1, ++age2, ++age3, ++age4 };
}

Then call it like this:

Console.WriteLine(string.Join(",", UpdateAges(1, 2, 3, 4)));

OK, hope that helps.

er-sho
  • 9,581
  • 2
  • 13
  • 26
Poida
  • 36
  • 4
1

While you can use Tuple or ref to achieve what you want, I'm not sure it's best practice in your case.

You can encapsulate all you logic with a dedicated class:

public class Ages
{
    public int Age1 { get; set; }
    public int Age2 { get; set; }
    public int Age3 { get; set; }
    public int Age4 { get; set; }

    private void UpdateAges()
    {
        this.Age1++;
        this.Age2++;
        this.Age3++;
        this.Age4++;
    }

    private string ToString() => 
        $"Age1 = {this.age1}, Age2 {this.age2}, Age3 = {this.age3}, Age4 {age4}";
}

This way everything related to handling the ages are in one place, and can be reused across your code.

You can ofcourse take it one step further and replace the "age" properties with a List or Array.

public class Ages
{
    public int[] Ages { get; }

    public Ages (params int[] ages)
    {
        this.Ages = ages;
    }

    private void UpdateAges() =>
        for (int i = 0; i < this.Ages.Length; i++) this.Ages[i]++;

    private string ToString() =>  
        string.Join(", ", 
            this.Ages.Select((age, i) => $"Age{i} = {age}"));
}


var ages = new Ages(31, 40, 12, 18);
ages.UpdateAges();
MessageBox.Show(ages.ToString());
Emaro
  • 1,397
  • 1
  • 12
  • 21
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
1

With the method parameter params, you can pass any numbers of parameter returning in a Enumerable, list array etc..
With a flexible call Increment(1,2,3),Increment(1,2),Increment(myIntArray),Increment(1)` ..

static IEnumerable<int> Increment(params int[] args)
{
    return args.Select(x => x + 1);
}

Allowing call like :

var result = Increment(45, 30, 45, 30);
// {46, 31, 46, 31}

For the display, you can use string join. string.Join(", ", result)

static void Main(string[] args)
{
    var uniqueValue = 99;
    var multipleValues = new[] { 1, 2, 6 };


    var uvResult = Increment(uniqueValue);
    var mvResult = Increment(multipleValues);


    Console.WriteLine($"uniqueValue = {uniqueValue}  => {string.Join(", ",uvResult)}");
    Console.WriteLine($"multipleValues = {string.Join(", ",multipleValues)}  => {string.Join(", ",multipleValues)}");

}
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
  • 1
    You could even call `Increment(1, 2, 3, 4);` – Emaro Sep 27 '18 at 07:53
  • 1
    @Emaro, Yep I know. op code `UpdateAges(45, 30, 45, 30)` immediatly ring this bell. Just forgot to include it in the answer. Thanks for that. Update the answer to make it clear – Drag and Drop Sep 27 '18 at 08:26
0

You can also use 'out int' like this.

private void UpdateAges(int IAge1, int IAge2, int IAge3, int IAge4, out int OAge1, out int OAge2, out int OAge3, out int OAge4)
{
    OAge1 = IAge1++;
    OAge2 = IAge2++;
    OAge3 = IAge3++;
    OAge4 = IAge4++;
}

when you call this, you need to use like this.

UpdateAges( Age1, Age2, Age3, Age4, out Age1, out Age2, out Age3, out Age4 );
er-sho
  • 9,581
  • 2
  • 13
  • 26
Arphile
  • 841
  • 6
  • 18
  • 2
    This solution is unnecessary complex. `void Update(out int i) => i++;` would be enough (eg. you just need the out var, no need to double the amount of parameters). – Emaro Sep 27 '18 at 07:47
0

1. Using out:

static void UpdateAges(out int age1, out int age2)
{
    age1++;
    age2++;
}

int age1;
int age2;
UpdateAges(out age1, out age2);
Console.WriteLine(age1);
Console.WriteLine(age2);

2. Usng KeyValuePair:

static KeyValuePair<int, int> UpdateAges(age1, age2)
{
    return new KeyValuePair<int, int>(age1++, age2++);
}

int age1;
int age2;
var pair = GetTwoNumbers(age1,age2);
Console.WriteLine(pair.Key);
Console.WriteLine(pair.Value);

3. Using Tuple:

static Tuple<int, int> UpdateAges(age1, age2)
{
    return new Tuple<int, int>(age1++, age2++);
}

int age1;
int age2;
var tuple = UpdateAges(age1,age2);
int a = tuple.age1;
int b = tuple.age2;
Kamrul Hasan
  • 127
  • 1
  • 12
  • 2
    A KeyValuePair should not be used like a tuple. How can age1 be the key for age2? – Emaro Sep 27 '18 at 07:36
  • 1
    @Emaro It's use to age1 like key and age2 like value – Kamrul Hasan Sep 27 '18 at 21:00
  • 2
    I get how it works. However it’s semantically misused which I consider as bad practice. Value tuples `(int a1, int a2)` would be a much better choice. – Emaro Sep 28 '18 at 05:54