lz说的是Partial Method吧Partial Method要求必须是Private的,不能有返回值,参数不能是out的。不能用virtual , abstract , override , new , sealed , or extern修饰
贴下原文:Partial classes may also define partial methods. Partial methods are defined in one partial class definition without a method body, and implemented in another partial class definition. In both places, the partial keyword is used: public partial class MyClass { partial void MyPartialMethod(); } public partial class MyClass { partial void MyPartialMethod() { // Method implementation } } Partial methods can also be static, but they are always private and can ’ t have a return value. Any parameters they use can ’ t be out parameters, although they can be ref parameters. They also can ’ t use the virtual , abstract , override , new , sealed , or extern modifier. Given these limitations, it is not immediately obvious what purpose partial methods fulfill. In fact, they are important when it comes to code compilation, rather than usage. Consider the following code: public partial class MyClass { partial void DoSomethingElse(); public void DoSomething() { Console.WriteLine(“DoSomething() execution started.”); DoSomethingElse(); Console.WriteLine(“DoSomething() execution finished.”); } } public partial class MyClass { partial void DoSomethingElse() { Console.WriteLine(“DoSomethingElse() called.”); } }Here, the partial method DoSomethingElse is defined and called in the first partial class definition, and implemented in the second. The output, when DoSomething is called from a console application, is what you might expect: DoSomething() execution started. DoSomethingElse() called. DoSomething() execution finished. If you were to remove the second partial class definition or partial method implementation entirely (or comment out the code), the output would be as follows: DoSomething() execution started. DoSomething() execution finished. You might assume that what is happening here is that when the call to DoSomethingElse is made, the runtime discovers that the method has no implementation and therefore continues executing the next line of code. What actually happens is a little subtler. When you compile code that contains a partial method definition without an implementation, the compiler actually removes the method entirely. It also removes any calls to the method. When you execute the code, no check is made for an implementation because there is no call to check. This results in a slight — but nevertheless significant — improvement in performance. As with partial classes, partial methods are useful when it comes to customizing autogenerated or designer - created code. The designer may declare partial methods that you can choose to implement or not depending on the situation. If you don ’ t implement them, you incur no performance hit because effectively the method does not exist in the compiled code. 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.
如果你的方法只是执行一下不需要值!比如说你这个方法需要显示一句话,你大可以Console.writeline("");不需要给他返回值!
不知道我有没有说明白!反正这个返不返回值 是你决定的 你想让他返回他就返回 不想让他返回就不返回
definition without a method body, and implemented in another partial class definition. In both places,
the partial keyword is used:
public partial class MyClass
{
partial void MyPartialMethod();
}
public partial class MyClass
{
partial void MyPartialMethod()
{
// Method implementation
}
}
Partial methods can also be static, but they are always private and can ’ t have a return value. Any
parameters they use can ’ t be out parameters, although they can be ref parameters. They also can ’ t use
the virtual , abstract , override , new , sealed , or extern modifier.
Given these limitations, it is not immediately obvious what purpose partial methods fulfill. In fact, they
are important when it comes to code compilation, rather than usage. Consider the following code:
public partial class MyClass
{
partial void DoSomethingElse();
public void DoSomething()
{
Console.WriteLine(“DoSomething() execution started.”);
DoSomethingElse();
Console.WriteLine(“DoSomething() execution finished.”);
}
}
public partial class MyClass
{
partial void DoSomethingElse()
{
Console.WriteLine(“DoSomethingElse() called.”);
}
}Here, the partial method DoSomethingElse is defined and called in the first partial class definition,
and implemented in the second. The output, when DoSomething is called from a console application, is
what you might expect:
DoSomething() execution started.
DoSomethingElse() called.
DoSomething() execution finished.
If you were to remove the second partial class definition or partial method implementation entirely (or
comment out the code), the output would be as follows:
DoSomething() execution started.
DoSomething() execution finished.
You might assume that what is happening here is that when the call to DoSomethingElse is made, the
runtime discovers that the method has no implementation and therefore continues executing the next
line of code. What actually happens is a little subtler. When you compile code that contains a partial
method definition without an implementation, the compiler actually removes the method entirely. It also
removes any calls to the method. When you execute the code, no check is made for an implementation
because there is no call to check. This results in a slight — but nevertheless significant — improvement in
performance.
As with partial classes, partial methods are useful when it comes to customizing autogenerated or
designer - created code. The designer may declare partial methods that you can choose to implement or
not depending on the situation. If you don ’ t implement them, you incur no performance hit because
effectively the method does not exist in the compiled code.
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.
在类中只能是 private ,类外部是无法对 Partial Method 进行访问的
编译器设计者不知道如何处理特别的情况
语法规定分部方法声明必须返回void