Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do partial methods support ref but not out parameters [duplicate]

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.

like image 254
Roy T. Avatar asked Apr 25 '14 11:04

Roy T.


People also ask

What is the difference between ref & out parameters?

ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.

What will happen if there is an implementation of partial method without defining partial?

Similar to a partial class, a partial method can be used as a definition in one part while another part can be the implementation. If there is no implementation of the partial method then the method and its calls are removed at compile time. Compiler compiles all parts of a partial method to a single method.

Why do we use partial class in C#?

Partial Class is a unique feature of C#. It can break the functionality of a single class into many files. When the application is compiled, these files are then reassembled into a single class file. The partial keyword is used to build a partial class.

What are partial methods in C#?

A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not.


1 Answers

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.

like image 129
Rawling Avatar answered Sep 28 '22 08:09

Rawling