The role of the partial keyword in C #

Source: Internet
Author: User
Tags modifiers
1. What is a local type?

C # 2.0 introduces the concept of a local type. Local types allow us to divide a class, struct, or interface into several parts, implemented in several different. cs files, respectively.

Local types are suitable for the following situations:

(1) The type is particularly large, should not be placed in a file to achieve.
(2) A part of the code for a type is generated by the automation tool, and it is not appropriate to mix it with the code we write ourselves.
(3) More people are required to cooperate in the preparation of a class.

A local type is a purely language-level compilation process that does not affect any execution mechanism-in fact, the C # compiler still merges the local types of parts into a complete class at compile time.

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

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


2. Local type restrictions

(1) Local types are only applicable to classes, interfaces, structs, and delegates and enumerations are not supported.
(2) Each part of the same type must have a modifier partial.
(3) When using a local type, each part of a type must be in the same namespace.
(4) Each part of a type must be compiled at the same time.

3. Local type of attention point

(1) The keyword partial is a contextual keyword that only has the meaning of the keyword when it is put together with class, struct, interface. Therefore, the introduction of partial does not affect variables with the name partial in the existing code.
(2) Parts of a local type are generally separated into several different. cs files, but the C # compiler allows us to place them in the same file.

4. Application characteristics of local types

The attribute on the local type has an "additive" effect.

[Attribute1, Attribute2 ("Hello")]
Partial class class1{}

[Attribute3, Attribute2 ("Exit")]
Partial class class1{}


Equivalent

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


Note: The Attribute2 property allows multiple use on a class.

5. Modifiers on a local type

(1) Access modifiers on the various parts of a type must maintain consistency.
(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) Each part of a class cannot use conflicting modifiers, such as the inability to use abstract on one part, and the use of sealed on another.
(5) If a partial class uses the static modifier, the entire class is treated as a static class.


6. Base classes and interfaces for local types

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

Partial class Class2:iinterface1, Iinterface2 {}
Partial class Class2:iinterface3 {}
Partial class Class2:iinterface2 {}


Equivalent

Class Class2:iinterface1, Iinterface2, Iinterface3 {}

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.