C # usage of partial class

Source: Internet
Author: User

Partial Class A indicates that this is only part of Class. I can create a class B. CS. Write partial class A in the code. After the program is compiled. The attributes and methods in two A classes are merged and called members of Class.

The New Keyword provided by the partial 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. All parts are combined during compilation of the application. In the following situations, you need to split the classification definition:


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


2. When using an automatically generated source, you can add the code 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 that uses these classes without editing the files created by Visual Studio.

For example:

In class1.cs:

Namespace vs2012_demo1 {public partial class class1 {public void showmessage1 () {system. Windows. Forms. MessageBox. Show ("111 ");}}}

In class2.cs:

Namespace vs2012_demo1 {public partial class class1 {public void showmessage2 () {system. Windows. Forms. MessageBox. Show ("222 ");}}}

In form1:

Public partial class form1: FORM {class1 class1; Public form1 () {initializecomponent (); class1 = new class1 ();} private void button#click (Object sender, eventargs E) {class1.showmessage1 ();} private void button2_click (Object sender, eventargs e) {class1.showmessage2 ();}}

So

Showmessage1 ()
Showmessage2 () is written in two files. But it belongs to class1.

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.