Pergunta

what is the reason /logic/obstacle behind the fact that partial methods can only have void return type?

thanks

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top