Partial in C #

Source: Internet
Author: User
Tags modifiers

First look at a code that appears frequently in C #

 Public Partial class form1:form{    public  Form1 ()     {         InitializeComponent ();     }}

The partial is used, we open the Form1.Designer.cs file, we can see that the original Form1 another part of the class was designed in this file. This part of the class defines the controls we use, the event delegates, and so on, such as the Dispose method. Because the code inside is automatically generated, it's designed to be a partial class.

Partial is the meaning of a local type. Allows us to divide a class, struct, or interface into several parts, implemented in several different. cs files, respectively. The C # compiler still merges the local types of parts into one complete class at compile time

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.

local type restriction
(1)   Local type only applies to classes, interfaces, structs, delegates and enumerations are not supported.
(2)   each part of the same type must have a modifier  partial.
(3)   When you use a local type, the parts 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)   keyword partial is a contextual keyword, only and  class, Structs, interface  are put together when the meaning of the keyword. 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 properties for local types
attributes on local types have an "additive" effect.

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


[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 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.

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 {}


class Class2:iinterface1, Iinterface2, Iinterface3 {}

Partial in C #

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: 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.