c#3.0 New Experience (II.)

Source: Internet
Author: User
Tags instance method static class
Foreword:

In addition to persistence in learning, you also need a good method! Summarizing and organizing the materials you learn can make the content you learn clear and easy to find and review! As the saying goes: "Good memory is worse than bad writing"; a lot of knowledge has been read before, and after a period of time I forget it, time spent, but the effect is very small; see more, but miscellaneous but not fine; these are not Taking notes does not waste time and energy caused by in-depth study, so each research topic must be well organized and a series of research!

Three. Extension method (continued from the previous series)

The extension method is defined as follows:

public static class Extensions {
Public static void Foo (this string s) {
Uh ...
}
}
use:
String s = “Hello, World”;
S.Foo ();

We previously performed some processing on some parameters and objects, and wrote some independent methods to encapsulate these processing statements; these methods may be written in some public classes for everyone to call, for example:

Define a public method to calculate how many minutes per day,
public class PublicCenter
{
// Incoming parameters are days
Public long GetDaysMin (int pDays)
{
Return pDays * 24 * 60;
}
}
External use call:
int days = 3;
PublicCenter pCenter = new PublicCenter ();
int minutes = pCenter.GetDaysMin (days);

I think everyone is very familiar with the use of the above code, which has been handled a lot in the past! Now let's see what it would look like to handle with extension methods;

Define the static method of a static class to calculate how many minutes per day (note that it must be static)
public staticclass PublicCenter
{
// Incoming parameters are days (note that this keyword is essential)
Public staticlong GetDaysMin (this int pDays)
{
Return pDays * 24 * 60;
}
}
External use calls: (Note that static classes must be in the same namespace, or add references, which everyone should also know (知))
int days = 3;
// When we press "." After all int type variables, we will intelligently prompt these extension methods GetDaysMin () we wrote.
int minutes = days.GetDaysMin ()

As you can see, the essence of the extension method is to change the instance method call at compile time to a static method call in a static class.

The definition of extension methods in MSDN is that extension methods enable you to "add" methods to existing types without having to create new derived types, recompile, or otherwise modify the original types.
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: 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.