Analysis of CSharpThinking Extension Method in asp.net,

Source: Internet
Author: User

Analysis of CSharpThinking Extension Method in asp.net,

This article describes the CSharpThinking Extension Method in asp.net. Share it with you for your reference. The specific analysis is as follows:

I. Evolution

① Extended method features

1) it must be in a static method.
2) there must be at least one parameter.
3) The first parameter must be prefixed with the this keyword.
4) The first parameter cannot have any other modifiers (such as out and ref ).
5) The type of the first parameter cannot be a pointer.
6) if the extension method name is the same as that of the type method (for example, the extension method name is ToString), only the type method will be called, but the extension method will not. This is a priority issue.

② Comparison Between extension methods and common static methods

When C #2 expands a class and does not apply the inheritance method, you can only write a static method that is slightly "ugly. C #3 allows us to change static classes to pretend that methods are inherent in classes.
Copy codeThe Code is as follows: public static void Demo1 ()
{
// C #2 common call Method
String Log2 = ExtensionCompare. GetLogError ("C #2 normal static mode ");
Console. WriteLine (Log2 );

// C #3 extension method call
String Log3 = "C #3 extension method". ToLogError ();
Console. WriteLine (Log3 );

Console. ReadLine ();
}
/// <Summary>
/// C #2 General static method extension
/// </Summary>
/// <Param name = "loginfo"> Format Information </param>
/// <Returns> </returns>
Public static string GetLogError (string loginfo)
{
Return string. Format ("This is C #2 style: {0}", loginfo );
}
/// <Summary>
/// C #3 string type Extension implemented using the Extension Method
/// </Summary>
/// <Param name = "loginfo"> </param>
/// <Returns> </returns>
Public static string ToLogError (this string loginfo)
{
Return string. Format ("This is C #3 style: {0}", loginfo );
}

II. The biggest purpose of the extension method is to use it in Linq.

① Where, Select, OrderBy,

Note: Sorting does not change the sequence and type of the original sequence. A new sequence is returned, which is different from List. Sort. The latter changes the sequence. Therefore, there are no side effects of Linq except for some special cases.
Copy codeThe Code is as follows: company. Department. Select
(Dept => new
{
Name = dept. name,
Cost = dept. Employees. Sum (person => person. Salary );
})
. OrderByDescending (x => x. Cost );
② Extension methods focus more on results rather than process understanding, which is different from static methods.

I hope this article will help you design your asp.net program.


Use the aspnet mvc htmlhelper Extension Method

Reference System. Web. Mvc;
And put the namespace of the class together with System. Web. Mvc.

Namespace System. Web. Mvc {

Public static class HtmlExp
{
Public static string SpanTagSet (this HtmlHelper helper, string id, string value, string key, string style)
{
TagBuilder tb = new TagBuilder (key );
Tb. MergeAttribute ("style", style );
Tb. MergeAttribute ("name", id );
Tb. GenerateId (id );
Tb. SetInnerText (value );
Return tb. ToString ();
}
}
}

C # why is there no Microsoft Extension Method for the aspnet ashx program?

Ashx inherits. the IHttpHandler interface is different from the normal Page interface. if you want any method of the class, you can use using above to extend the method. You may not understand it ....

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.