Top useful .Net extension methods

banner

Special extension methods were released in C# 3.0. Developers have continuously been looking for ways to extend classes to every coding got top most preferable extension methods for .net web development.

Is there a class that just doesn’t do enough for you? Is it true that you always wanted to add more onto a Date-Time class, but somehow you couldn’t? Things which were not possible earlier can be true with extension methods that C# 3.0 unveiled.

What are 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 are:

  • The method needs to be static
  • The class needs to be static
  • The method’s initial parameter in the signature should have the “this” declared
Always looking for ways to extend the classes in your coding?

It might be difficult for you to build new functionalities for the types existing in your coding without altering them. Then contact us to get the awareness of the most preferable extension methods and the major characteristics of the extension methods.

Top useful .net extension methods are:

1. ToFileSize-Type: Long

It was required by developers because it was easier to read the number. Developers can convert it into formatted number with the apt size metric.

net

2. ToXmlDocument()/ToXDocument() – Type: XmlDocument or XDocument

Your .Net developer can’t explain how many times he needs to convert an XmlDocument into an XDocument and vice versa for LINQ use. The following handy extension methods are intended to save huge time.

net

3. Between() – Type: Daytime

It is used to check if a date is between two dates.

net

4. CalculateAge() – Type: DateTime

As the name suggests, it is used for calculating age.

net

5. Workingday()/Isweekend()/NextWorkday() – Type: Datetime

It is used for determining if a date is a working day, weekend, or finding the next upcoming workingday.

net

6. Next() – Type: DateTime

It is used for determining the Next Date by passing in a DayofWeek.

net

7. Has<T>()/Is<T>()/Add<T>()/Remove<T>() – type Enum

This extension method is great when an enumerated type is flag instead of full items.

public static bool Has<T> (this System.Enum type, T value) { try { return (((int)(object)type & (int)(object)value) == (int)(object)value); } catch { return false; } } public static bool Is<T> (this System.Enum type, T value){ try { return (int)(object)type == (int)(object)value; } catch { return false; } } public static T Add<T> (this System.Enum type, T value) { try { return (T)(object)(((int)(object)type | (int)(object)value)); } catch (Exception ex) { throw new ArgumentException( string.Format( "Could not append value from enumerated type '{0}'.", typeof(T).Name ), ex); } } public static T Remove<T> (this System.Enum type, T value) { try { return (T)(object)(((int)(object)type & ~(int)(object)value)); } catch (Exception ex) { throw new ArgumentException( string.Format( "Could not remove value from enumerated type '{0}'.", typeof(T).Name ), ex); } }

For further information, mail us at [email protected]

Related article

The Microsoft .NET stage assuming the huge Role in transit applications are created for the internet-based applications.

In the beginning stages of contemplating your future as a software developer, selecting the appropriate framework might be difficult, particularly for newcomers to the profession. Understanding any programming language requires a significant amount of time and effort, both of which you do not want to squander.

Azure function is a service offering by Microsoft Azure which allows a small piece of code to be written for special purpose and can be deployed on cloud resources independently. It is used in building WEB APIs which exposes an endpoint that can be hit by any external consumer and logic can be processed on demand. We can use functions to build web apis which we will be doing in this article and we will also test it with the help of Postman client.

DMCA Logo do not copy