Why partial classes




















Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Previous C Static Class. Next Partial Methods in C. Recommended Articles. Article Contributed By :. As we answered right at the beginning of the post, the main use case for the partial class is to allow developers to extend automatically generated code.

In short, being able to edit automatically generated classes is somewhat tricky. The way developers solved this problem before partial classes were suboptimal. Then they would just edit the area of the class that was clearly marked as so. Likewise, when automatic generation kicked in, it was supposed to only replace the area defined for that.

It was way too easy for a developer to add code to the wrong area and thus lose their work when code generation ran. Partial classes remove the pain from those scenarios. By having the same type split into different files, you can easily prevent developers from editing the wrong files. In other words, you can prevent bugs and loss of productivity. Hopefully, you were also glad to learn a little bit more about it by reading this post. Thanks for the read and see you next time!

Carlos Schults is a. You can read more from Carlos at carlosschults. Measure quality with metrics, generate diagrams and enforce decisions with code rules, right in Visual Studio. I have in the past come across another use case where a partial class was exactly what was needed. There was a particular application class that was undergoing two streams of work. One team of developers was adding new functionality, whilst another team were performing heavy refactoring to the existing code.

Without careful management there was likely to be complex merge issues. The use of a partial class in this case was an elegantly simple solution. By temporarily splitting the class into two partial class files both teams were able to work fairly independently. Once the first team had finished the two files were recombined. A partial class splits the definition of a class over two or more source.

You can create a class definition in multiple physical files but it will be compiled as one class when the calsses are compiled. Suppose you have a "Person" class. That definition is divided into the two physical source files, Person1. Then these two files have a class that is a partial class. You compile the source code then create in a single class. Let's see that in Figure 1.

Figure 1. You can separate UI design code and business logic code so that it is easy to read and understand. For example you are developing an web application using Visual Studio and add a new web form then there are two source files, "aspx. These two files have the same class with the partial keyword. The ". When working with automatically generated source, the code can be added to the class without having to recreate the source file.

Many of the articles found on Google talked about the concept of partial classes and partial methods, but very few highlighted what scenarios to use them in. In this article, we will first start with the fundamentals of partial classes and methods and then discuss real-time use. I have also created a video on partial classes and shown the real-time use of it.

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 following figure we have the customer class divided into two different files "customer1. 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. Fundamentals of partial methods There is one more important concept in partial classes called partial methods. Partial methods help us to define a method definition in one of the physical files and we can implement that method in the other physical files as shown in the following figure. In the following figure, you can see we have defined the "Validate" method in "Customer1.

Please note the partial keywords attached to both of these methods. Use number 1: ASP.



0コメント

  • 1000 / 1000