“出"的目的是什么?调用方的关键字(在 C# 中)? [英] What is the purpose of the "out" keyword at the caller (in C#)?

查看:27
本文介绍了“出"的目的是什么?调用方的关键字(在 C# 中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个 C# 函数有一个输出参数时,你要明确如下:

When a C# function has an output parameter, you make that clear as follows:

private void f(out OutputParameterClass outputParameter);

这表明在调用函数时不必初始化参数.但是,在调用这个函数时,必须重复out关键字:

This states that the parameter does not have to be initialized when the function is called. However, when calling this function, you have to repeat the out keyword:

f(out outputParameter);

我想知道这有什么用.为什么需要重复部分功能规范?有人知道吗?

I am wondering what this is good for. Why is it necessary to repeat part of the function specification? Does anyone know?

推荐答案

我得到的最佳答案是 plinth:

The best answer I got was posted as a comment by plinth:

重复 out/ref 的最重要原因是,如果您调用的函数被重构为不同的签名,您将收到编译错误.最值得注意的是,如果参数从非输出变为输出,您马上就会知道.

The most important reason for the repeating of out/ref is that if the function you're calling gets refactored with a different signature, you will get a compile error. Most notably, if a parameter goes from non-out to out, you'll know right away.

这篇关于“出"的目的是什么?调用方的关键字(在 C# 中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