C # partial

Source: Internet
Author: User

Partial is a class modifier used to split the class definition into several parts for easy code management, such
Class classa
{
Void (){;}
Void B (){;}
}
And
Partial class classa
{
Void (){;}
}
Partial class classa
{
Void B (){;}
}
Is the same

 

The details are as follows:

 

You can split the definition of a class, structure, or interface into two or more source files. Each source file contains a part of the class definition. All parts are combined during compilation of the application. In the following situations, you need to split the classification definition:

When processing large projects, distributing a class to multiple independent files allows multiple programmers to process the class at the same time.

When an automatically generated source is used, the code can be added to the class without re-creating the source file. Visual Studio uses this method when creating Windows Forms and Web Service packaging code. You can create code using these classes without editing the files created by Visual Studio.

To split the classification definition, use the partial keyword modifier as follows:

C # copy code
Public partial class employee
{
Public void dowork ()
{
}
}

Public partial class employee
{
Public void gotolunch ()
{
}
}

Remarks

The partial keyword indicates that other parts of the class, structure, or interface can be defined in the namespace. All parts must use the partial keyword. During compilation, each part must be used to form the final type. Each part must have the same accessibility, such as public and private.

If any part is declared as abstract, the entire type is considered abstract. If any part is declared as sealed, the entire type is regarded as sealed. If any part is declared as the base type, the entire type inherits the class.

All the parts of the specified base class must be consistent, but the parts that ignore the base class still inherit the base type. You can specify different basic interfaces for each part. The final type will implement all interfaces listed in all segment declarations. Any class, structure, or interface member declared in a segment definition can be used by all other parts. The final type is the combination of all parts during compilation.

Note:
The segment modifier cannot be used in a delegate or enumeration declaration.

Nested types can be segments, even if the types they are nested in are not segments. For example:

C # copy code
Class container
{
Partial class nested
{
Void test (){}
}
Partial class nested
{
Void Test2 (){}
}
}

The property defined by the Division type is merged during compilation. For example, the following statement:

C # copy code
[System. serializableattribute]
Partial Class moon {}

[System. obsoleteattribute]
Partial Class moon {}

It is equivalent:

C # copy code
[System. serializableattribute]
[System. obsoleteattribute]
Class moon {}

The following content will be merged from all the segment type definitions:

XML Annotation

Interfaces

Generic Type parameter attributes

Class attributes

Member

For example, the following statement:

C # copy code
Partial class Earth: Planet, irotate {}
Partial class Earth: irevolve {}

It is equivalent:

C # copy code
Class Earth: Planet, irotate, irevolve {}

Restrictions

The following rules must be followed when processing the definition of a division class:

Partial must be used to modify all partial type definitions of each part of the same type. For example, the following class declaration will generate an error:

C # copy code
Public partial class {}
// Public class A {}// error, must also be marked partial

The partial modifier can only appear next to the class, struct, or interface keyword.

The segment type definition allows the use of nested segment types, for example:

C # copy code
Partial class classwithnestedclass
{
Partial class nestedclass {}
}

Partial class classwithnestedclass
{
Partial class nestedclass {}
}

