[c#] When is it appropriate to use C# partial classes?

Partial classes span multiple files.

How can you use the partial modifier on a C# class declaration?

With partial classes, you can physically separate a class into multiple files. This is often done by code generators.

Example

With normal C# classes, you cannot declare a class in two separate files in the same project. But with the partial modifier, you can.

This is useful if one file is commonly edited and the other is machine-generated or rarely edited.

Here's an example to clarify:

class Program
{
    static void Main()
    {
        A.A1();
        A.A2();
    }
}

Contents of file A1.cs: C#

using System;

partial class A
{
    public static void A1()
    {
        Console.WriteLine("A1");
    }
}

Contents of file A2.cs: C#

using System;

partial class A
{
    public static void A2()
    {
        Console.WriteLine("A2");
    }
}

Output:

A1
A2

Partial is required here.

If you remove the partial modifier, you will get an error containing this text:

[The namespace '<global namespace>' already contains a definition for 'A'].

Tip:

To fix this, you can either use the partial keyword, or change one of the class names.

How does the C# compiler deal with partial classes?

If you disassemble the above program (using IL Disassembler), you will see that the files A1.cs and A2.cs are eliminated. You will find that the class A is present.

Class A will contain the methods A1 and A2 in the same code block. The two classes were merged into one.

Compiled result of A1.cs and A2.cs: C#

internal class A
{
    // Methods
    public static void A1()
    {
        Console.WriteLine("A1");
    }

    public static void A2()
    {
        Console.WriteLine("A2");
    }
}

Summary

  • Partial classes can simplify certain C# programming situations.
  • They are often used in Visual Studio when creating Windows Forms/WPF programs.
  • The machine-generated C# code is separate.
  • Or You could find the whole description here.

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to architecture

Single Page Application: advantages and disadvantages Dilemma: when to use Fragments vs Activities: What is the technology behind wechat, whatsapp and other messenger apps? Design Documents (High Level and Low Level Design Documents) A potentially dangerous Request.Form value was detected from the client Is Django for the frontend or backend? How should a model be structured in MVC? When to Redis? When to MongoDB? I just discovered why all ASP.Net websites are slow, and I am trying to work out what to do about it When is it appropriate to use C# partial classes?