c# - C# - 获取以元组作为输出的方法,但多次执行相同的参数?
问题描述
我是编码和 C# 的新手,我想知道如果您使用元组作为返回值,一个方法是否会被多次调用?
可能缺少此描述,因为我不是英语母语,所以这里有一些示例代码:
public static (bool Correct, string Output) MyMethod1(int MyArgument)
{
// Do something
return (true, MyOutput);
}
public void Execute()
{
if (MyMethod1(100).Correct)
{
Console.WriteLine(MyMethod1(100).Output);
}
}
所以我想知道我们现在是否通过整个方法 MyMethod1 两次或一次,因为参数是相同的。
非常感谢。
如果我做错了什么,也请让我知道,发布这个。因为我也是新手。
解决方案
在这种情况下,要让你的方法只执行一次,你应该像这样存储方法的检索结果:
public static (bool Correct, string Output) MyMethod1(int MyArgument)
{
// Do something
return (true, MyOutput);
}
public void Execute()
{
(bool correct, string output) result = MyMethod(100);
if (result.correct)
{
Console.WriteLine(result.output);
}
}
推荐阅读
- gltf - 如何以编程方式为给定的 GLTF 文件生成全景图像(360 度图像)?
- android - Grid Recyclerview 消失的项目,两个项目之间有一个空白单元格
- ios - 如何确定我面对的是节点
- java - 无法使用 resttemplate 使用 rest api
- android - Android 一加 6(Android pie 版) BOOT COMPLETE Broadcast Receiver not working
- numpy-ndarray - numpy排序不一致?lexsort vs 独特的
- keyboard - 时间:2019-04-01 标签:c#globalkeyhookswithsourceid
- java - 会话范围 bean 中的方法为非 null 字段返回 null
- windows - 变量绑定响应值不一致 - Windows SNMP 代理
- xml - 使用 XPATH 解析 xml