Pergunta

I was reading up on partial methods since they will become a lot more important in C#-6 / Visual Studio 2013 update 2 in combination with Windows Universal Projects. While reading the documentation I read this weird limitation on the signature of partial methods:

Partial methods can have ref but not out parameters.

I don't understand the reason for this limitation. Since partial methods are basically a normal method with the signature and implementation in different files, what technical reason would there be to not support out parameters? Or any other reason for this limitation for that matter. Especially since they do support ref parameters which are very similar.

Foi útil?

Solução

If a partial method is declared but not implemented, it does not get called.

This would mean that any out parameter does not get assigned, which is not allowed.

This isn't a problem with ref parameters, as they have to be assigned before they are passed to the method, so they are definitely assigned even if the method is not called.

Outras dicas

Makes sense, if you look at details to be taken care of when implementing partial methods:

Partial methods implementation is optional.

An out variable is never assigned a value while a ref variable is always assigned a value. Consider, the case when partial method is not implemented, we would have a variable which is unassigned. No problem would occur for a ref variable since it has some assigned value.

Due to this, out variables are not supported since an out variable means that value is being returned.

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