C # Partial partial type

Source: Internet
Author: User

  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. 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.
  2. Application characteristics of local types: the attributes on the local type have 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.

  3. (1) Access modifiers on the various parts of a type must maintain consistency.
    (2) If a type has a section that uses the abstract modifier, the entire class is treated as an abstract class.
    (3) If a type has a section that 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.
  4. 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 {}

  5. It is a good programming habit to keep all the source code as a type in a separate file, but sometimes a type becomes too large to be an unrealistic constraint. In addition, programmers often use source code generators to produce an initial structure of an application, and then modify the resulting code. Unfortunately, when the source code is released again sometime in the future, the existing changes will be overwritten.

    The Partial types allows classes, structs, and interfaces to be broken into different source code files for multiple fragment fragments in order to simplify development and maintenance. Plus the partial types allows machine-generated and user-written parts to be separated, which makes it easy to add code generated by the tool.

    Partial, is a new type modifier that is used when a type is defined in multiple parts. The following is an example of a partial class, implemented in two parts. These two parts may be in two different source code files, for example the first part is generated by a database mapping tool, and the second part is hand-written.

    public partial class Customer
    {
    private int id;
    private string name;
    private string address;
    Private list<order> orders;
    Public Customer () {

    }
    }
    public partial class Customer
    {
    public void SubmitOrder (order order) {
    Orders. ADD (order);
    }
    public bool Hasoutstandingorders () {
    return orders. Count > 0;
    }
    }


    When the above two sections are compiled together, the resulting code is as if the class were written in a single unit.

    public class Customer
    {
    private int id;
    private string name;
    private string address;
    Private list<order> orders;
    Public Customer () {

    }
    public void SubmitOrder (order order) {
    Orders. ADD (order);
    }
    public bool Hasoutstandingorders () {
    return orders. Count > 0;
    }
    }


    All parts of a partial type must be compiled together so that all the parts can be combined at compile time. In particular, the partial types is not allowed to join a compiled type

C # Partial partial type

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.