Partial classes and methods

Partial classes and methods

In this article, I'm going to explain what partial classes and partial methods are, and how to implement them in C#.

In C#, the identifier used to identify a class must be unique, however, when working on large scale projects, it might be necessary that multiple developers work on the same class or methods at the same time. For this reason, Microsoft introduced the keyword partial and added this new feature in C# 2.0.

The keyword partial allows splitting a class definition into different parts, within the same namespace, which will be combined when the application is compiled.

In order to use this feature each part has to:

  • use the partial keyword.
  • be available at compile time.
  • have the same access modifier (public, private, protected and internal)
  • be in the same namespace as the other parts.

It's also important to mention that:

  • If even one part is declared abstract, then the whole type is considered abstract.
  • If even one part is declared sealed, then the whole type is considered sealed.
  • If even one part declares a base type, then the whole type inherits that class (even parts that omit a base class still inherit the base type).
  • Any class, struct, or interface members declared in a part are available to all the other parts.

For example, if we consider the following parts:

PartialClassFirstFile.cs

partial class Earth : Planet, IRotate 
{
    public void CalcolateMass() { ... } 
}

PartialClassSecondFile.cs

partial class Earth : IRevolve 
{ 
    public void CalcolateSpeed() { ... } 
}

Once the compilation is over, the equivalent class is:

class Earth : Planet, IRotate, IRevolve 
{ 
    public void CalcolateMass() { ... }
    public void CalcolateSpeed() { ... }
}

The partial keyword can also be applied to methods. Its behavior is very similar to the classes, in fact, a class can contain the signature of the method and another part can supply its implementation. Both parts will be then united at compile time.

If no implementation is provided, the compiler will optimize the code by removing both method and all calls at compile time.

It's import to mention that partial method:

  • must return void.
  • can have in or ref but not out parameters (out params must be assigned and since a partial method might be not implemented, the use of this kind of parameters is not allowed).
  • are implicitly private (consequently cannot be virtual).
  • can have static and unsafe modifiers.
  • can be generics.

In the following example, we will add a little more code to the previous code.

PartialClassFirstFile.cs

partial class Earth : Planet, IRotate 
{
    public void CalcolateMass() { ... } 
    public partial void CalcolateVolume() { } 
    public partial void CalcolateForce() { } 
}

PartialClassSecondFile.cs

partial class Earth : IRevolve 
{ 
    public void CalcolateSpeed() { ... }
    public partial void CalcolateVolume() { ... } 
}

In this case, the class Earth, implemented in the file PartialClassFisrtFile.cs declares only the method signature of both CalcolateVolume and CalcolateForce. While the method CalcolateVolume is implemented in the relative class in the file PartialClassSecondFile.cs, the other method is not which means that once the compilation is over, the CalcolateForce method will be removed and the final class will be:

class Earth : Planet, IRotate, IRevolve 
{ 
    public void CalcolateMass() { ... }
    public void CalcolateVolume() { ... } 
    public void CalcolateSpeed() { ... }
}

要查看或添加评论,请登录

Ivan Porta的更多文章

  • How to create and configure your bot to work in Microsoft Teams

    How to create and configure your bot to work in Microsoft Teams

    This article is a step-by-step guide on how to create a Bot from scratch using Microsoft Bot Framework and how to…

  • Move your SharePoint On-Premise to Teams

    Move your SharePoint On-Premise to Teams

    With the Covid-19 pandemic, remote working has become a must more than a luxury. Many companies decided to adopt…

  • Continuous testing and integration with GitHub repo and CircleCI

    Continuous testing and integration with GitHub repo and CircleCI

    In this article, I'll show you how to use two patterns that have penetrated all aspects of software development:…

  • How to create a web application using NPM and Docker in 5 minutes or less

    How to create a web application using NPM and Docker in 5 minutes or less

    In this article, I will show first how to build a Docker image and then download it from Docker Hub and use it to…

    3 条评论
  • Sealed modifiers

    Sealed modifiers

    In this article, I will discuss what is a sealed modifier, how to use it and what's its impact on your application's…

  • Data types and memory management in C#

    Data types and memory management in C#

    Before explaining the different data types available in C#, it's important to mention that C# is a strongly-typed…

  • What is a virtual member?

    What is a virtual member?

    We can't talk about virtual members without referring to polymorphism. In fact, a function, property, indexer or event…

  • State Management in ASP.NET

    State Management in ASP.NET

    Whenever you visit a web application, your browser will communicate with the respective server through HTTP or HTTPs…

  • What is CTS?

    What is CTS?

    .NET is language agnostic which allows programmers to write code in different languages (which can be compiled to IL)…

  • DRY principle

    DRY principle

    Do not repeat yourself The idea behind the DRY principle is to reducing repetitions of information and to represent any…

社区洞察

其他会员也浏览了