• Top useful .Net extension methods


    Special extension methods were released in C# 3.0. Developers have 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 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
    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.

    2. ToXmlDocument()/ToXDocument() – Type: XmlDocument or XDocument
    Your 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.

    3. Between() – Type: Daytime
    It is used to check if a date is between two dates.

    4. CalculateAge() – Type: DateTime
    As the name suggests, it is used for calculating age.

    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.

    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 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); }
            }
     
  • 相关阅读:
    新买的电脑桌面只有回收站该做些什么
    不安装oracle客户端也可以使用pl/sql developer
    Win7上安装Oracle数据库
    忘记oracle的sys用户密码怎么修改
    UML中类之间的关系
    JAVAEE 是什么,如何获取各种规范jar包及各种规范的jar包源码
    PL/SQL Developer使用技巧、快捷键
    Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)
    超棒的30款JS类库和工具
    HTTP协议
  • 原文地址:https://www.cnblogs.com/itelite/p/4241691.html
Copyright © 2020-2023  润新知