For those of you interested, i found a very interesting list of features that were introduced in C# in here. This is a very complete list that contains all the features, and i'm explaining them one by one in this post series. We've talked about Generics and Iterators. Now it's time for some partial types.
Thank you for reading, next time i'll talk about nullable types!
A partial type is a type which definition is spread across one or more files. It doesn't have to be in multiple separated files, but can be. This is a very simple concept that can give us many benefits, let's see:
- If a type is partial, multiple developers can work on every part of it. This allows a more organized way of working and can lead to production improvement.
- Winforms, for example, generates a partial class for the form so that the client can separately edit other parts it. This way, a part contains information about the design and the other contains the logic of the form. In fact, this is a very spread pattern across .Net. Entity Framework uses this too. The T4 Template by default creates partial classes for each entity and then we can create other parts of the same class to include our business logic without worrying with it getting overwritten.
- There is virtually no performance penalty in using partial types, because at compile time the parts will be unified. So, this is really just a convenience feature.
Below are some inconveniences of using partial types:
- A partial type generally has it's logic or definition spread across it's parts, so it can be difficult to find the piece of code you want (this is arguably a bad argument, because today's IDE's overcome this problem.).
- When using a partial type, sometimes may become hard to know every attribute a class implements, or every interface (also, the same as before).
To be clear, a partial type is always the sum of it's attributes, implemented interfaces, generic type parameters, documentation, etc. Let's see an example:
This is equivalent to:
To use a partial type, keep in mind that the partial declarations must obviously be in the same namespace.Thank you for reading, next time i'll talk about nullable types!
Comments
Post a Comment