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