Description and use of the partial keyword

Source: Internet
Author: User
Tags dot net modifiers
ArticleDirectory
    •  
Category: ASP Dot Net Read by 9258 Comment (29) Favorites Report

PartialThe New Keyword provided by the keyword C #2.0 is used to split the definition of a class, struct, or interface and write it in different source files. Each source file contains a part of the class definition and compiles the application.ProgramAll parts are combined. In the following situations, you need to split the classification definition:

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

2.When an automatically generated source is used, you do not need to re-create the source fileCodeAdd to class. Visual Studio uses this method when creating Windows Forms and Web Service packaging code. You can create code that uses these classes without editing the files created by Visual Studio.

The usage is as follows:[Modifiers] partial type

ModifierThis includes abstract new override virtual static extern and one of the four access modifiers (Public Private protected internal.TypeIncluding one of the class struct interfaces.

See the following definition.:
Public   Partial   Class Employee
{
Public VoidDowork ()
{}
}
Public   Partial   Class Employee
{
Public VoidGotolunch ()
{}
}

The code above defines a classEmployeeThis class has two methodsDowork ()AndGotolunch ()You can view the above Code as follows:In fact, the compiler is also merged during compilation. The compiler will put all the information of a partial class together during compilation, so we can expand it.Suppose we have already defined two interfaces:Our class is defined as follows:This is necessary when developing distributed systems. Multiple programmers only need to develop their own parts.

Public   Class Employee
{
Public VoidDowork ()
{}
Public VoidGotolunch ()
{}
}

 

Interface Idosomething
{
VoidDowork ();
VoidDoplay ();
}

Interface Igotodinner
{
VoidGotobreakfast ();
VoidGotolunch ();
VoidGotosupper ();
}

 

Public   Partial   Class Employee: idosomething
{
Public VoidDowork ()
{}

Public VoidDoplay ()
{}
}

Public   Partial   Class Employee: igotodinner
{
Public VoidGotobreakfast ()
{}


[System. obsoleteattribute]
Public VoidGotolunch ()
{}

Public VoidGotosupper ()
{}
}

Therefore, the compiler combines the above into a class during compilation:

Public   Class Employee: idosomething, igotodinner
{
Public   Void Dowork ()
{}

Public   Void Doplay ()
{}

Public   Void Gotobreakfast ()
{}


[System. obsoleteattribute]
Public   Void Gotolunch ()
{}

Public   Void Gotosupper ()
{}
}

 

Note the following points: 1. Partial must be used to modify all partial type definitions of each part of the same type. The following error occurs: Public partial class {} // Public class A {}// error, must also be marked partial 2. Partial The modifier can only appear next to the class, struct, or interface keyword. 3. You can use nested segment types in the division type definition. Of course, if the nested class is also a partial class, it also complies with the above rules. For example:
Partial   Class Classwithnestedclass
{
Partial ClassNestedclass{}
}

Partial   Class Classwithnestedclass
{
Partial ClassNestedclass{}
}
4.  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. 5.  the class name and generic type parameters must be matched in all segment type definitions. Generic types can be segments. Each division declaration must use the same parameter name in the same order.

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.