简体   繁体   English

一次从同一方法获取多个值时的更好方法

[英]Better approach when obtaining multiple values from the same method at once

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

You just need to save return value你只需要保存返回值

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

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM