Home > Backend Development > C#.Net Tutorial > The role of partial keyword in C#

The role of partial keyword in C#

高洛峰
Release: 2016-12-13 11:46:03
Original
1178 people have browsed it

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.

Partial types are suitable for 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 automated tools and should not be mixed with code written by ourselves.
(3) Multiple people need to collaborate to write a class.

Local types are a pure language layer compilation process that does not affect any execution mechanism - in fact, the C# compiler will still merge the local types of each part into a complete class during compilation.

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

partial class Program
{
public void Test()
{ }
}

2. Partial Type restrictions

(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, 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. Points to note about 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 a 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 features of local types

Features 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 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 considered an abstract class.
(3) If a partial class uses the sealed modifier, 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.
(5) If a partial class uses the static modifier, the entire class will be considered a static class.

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