Home > Backend Development > C#.Net Tutorial > Detailed explanation of C# partial keyword

Detailed explanation of C# partial keyword

高洛峰
Release: 2016-12-13 11:48:05
Original
1327 people have browsed it

When we create a new Windows Form, the background code will automatically add the following code:

1 public partial class Form1 : Form
2 {
3 public Form1()
4 {
5 InitializeComponent();
6 }
7}

Why does class Form1 need to be modified with partial? Where are partial mainly used?

Why is the newly created Winows Form class defined as a partial class. When we open the Form1.Designer.cs file, we can see that another part of the original Form1 category was designed in this file. This partial class defines the controls, event delegates, and Dispose methods we use. Because the code here is automatically generated, it is designed as a partial class.

Partial means partial type. Allows us to divide a class, structure or interface into several parts and implement them in several different .cs files. The C# compiler will still merge the local types of each part into a complete class during compilation.

Partial types are suitable for the following situations:
(1) The type is extremely large and should not be implemented in one file.
(2) Part of the code in a type is code generated by automated tools and should not be mixed with code written by ourselves.
(3) Multiple people need to collaborate to write a class.

Limitations of local types
(1) Local types are only applicable to classes, interfaces, and structures, and do not support delegation and enumeration.
(2) Each part of the same type must have the modifier partial.
(3) When using local types, each part of a type must be in the same namespace.
(4) All parts of a type must be compiled at the same time.

3. Notes on partial types

(1) The keyword partial is a contextual keyword, which only has the meaning of the keyword when placed together with class, struct, and interface. Therefore, the introduction of partial will not affect variables named partial in existing code.
(2) Each part of the local type is usually placed 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 a "cumulative" effect.

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

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


is equivalent to

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


Note: The Attribute2 attribute is allowed to be used multiple times on a class.

5. Modifiers on local types

(1) Access modifiers on various parts of a type must maintain consistency.
(2) If one part of a type uses the abstract modifier, then the entire class will be considered an abstract class.
(3) If a part of a type uses the sealed modifier, then the entire class will be considered a sealed class.
(4) Each part of a class cannot use contradictory modifiers. For example, you cannot use abstract on one part and sealed on another part.

6. Base classes and interfaces of local types

(1) The base classes specified on each part of a type must be consistent. A section may not specify a base class, but if specified, it must be the same.
(2) Interfaces on local types have a "cumulative" effect.

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


is equivalent to

class Class2: Iinterface1, Iinterface2, Iinterface3 {}


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template