c# - 一次从同一方法获取多个 values 时的更好方法

我有以下返回元组的方法,

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

称为,

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

这工作正常,但我不喜欢我调用相同的方法两次来获取 item1 和 item2,使用 Tuple 可能不是前进的方向,但是如果 c# 支持在一次执行中返回两个 values 的任何建议方法?

回答1

你只需要保存 return value

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

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

回答2

使用元组时我更喜欢的更清晰方式的示例:

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

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

相似文章

随机推荐

最新文章