C # extension method is simple

Source: Internet
Author: User

Extension methods

How to know.

This has to be said from the project itself. The project was built on a set of Microsoft's underlying architectures.

The entire framework and the encapsulation of the control, the method of data transfer is the overall framework encapsulated well.

For data binding to frequently used dropwodnlist controls, here is the encapsulation of some of the extension methods used. How come I've never seen this method before? The only way to find out is to follow the past.

What is an extension method

An extension method can "join" a method to an existing type without creating a new derived type, compiling again, or otherwise altering the original type.

An extension method is a special static method , and there is no significant difference between calling an extension method and the method that is actually defined in the type.

How to write an extension method

Extension methods are defined as static methods, but they are called through the instance method syntax. Their first parameter specifies which type the method acts on. And the parameter is prefixed with the T his modifier . extension methods are only in scope when you explicitly import namespaces into the source using a using directive.

Look at the following examples

Using System;usingsystem.collections.generic; Using myspace;//Note: The introduction of extension method Space namespace con_1{    class program    {        static void Main (string[] args)        {            String str = "{0}".

". With ("Xugang"); Console.WriteLine ("Hello. "+ str); 2 Call the collection's extension method str. Showitems<char> ();}}} namespace myspace{ //extension method must define public static class Mymethods { //NOTE: The first parameter uses "This" to get the current object Public static string with (this string_context, params string[] _args) { returnstring. Format (_context,_args); } 2 extension method for collection public static voidshowitems<t> (this ienumerable<t> _al) { foreach (var item in _al) { Console.WriteLine (item); } }



It can be seen from the above example that

1 C # only supports extension methods. Extended attributes, extended events, etc. are not supported;

2 The method name is unlimited, the first parameter must take a This ;

3 The namespace of the extension method can be used namespace System, but not recommended;

4 The class that defines the extension method is a static class.

Please refer to the detailed guidelines

for specific information. See How to : Implement and invoke your own definition extension methods (C # Programming Guide)

Summary:


These days just arrived at the company, familiar with the environment as the main. A lot of things they said were not immediately able to be clear.

So you still need to ask the next colleague.

。 Many of the methods used are packaged well. So to see this thought familiar but meticulous look. Not. Extension methods no more places to use as little as possible.

Avoid errors. After all, these methods are the foundation of the platform designers for several years.



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

C # extension method is simple

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.