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

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

User Comments

Title: sizar   
Name: sizar
Date: 2012-12-01 1:43:44 AM
please check this website for more information http://fantasyaspnet.blogspot.in/2012/11/write-program-in-c-to-implement.html
Title: 1   
Name: 1
Date: 2012-11-15 6:38:39 AM
Title: *   
Name: *
Date: 2012-09-06 9:38:19 AM
Title: partial class   
Name: Bijay
Date: 2012-09-04 12:52:54 PM
Thanks ,but not enough.
Title: Partial Class   
Name: Manoj
Date: 2012-08-09 9:19:03 PM
good . Easy to understand !!!
Title: w   
Name: w
Date: 2012-07-02 6:25:22 AM
Title: sfd   
Name: sdf
Date: 2012-06-12 5:14:19 PM
Title: Awesome   
Name: Vinod
Date: 2012-05-15 1:07:46 AM
Very Good and helpfull
Title: thanks   
Name: ajith
Date: 2011-08-26 7:54:19 AM
super discription
Title: thanks   
Name: shaheen
Date: 2011-01-13 1:13:12 AM
Title: good   
Name: smithu valsalan
Date: 2011-01-13 1:07:37 AM
very useful article
Title: kumar   
Name: umesh
Date: 2010-12-16 2:36:39 AM
Title: Excellant Artical   
Name: Haribabu Nimmagadda
Date: 2010-07-22 7:58:00 AM
It helps me alot.
Title: Good one!   
Name: Lakshmi Sudheendra
Date: 2010-05-26 8:53:17 AM
Very useful article. Described in simple words and optimally covered the essential topics.
Title: excellent   
Name: vasudev
Date: 2010-05-22 6:10:46 AM
excellent jestification
Title: I like the justification   
Name: george
Date: 2010-02-24 10:20:43 AM
I was aware of the autogenerated code case, but code merging is the real day to day convenience this feature provides.
Title: very good explanation   
Name: krishna kishore
Date: 2010-02-16 9:29:59 AM
your explanation is very clear. and simple
Title: Wonderfull   
Name: s.arul
Date: 2010-02-09 8:17:46 AM
Wonderful article for asp.net beginers
Title: Good   
Name: Sanjeev
Date: 2010-02-09 5:29:54 AM
Here in ur article u have described enough.. but plz try to add more examples so it will clear the doubts of readers.. all the best
Title: Good   
Name: Laxmi
Date: 2009-12-22 8:04:24 AM
Its good but I have a question.I have a class which is splitted into two partial classes.These two partial classes have same methods.what happens at compile time.Please help me.thanks in advance..
Title: Nice Article   
Name: Senthil
Date: 2009-11-02 6:07:40 AM
Very Nice Article!!
Title: Excellent !!   
Name: Sai
Date: 2009-10-30 3:10:32 AM
Nice article for beginners.
Title: partial class   
Name: vikash kumar
Date: 2009-10-15 6:07:41 AM
Article written by you is good but not enough.pls modify it.
Title: Great little read   
Name: Piao!
Date: 2009-09-18 11:34:43 AM
Good writing style, and nice real world examples - cheers man!
Title: Nice Article   
Name: sam
Date: 2009-09-09 7:42:56 AM
nice article Enrick. Can you suggest me some article on delegates & events?
Title: Good Article   
Name: Rajender Kumar
Date: 2009-08-28 12:49:41 AM
i am a web developer Asp.Net C#. i have read this article, that is well understandig for beginners.
Thanks for taking time for this post.
Title: Thank you   
Name: phil
Date: 2009-07-01 8:01:46 PM
Thanks for posting this. I despise .net but I have to do it at work so its nice to find articles that teach the concepts rather than syntax.
Title: confuseeeeeeeeee   
Name: sagar
Date: 2009-01-29 1:48:30 AM
Hi.. i am cleared abt partial class in c#.... but u r article mix with aspx... ca u clarify this
Title: partial classes   
Name: shraddha
Date: 2009-01-13 4:12:44 AM
very good article
Title: I am heap to get you   
Name: zinabu Alemayehu
Date: 2008-11-08 10:00:40 AM
I need some help from you based on the asp,c#,and vb.if you get full of code about some project please send to me via my email.

I always Thank u.........
Title: Thank you   
Name: Shawn F.
Date: 2008-09-23 10:55:07 AM
Thanks for taking the time to post this. It helps clear things up.

BTW...You're able to describe things well enough for me.

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

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-06-15 2:34:38 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search