简体   繁体   中英

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?

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. The method call may or may not exist in the compiled 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.

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. It cannot remove methods that return objects because that would invalidate the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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