Understanding and Using Partial Classes in C#
page 5 of 8
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 12549/ 344

Implementing Multiple Interfaces with Partial Classes

When you need to implement multiple interfaces, the best way to do this is by using partial classes. If you don't know what interfaces are or how to use them I recommend reading my article which describes how interfaces work and how to use them.

For each interface your class should implement you will have certain code you need to write. When I implement an interface, I will place the code for the interface implementation in a region block. This gives me a small sense of code separation. When implementing multiple interfaces you will want to separate the code for this into separate files and declare the class as partial. This will give you great separation of the interface code and the core class code. This is not very important with just one interface, but if you're going to have more than one it becomes a necessity.

This is a nice solution because you are able to keep each individual interface on a different partial class, and have your core class methods stored in its own partial class as well. This will let you keep the class nice and clean without having to have the extra code from the interfaces in the same file as the code for the class. A lot of times the code for the class is something unimportant to the core functionality of the class. As an example, if you were writing something with the IDisposable interface, the IDisposable data has nothing to do with the rest of your class. This is just code dealing with how to dispose of the class. Since this is the case, when writing the core logic of the class you don't care about the IDisposable data, and if you have another interface you don't care about that one either when writing the code of the class. It is usually an external capability of the class which is not involved in the main functionality of the class.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 6 and 8 and type the answer here:

User Comments

Title: Thank you   
Name: Shawn F.
Date: 9/23/2008 10:55:07 AM
Comment:
Thanks for taking the time to post this. It helps clear things up.

BTW...You're able to describe things well enough for me.
Title: not decribe well   
Name: vivek rai
Date: 5/4/2008 7:21:53 AM
Comment:
u r not able to decribe well....

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 10/13/2008 3:32:26 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search