SoFunction
Updated on 2025-03-08

The role of partial keywords in C#

1. What is a local type?

C# 2.0 introduces the concept of local types. Local types allow us to divide a class, structure, or interface into several parts, and implement them in several different .cs files.

Local types are applicable to the following situations:

(1) The type is particularly large and should not be implemented in one file.

(2) Part of the code in a type is code generated by automation tools and should not be mixed with code we write ourselves.

(3) Multiple people need to cooperate to write a class.

Local types are compiled and processed in a pure language layer and do not affect any execution mechanism. In fact, when the C# compiler compiles, it will still merge the local types of each part into a complete class.

  public partial class Program
  {
   static void Main(string[] args)
   {
   }
  }
  partial class Program
  { 
   public void Test()
   { 
   }
  }

2. Limitations of local types

(1) Local types are only applicable to classes, interfaces, and structures, and do not support delegates and enumerations.

(2) Each part of the same type must have a modifier partial.

(3) When using local types, each part of a type must be in the same namespace.

(4) Each part of a type must be compiled simultaneously.

3. Notes on local types

(1) The keyword partial is a context keyword, and it only has the meaning of a keyword when it is put together with class, struct, and interface. Therefore, the introduction of partial will not affect variables named partial in existing code.

(2) The parts of the local type are generally open in several different .cs files, but the C# compiler allows us to put them in the same file.

4. Application characteristics of local types

Characteristics on local types have an "accumulative" effect.

[Attribute1, Attribute2("Hello")]
partial class Class1{}
[Attribute3, Attribute2("Exit")]
partial class Class1{}

Equivalent to

[Attribute1, Attribute2("Hello"), Attribute3, Attribute2("Exit")]
class Class1 {}

Note: The Attribute2 attribute allows multiple use on the class.

5. Modifiers on local types

(1) Access modifiers on various parts of a type must maintain consistency.

(2) If a partial class uses the abstract modifier, the entire class will be treated as an abstract class.

(3) If a partial class uses the sealed modifier, the entire class will be treated as a sealed class.

(4) Each part of a class cannot use conflicting modifiers, such as the use of abstract on one part and sealed on another part.

(5) If a partial class uses a static modifier, the entire class will be treated as a static class.

6. Base classes and interfaces for local types

(1) The base classes specified on each part of a type must be consistent. A part may not specify a base class, but if specified, it must be the same.

(2) Interfaces on local types have an "accumulative" effect.

partial class Class2: Iinterface1, Iinterface2 {}
partial class Class2: Iinterface3 {}
partial class Class2: Iinterface2 {}

Equivalent to

class Class2: Iinterface1, Iinterface2, Iinterface3 {}

The above is the role of the partial keyword in C# introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!