简体   繁体   English

为什么部分方法必须无效?

[英]Why do partial methods have to be void?

I am currently learning C# with the book called Beginning Visual C# 2010 and I am in the chapter that discusses different aspects and characteristics of partial methods and classes. 我目前正在学习C#,这本书名为Beginning Visual C#2010,我在讨论部分方法和类的不同方面和特征的章节中。

To quote the book: 引用这本书:

Consider at this point why partial methods can't have a return type. 在这一点上考虑为什么部分方法不能具有返回类型。 If you can answer that to your own satisfaction, you can be sure that you fully understand this topic — so that is left as an exercise for you." 如果您能够自己满意地回答这个问题,那么您可以确信您完全理解了这个主题 - 所以这是留给您的练习。“

This is where I get stuck. 这是我被卡住的地方。 The only reason that I can think of is that when the method's return value is assigned to something in the code, it would generate an error if there is no definition implemented for the partial method. 我能想到的唯一原因是,当方法的返回值被分配给代码中的某些内容时,如果没有为partial方法实现定义,则会产生错误。

Can someone clear this topic for me please? 有人可以帮我清楚这个话题吗?

Because calls to them can't be eliminated from the calling code in case they are not implemented without breaking it. 因为在没有破坏调用代码的情况下不能从调用代码中删除对它们的调用。

Example: 例:

partial void foo();
partial int bar();

Calling code: 来电代码:

...
foo(); // successfully removed if foo isn't implemented
int x = bar() * 2; // what to do here?
Console.WriteLine(x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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