简体   繁体   English

为什么部分方法只能有void返回类型?

[英]Why partial methods can only have void return type?

what is the reason /logic/obstacle behind the fact that partial methods can only have void return type? 部分方法只能具有void返回类型这一事实背后的原因/逻辑/障碍是什么?

thanks 谢谢

Partial methods are designed to be left out if you did not provide an implementation for them. 如果您没有为它们提供实现,则部分方法被设计为省略。 The compiler actually removes calls to partial methods that are not implemented. 编译器实际上删除了对未实现的部分方法的调用。

This also highlights why they cannot return anything: If you relied on a return value without implementing the partial method, then what? 这也突出了为什么他们不能返回任何东西:如果你依赖一个返回值而不实现部分方法,那么呢? You'd have something uninitialised, despite the code clearly showing an assignment. 尽管代码清楚地显示了一项任务,但你还是有一些未初始化的东西。

Similarly, methods that use the Conditional attribute can only return void for the same reason. 同样,使用Conditional属性的方法只能出于同样的原因返回void The method call may or may not exist in the compiled IL. 方法调用可能存在于编译的IL中,也可能不存在。

Implementation of partial methods are intended as optional. 部分方法的实现是可选的。

If the implementation is not provided, a call is still valid, but will be silently removed by the compiler. 如果未提供实现,则调用仍然有效,但编译器将以静默方式删除。 A delegate can only be assigned a partial methods which is implemented. 委托只能分配一个实现的部分方法。

This means that they should have no side effects - effectively no return values or "out" parameters, they can not be virtual, and are always private. 这意味着它们应该没有副作用 - 实际上没有返回值或“out”参数,它们不能是虚拟的,并且始终是私有的。

More here . 更多这里

Unimplemented partial methods are removed at compile time. 在编译时删除未实现的部分方法。 A compiler can ignore a call to void methods because they only modify state of an already existing object. 编译器可以忽略对void方法的调用,因为它们只修改现有对象的状态。 It cannot remove methods that return objects because that would invalidate the code. 它无法删除返回对象的方法,因为这会使代码无效。

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

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