To define all the sub-departments of different regions in the same region, they must be defined in the same program set and in the same region (.exe or. dll file. The Division definition cannot span multiple modules.

The class name and generic type parameters must match in all the division type definitions. Generic types can be segments. Each division declaration must use the same parameter name in the same order.

The following keywords are optional for the definition of the division type, but if a keyword appears in a division type definition, the keyword cannot conflict with the keyword specified in other segment definitions of the same type:

Public

Private

Protected

Internal

Abstract

Sealed

Base Class

New modifier (nested part)

General constraints (for more information, see type parameter constraints (C # programming guide ).)

Example 1

The following example declares the fields and constructor of the class coords in a partial class definition, and declares the member printcoords in another partial class definition.

C # copy code
Public partial class coords
{
Private int X;
Private int y;

Public coords (int x, int y)
{
This. x = X;
This. Y = y;
}
}

Public partial class coords
{
Public void printcoords ()
{
System. Console. writeline ("coords: {0}, {1}", x, y );
}

}

Class testcoords
{
Static void main ()
{
Coords mycoords = new coords (10, 15 );
Mycoords. printcoords ();
}
}

Output

Coords: 10, 15

Example 2

The following example shows that you can also develop the branch structure and interfaces.

C # copy code
Partial interface itest
{
Void interface_test ();
}

Partial interface itest
{
Void interface_test2 ();
}

Partial struct S1
{
Void struct_test (){}
}

Partial struct S1
{
Void struct_test2 (){}
}

Partial is a class modifier used to split the class definition into several parts for easy code management, such
Class classa
{
Void (){;}
Void B (){;}
}
And
Partial class classa
{
Void (){;}
}
Partial class classa
{
Void B (){;}
}
Is the same

 

The details are as follows:

 

You can split the definition of a class, structure, or interface into two or more source files. Each source file contains a part of the class definition. All parts are combined during compilation of the application. In the following situations, you need to split the classification definition:

When processing large projects, distributing a class to multiple independent files allows multiple programmers to process the class at the same time.

When an automatically generated source is used, the code can be added to the class without re-creating the source file. Visual Studio uses this method when creating Windows Forms and Web Service packaging code. You can create code using these classes without editing the files created by Visual Studio.

To split the classification definition, use the partial keyword modifier as follows:

C # copy code
Public partial class employee
{
Public void dowork ()
{
}
}

Public partial class employee
{
Public void gotolunch ()
{
}
}

Remarks

The partial keyword indicates that other parts of the class, structure, or interface can be defined in the namespace. All parts must use the partial keyword. During compilation, each part must be used to form the final type. Each part must have the same accessibility, such as public and private.

If any part is declared as abstract, the entire type is considered abstract. If any part is declared as sealed, the entire type is regarded as sealed. If any part is declared as the base type, the entire type inherits the class.

All the parts of the specified base class must be consistent, but the parts that ignore the base class still inherit the base type. You can specify different basic interfaces for each part. The final type will implement all interfaces listed in all segment declarations. Any class, structure, or interface member declared in a segment definition can be used by all other parts. The final type is the combination of all parts during compilation.

Note:
The segment modifier cannot be used in a delegate or enumeration declaration.

Nested types can be segments, even if the types they are nested in are not segments. For example:

C # copy code
Class container
{
Partial class nested
{
Void test (){}
}
Partial class nested
{
Void Test2 (){}
}
}

The property defined by the Division type is merged during compilation. For example, the following statement:

C # copy code
[System. serializableattribute]
Partial Class moon {}

[System. obsoleteattribute]
Partial Class moon {}

It is equivalent:

C # copy code
[System. serializableattribute]
[System. obsoleteattribute]
Class moon {}

The following content will be merged from all the segment type definitions:

XML Annotation

Interfaces

Generic Type parameter attributes

Class attributes

Member

For example, the following statement:

C # copy code
Partial class Earth: Planet, irotate {}
Partial class Earth: irevolve {}

It is equivalent:

C # copy code
Class Earth: Planet, irotate, irevolve {}

Restrictions

The following rules must be followed when processing the definition of a division class:

Partial must be used to modify all partial type definitions of each part of the same type. For example, the following class declaration will generate an error:

C # copy code
Public partial class {}
// Public class A {}// error, must also be marked partial

The partial modifier can only appear next to the class, struct, or interface keyword.

The segment type definition allows the use of nested segment types, for example:

C # copy code
Partial class classwithnestedclass
{
Partial class nestedclass {}
}

Partial class classwithnestedclass
{
Partial class nestedclass {}
}

To define all the sub-departments of different regions in the same region, they must be defined in the same program set and in the same region (.exe or. dll file. The Division definition cannot span multiple modules.

The class name and generic type parameters must match in all the division type definitions. Generic types can be segments. Each division declaration must use the same parameter name in the same order.

The following keywords are optional for the definition of the division type, but if a keyword appears in a division type definition, the keyword cannot conflict with the keyword specified in other segment definitions of the same type:

Public

Private

Protected

Internal

Abstract

Sealed

Base Class

New modifier (nested part)

General constraints (for more information, see type parameter constraints (C # programming guide ).)

Example 1

The following example declares the fields and constructor of the class coords in a partial class definition, and declares the member printcoords in another partial class definition.

C # copy code
Public partial class coords
{
Private int X;
Private int y;

Public coords (int x, int y)
{
This. x = X;
This. Y = y;
}
}

Public partial class coords
{
Public void printcoords ()
{
System. Console. writeline ("coords: {0}, {1}", x, y );
}

}

Class testcoords
{
Static void main ()
{
Coords mycoords = new coords (10, 15 );
Mycoords. printcoords ();
}
}

Output

Coords: 10, 15

Example 2

The following example shows that you can also develop the branch structure and interfaces.

C # copy code
Partial interface itest
{
Void interface_test ();
}

Partial interface itest
{
Void interface_test2 ();
}

Partial struct S1
{
Void struct_test (){}
}

Partial struct S1
{
Void struct_test2 (){}
}

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.