Top useful. Net extension methods

Source: Internet
Author: User

Special extension methods were released in C # 3.0. Developers has continuously been looking for ways to extend classes to every coding and got top most preferable extension Methods for. NET development.
Is there a class this just doesn ' t do enough for you? Is it true this wanted to add more onto a date-time class, but somehow you couldn ' t? Things which were not possible earlier can is true with extension methods that C # 3.0 unveiled. What is extension methods?

Extension methods empower developers to build new functionality for existing types without altering the actual type itself . The major characteristics of an extension method is:

    • The method needs to be static
    • The class needs to be static
    • The method ' s initial parameter in the signature should has the "this" declared
Top useful. Net extension methods are:

1. Tofilesize-type:long
It is required by developers because it is easier to read the number. Developers can convert it into formatted number with the APT size metric.

2. Toxmldocument ()/toxdocument () –type:xmldocument or XDocument
Your developer can ' t explain how many times he needs to convert a XmlDocument into a XDocument and vice versa for LINQ u Se. The following handy extension methods is intended to save huge time.

3. Between () –type:daytime
It is used to check if a date is between the dates.

4. Calculateage () –type:datetime
As the name suggests, it's used for calculating.

5. Workingday ()/isweekend ()/nextworkday () –type:datetime
It is used for determining if a date was a working day, weekend, or finding the next upcoming workingday.

6. Next () –type:datetime
It is used for determining the Next Date by passing in a DayofWeek.

7. Has ()/is ()/add ()/remove () –type Enum
This extension method was great when a enumerated type is flag instead of the full items.

 Public Static BOOLHas<t> ( ThisSystem.Enum type, T value) {            Try{return(((int)(Object) Type & (int)(Object) value) = = (int)(Object) value); }            Catch{return false; } }         Public Static BOOLIs<t> ( ThisSystem.Enum type, T value) {            Try{return(int)(Object) Type = = (int)(Object) value; }            Catch{return false; } }         Public StaticT add<t> ( ThisSystem.Enum type, T value) {            Try{returnT (Object)(((int)(Object) type | (int)(Object) (value)); }            Catch(Exception ex) {Throw NewArgumentException (string. Format ("Could not append value from enumerated type ' {0} '.",typeof(T). Name), ex); }        }         Public StaticT remove<t> ( ThisSystem.Enum type, T value) {            Try{returnT (Object)(((int)(Object) Type & ~ (int)(Object) (value)); }            Catch(Exception ex) {Throw NewArgumentException (string. Format ("Could not remove value from enumerated type ' {0} '.",typeof(T). Name), ex); }        }

Top useful. Net extension methods

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.