Partial Class

By | August 8, 2017

Partial class allows us to split the definition of a class or a struct or an interface over two or more source files. Each source file contains a section of the class definition and all the parts are grouped as a single class when the application compiled. So the main advantage is, it will allow more than one developer can work on the same class.

Example: In below code and screenshot, we will break a single class into multiple partial classes.

  public class Service1
 {
 public void Add()
 {
 //some code
 }
 public void Update()
 {
 //some code
 }
 public void Delete()
 {
 //some code
 }
.........
.....
...
 }

Now, Break the above class into partial class so that more than one developer can work on the same class as below screenshot.

 

 

 

We should be careful,when writing code for partial classes

  • All the partial definitions of a class or a struct or an interface must proceeded with the keyword “partial”.
  • The partial types must have the same accessibility.
  • If any part is declared as abstract,then the entire class is abstract.
  • If any part is declared as sealed,then the entire class is sealed.

Briefly, the basic difference between generic and non-generic collections:

Generic collections: These are the collections that can hold data of same type and we can decide what type of data that collections can hold. The advantages of generic collections are Type Safe, Secure, reduced overhead of type conversions.

Non-Generic collections: These are the collections that can hold elements of different data types. It holds all elements as object type. So it includes overhead of type conversions.

 

I hope you will enjoy the use of Partial Class in C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Like it? Share it