Home > Mobile >  Better approach when obtaining multiple values from the same method at once
Better approach when obtaining multiple values from the same method at once

Time:05-19

I have the following method which returns a tuple,

public Tuple<int, bool> GetStudentInformation(long stutID)

Called as,

Marks= GetStudentInformation((Id).Item1;
HasPassed= GetStudentInformation((Id).Item2;

This works fine as it is but I dont like that I call the same method twice to get item1 and item2, using Tuple is probably not the way ahead but any advice if c# has support to get two values returned over a single execution of the method?

CodePudding user response:

You just need to save return value

Tuple<int, bool> info = GetStudentInformation(Id);

Marks = info.Item1;
HasPassed = info.Item2;

CodePudding user response:

example of a clearer way I prefer when using tuples:

public (int data1, bool data2) GetStudentInformation(Id) {
    return (123, true);
}

var studentInfo = GetStudentInformation(111);
Console.Write(studentInfo.data1);
Console.Write(studentInfo.data2);
  • Related