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 allows the author of a partial class to define a 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 gets 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 definition, the compiler goes a step further and removes the method from the class along with all calls to it.

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.

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

C#
partial class Employee {

double _salary;

public double Salary {
get {
return _salary;
}
set {
_salary = value;
SalaryChanged();
}
}

partial void SalaryChanged();
}

The former code might represent code that was created by a code generator. The next task in implementing a partial method then is to create another partial Employee class and provide behavior for the SalaryChanged method. The following code does just that:

VB
Partial Class Employee
Private Sub SalaryChanged()
Dim newSalary As Double = Me.Salary
‘do something with the salary information ...
End Sub
End Class

C#
partial class Employee {
partial void SalaryChanged() {
double newSalary = this.Salary;
//do something with the salary information ...
}
}

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. If you decide to write it, then it gets 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 had never existed.

Source of Information : Sams Microsoft Visual Studio 2008 Unleashed

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner