Fundamentals of partial classes
A partial class allows a single class to be divided into two separate physical files. During compile time, these files get compiled into a single class. For instance, you can see in the below figure we have the customer class divided into two different files “customer1.cs” and “customer2.cs”.
During compilation, these files get compiled into a single class internally. So when you create an object of the
Customer
class, you will be able to see methods lying in both the physical files. For instance, you can see the Add
method belongs to customer1.cs and the Delete
method belongs to customer2.cs, but when the Customer
object is created, we can see both the Add
and Delete
methods.Fundamentals of partial methods
There is one more important concept in partial classes called partial methods. Partial methods helps us to define a method in one physical file and we can implement that method in another physical file, as shown in the below figure.
In the figure, you can see we have defined the
Validate
method in Customer1.cs and this Validate
method is implemented in Customer2.cs. Please note the partial
keywords attached to both of these methods.
l
No comments:
Post a Comment