C#2.0 New Features 01 Partial Class and Partial Method

C#2.0 New Features 01 Partial Class and Partial Method

Division type

  Split the definition of a class , a structure , an interface, or a method into two or more files. Each source file contains part of the type or method definition, and all parts will be combined when the application is compiled.

Division class

The classification definition needs to be broken down in the following situations:

  • When dealing with large projects, distributing a class in multiple independent files allows multiple programmers to process the class at the same time.
  • When using automatically generated source files, you can add code without recreating the source files. Visual Studio uses this method when creating Windows Forms, Web service wrapper code, etc. You can create code that uses these classes so that you do not need to modify the files generated by Visual Studio.
  • To split the classification definition, use the partial keyword modifier, as shown below:
public partial class Employee
{
    public void DoWork()
    {
    }
}

public partial class Employee
{
    public void GoToLunch()
    {
    }
}

partialThe keyword indicates that other parts of the class, structure, or interface can be defined in the namespace. All parts must use partialkeywords. At compile time, each part must be available to form the final type. All parts must have the same accessibility, such as public, privateetc.

If any part is declared as abstract, the entire type is considered abstract. If any part is declared as sealed, the entire type is considered sealed. If any part declares a base type, the entire type will inherit that class.

All parts of the specified base class must be consistent, but the part that ignores the base class still inherits the base type. Each part can specify different base interfaces, and the final type will implement all the interfaces listed in all the division declarations. Any class, structure, or interface member declared in a division definition can be used by all other divisions. The final type is the combination of all parts at compile time.

The partial modifier cannot be used in delegate or enumeration declarations.

The following example demonstrates that nested types can be partial, even if the type in which they are nested is not itself partial.

class Container
{
    partial class Nested
    {
        void Test() {}
    }
    partial class Nested
    {
        void Test2() {}
    }
}

The attributes defined by the partial type are merged at compile time. Take the following statement as an example:

[SerializableAttribute]
partial class Moon {}

[ObsoleteAttribute]
partial class Moon {}

They are equivalent to the following declarations:

[SerializableAttribute]
[ObsoleteAttribute]
class Moon {}

The following will be merged from all division type definitions:

  • XML comments
  • interface
  • Generic type parameter attributes
  • class characteristics
  • member

Take the following statement as an example:

partial class Earth: Planet, IRotate {}
partial class Earth: IRevolve {}

They are equivalent to the following declarations:

class Earth: Planet, IRotate, IRevolve {}

limit

The following rules need to be followed when processing partial class definitions:

  • To all partial type defined as parts of the same type of use must partialbe modified. For example, the following class declaration will generate an error:
public partial class A {}
//public class A {}//error, partial decoration must also be used
  • partialModifiers can only appear next to keywords class, structorinterface previous position.
  • Nested division types are allowed in division type definitions, as shown in the following example:
partial class ClassWithNestedClass
{
    partial class NestedClass {}
}

partial class ClassWithNestedClass
{
    partial class NestedClass {}
}
  • All partial type definitions that are to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Division definitions cannot span multiple modules.
  • The class name and generic type parameter must match in all partial type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
  • The following keywords used in segment type definitions are optional, but if a keyword appears in a segment type definition, the keyword cannot conflict with keywords specified in other segment definitions of the same type :

For details, see Constraints on type parameters .

Example 1

The following example defines a partial class declared Coordsfields and constructors class members in another branch declaration class definitionPrintCoords .

public partial class Coords
{
    private int x;
    private int y;

    public Coords(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

public partial class Coords
{
    public void PrintCoords()
    {
        Console.WriteLine("Coords: {0},{1}", x, y);
    }

}

class TestCoords
{
    static void Main()
    {
        Coords myCoords = new Coords(10, 15);
        myCoords.PrintCoords();

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
//Output: Coords: 10,15

Example 2

As you can see from the example below, you can also develop branch structures and interfaces.

partial interface ITest
{
    void Interface_Test();
}

partial interface ITest
{
    void Interface_Test2();
}

partial struct S1
{
    void Struct_Test() {}
}

partial struct S1
{
    void Struct_Test2() {}
}
Reference: https://cloud.tencent.com/developer/article/1507935 C#2.0 New Features 01 Division Class and Division Method-Cloud + Community-Tencent Cloud