A partial method (like a partial class) represents code you write to be added as a specific method to a given class upon compilation. This enables the author of a partial class to define an empty method stub and then call that method from other places within the class. If you provide implementation code for the partial method stub, your code is called when the stub would be called (actually the compiler merges your code with the partial class into a single class). If you do not provide a partial method implementation, the compiler goes a step further and removes the method from the class along with all calls to it. This is why such as partial method returns void
and cannot take out
parameters.
The partial method (and partial class) was created to aid in code generation and should generally be avoided unless you are writing code generators or working with them because they can cause confusion in your code.
Of course, Visual Studio has more and more code generation built in. Therefore, it is likely you will run into partial methods sooner or later. In most cases, a code generator or designer (such as LINQ to SQL) generates a partial class and perhaps one or more partial methods. The Partial
keyword modifier defines both partial classes and partial methods. If you are working with generated code, you are often given a partial class that allows you to create your own portion of the class (to be merged with the code-generated version at compile time). In this way, you can add your own custom business logic to any partial method defined and called by generated code.
Let’s look at an example. The following represents an instance of a partial class Employee
. Here there is a single property called Salary
. In addition, there is a method marked Partial
called SalaryChanged
. This method is called when the value of the Salary
property is modified.
C#
partial class Employee {
double _salary;
public double Salary {
get {
return _salary;
}
set {
_salary = value;
SalaryChanged();
}
}
partial void SalaryChanged();
}
VB
Partial Class Employee
Private _salary As Double
Property Salary() As Double
Get
Return _salary
End Get
Set(ByVal value As Double)
_salary = value
SalaryChanged()
End Set
End Property
Partial Private Sub SalaryChanged()
End Sub
End Class
The preceding code might represent code that was created by a code generator. The next task in implementing a partial method then is to define another partial Employee
class and provide behavior for the SalaryChanged
method. The following code does just that.
C#
partial class Employee
{
partial void SalaryChanged()
{
double newSalary = this.Salary;
//do something with the salary information ...
}
}
VB
Partial Class Employee
Private Sub SalaryChanged()
Dim newSalary As Double = Me.Salary
'do something with the salary information ...
End Sub
End Class
When the compiler executes, it replaces the SalaryChanged
method with the new partial method. In this way, the initial partial class (potentially code generated) made plans for a method that might be written without knowing anything about that method implementation. If you decide to write it, it is called at the appropriate time. However, it is optional. If you do not provide an implementation of the partial method SalaryChanged
, the compiler strips out the method and the calls to the method (as if they had never existed). This provides similar services to the virtual
/override
mechanisms presented earlier in this chapter.