The Role of partial Keyword in C

  • 2021-12-11 08:39:26
  • OfStack

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, which are implemented in several different. cs files.

Local types are suitable for the following situations:

(1) The type is very large, so it is not suitable to put it in one file.

(2) One part of the code in one type is generated by automation tools, which should not be mixed with the code written by ourselves.

(3) It is necessary for many people to cooperate to write a class.

Local types are a pure language-level compilation process that does not affect any execution mechanism-in fact, the C # compiler will still merge the local types of various parts into a complete class when compiling.


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

2. Restrictions on 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 the modifier partial.

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

(4) All parts of a type must be compiled at the same time.

3. Attention points of local types

(1) The keyword partial is a context keyword, which has the meaning of keyword only when it is placed with class, struct and interface. Therefore, the introduction of partial does not affect the variable named partial in the existing code.

(2) Parts 1 of a local type are generally split into 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 "accumulation" 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 uses on the class.

5. Modifiers on local types

(1) Access modifiers on various parts of a type must remain unisex.

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

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

(4) You can't use contradictory modifiers in parts of a class, such as abstract on one part and sealed on another.

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

6. Base classes and interfaces for local types

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

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


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

Equivalent to


class Class2: Iinterface1, Iinterface2, Iinterface3 {}

Related articles: