C# Why can partial methods use ref, but not out?

我们两清 提交于 2019-12-18 12:14:15

问题


Pretty straight forward. MSDN states that you can use ref, but not out for partial methods. I'm just curious as to the why? It was my understanding that when code is compiled, the partials are merged, so what is up with the restriction? Is there more to partial than just making code files cleaner and organized (i.e. eyecandy)?

Reference: MSDN Article - "Partial methods can have ref but not out parameters."


回答1:


You got to consider what happens if the partial method isn't implemented.

What happens then is that all calls to the method is just stripped out as though they never happened.

So for a method using out, it would look like this:

stream s;
GetStream(out s);
s.Write(...);

and be compiled as though it said this:

stream s;
s.Write(...);

This code is not allowed because s has not been initialized. The guarantee that the variable would be initialized by the time you try to call the Write method on it was tied up with the call to GetStream.

It is the same with methods returning data. Since the entire method call is just not compiled if you haven't implemented the partial method, you need to consider what you can and cannot do and still leave the code that calls it valid. In terms of out and return values, it has the potential of leaving the calling code invalid or incomplete, so it is not allowed.

As for ref, that is valid since the initialization has been taken care of by the calling code:

stream s = null;
GetStream(ref s); // may be stripped out
if (s != null)
    s.Write(...);



回答2:


Because unlike ref parameters, out parameters MUST be initialized before the method returns. If the partial method is not implemented (which is a valid scenario,) how can it be initialized?




回答3:


My guess would be because out parameters don't need to be initialized whereas ref parameters do.

If you used an out parameter on a partial method, how could C# verify that the parameter was initialized or not?




回答4:


An out parameter suggests that you want a value out of the method. If the method doesn't exist, it can't provide that value.

The alternative would be to set the variable's value explicitly to its default value (0, null etc) instead of executing the method call. That way the variable would still be definitely initialized - although the default value may not be a terribly useful one. I believe the C# team have considered this - it may even make it into a future version, who knows? Personally I doubt that it would be particularly useful, but the possibility is there.

For the moment, you could always use a ref parameter instead, and just initialize the variable manually before the call to whatever the default value should be.




回答5:


I would assume the reason is because a partial method with only a signature (i.e. no implementation) is still valid. If you had an out parameter an implementation-less method would always cause an error (as there's nothing assigning the out value)




回答6:


A partial method is split across partial classes. A method is required to assign a value to an OUT parameter. Partial methods may or may not be implemented. It would mean multiple code chunks is trying to assign value to the OUT parameter.




回答7:


As everyone else has stated out params must be assigned. To add this will generate compiler error CS0177 ref on the other hand must be assigned prior to making the call.



来源:https://stackoverflow.com/questions/3453197/c-sharp-why-can-partial-methods-use-ref-but-not-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!