Ultimately, "best" is subjective and depends on your scenario. We can present options, but "best" is local to you.
There are various ways of returning multiple values; out
is very efficient, but not very flexible, and many people find it confusing (indeed, some would say it is an anti-pattern). But for example:
DataTable Foo(out int val) {
...
val = something
return dataTable;
}
Perhaps more convenient is to declare a return type that encapsulates the two values:
FooResult Foo() {
...
return new FooResult { Table = dataTable, Value = val };
}
where FooResult
is a class with 2 properties. This has the advantage that you can add more properties trivially, without changing the API.
Similarly, you could return a Tuple<DataTable,int>
- but I advise against it as it expresses nothing about what each value represents. In a Tuple<string,int,string>
, what is Item1
? how is it different to Item3
?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…