C #3.0 use of partial Method

Source: Internet
Author: User

As early as C #2.0, partial appeared, but that is, partial can only be used on the class,

That is, the branch class. For the branch class, there are many practical aspects,

Because the partial method is based on the partial class,

So here we will first introduce the use of partial class,

Then we will introduce partial method together,

For partial class, it is actually to split a class into several partial classes,

And these branch classes can be written in different class files. CS,

For example, write the first part of this class in A. CS file,

Then complete the second part of the class in another. CS,

Complete the third part of this class in another. CS,

....................

This is the branch class.

Next, let's take a look at the example,

From the project view above, we can see that a class has been divided into four parts,

Then, instantiate and access this class in code-behind of demo _ 3,

Let's take a look atCodeRight (very simple)

This is demo _ 3 _ use _ partialmethodone. CS

PublicPartialClass demo _ 3 _ use _ partialmethod
{
Private string name = "";

Public void methodone ()
{
Name + = "you have come to the methodone method <br/> ";
}

Public String getname ()
{
Methodone ();
Methodtwo ();
Methodthree ();
Methodfour ();
Return name;
}
}

This is demo _ 3 _ use _ partialmethodtwo. CS

PublicPartialClass demo _ 3 _ use _ partialmethod
{
Public void methodtwo ()
{
Name + = "you have come to the methodtwo method <br/> ";
}
}

This is demo _ 3 _ use _ partialmethodthree. CS

PublicPartialClass demo _ 3 _ use _ partialmethod
{
Public void methodthree ()
{
Name + = "you have come to the methodthree method <br/> ";
}
}

This is demo _ 3 _ use _ partialmethodfour. CS.

PublicPartialClass demo _ 3 _ use _ partialmethod
{
Public void methodfour ()
{
Name + = "you have come to the methodfour method <br/> ";
}
}

Then you just need to check the demo _ 3. aspx code-behind.

Using system;

Namespace csharplanguage
{
Public partial class demo _ 3: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Demo _ 3 _ use _ partialmethod =
New demo _ 3 _ use _ partialmethod ();

Response. Write (partialmethod. getname ());
}
}
}

The above is all the code of the entire demo,

Let's take a look at the results after using partial class.

After the explanation of the partial class is completed, the partial method is started,

In fact, the partial method is fully dependent on the implementation of the partial class,

The partial method is also the division method,

The division method is to split a complete method into two parts,

That is, the definition and implementation of methods,

Then, we can put the two parts in different. CS,

For example, I have a method,

I define this method in this branch class,

Then I implement this method in another branch class,

This is the division method,

Obviously, the partial method depends on the partial class,

Because you must complete the previous method definition in this partial class,

Then, complete the subsequent method implementation in another branch class,

Therefore, we can clearly see that there is no partial method without partial class,

However, we can see from the example of partial class described above that,

Partial class is also relatively easy, so partial method is naturally not difficult,

Let's take a look at a demo.

Demo _ 4 _ use _ partialone. CS

PublicPartialClass demo _ 4 _ use _ partial
{
Private string name = "";
PartialVoid usename ();
Public String getname ()
{
Usename ();
Return name;
}
}

Demo _ 4 _ use _ partialtwo. CS

PublicPartialClass demo _ 4 _ use _ partial
{
PartialVoid usename ()
{
Name + = "partial method is used to manipulate name ";
}
}

Demo _ 4. Code in aspx code-behind

Using system;

Namespace csharplanguage
{
Public partial class demo _ 4: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Demo _ 4 _ use _ partial partialmethod =
New demo _ 4 _ use _ partial ();
Response. Write (partialmethod. getname ());
}
}
}

Check the result

Although the above is a complete use of the partial class and partial method,

However, you must note that there are still many restrictions on using the partial method,

First, when declaring the partial method, you must use the keyword partial to declare it,

Then, when using the partial method, you must pass the partial class to use it,

When partial method is used, its position in the class can only be private,

It is a private member and cannot access the partial method outside the class,

It is also important that the partial method cannot return values, that is, it can only be void.

2010-2-01

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.