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); }
}
 

Top useful .Net extension methods的更多相关文章

  1. C# Extension Methods

    In C#, extension methods enable you to add methods to existing class without creating a new derived ...

  2. AX7: HOW TO USE CLASS EXTENSION METHODS

    AX7: HOW TO USE CLASS EXTENSION METHODS   To create new methods on a standard AX class without custo ...

  3. Extension Methods

    Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...

  4. C# -- 扩展方法的应用(Extension Methods)

    当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...

  5. (转)C# -- 扩展方法的应用(Extension Methods)

    本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...

  6. Extension Methods "点"函数方法 扩展方法

    原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...

  7. Extension Methods(扩展方法)

    在 OOPL 中,有静态方法.实例方法和虚方法,如下:   public sealed class String {      public static bool  IsNullOrEmpty(st ...

  8. Extension Methods (C# Programming Guide)

    https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...

  9. C# Extension Methods(C#类方法扩展)

    使用Extension methods 可以在已有的类型(types)中添加方法(Methods),而无需通过增加一种新的类型或修改已有的类型. 比如说,想要给string类型增加一个PrintStr ...

随机推荐

  1. 探究functools模块wraps装饰器的用途

    <A Byte of Python>17.8节讲decorator的时候,用到了functools模块中的一个装饰器:wraps.因为之前没有接触过这个装饰器,所以特地研究了一下. 何谓“ ...

  2. SolidWorks知识积累系列-01

    Solidworks学习 1. 基本知识点总结 基准视图 主视图:从前往后看,前视基准 俯视图:从上往下看,上视基准 侧视图:从右向左看,右视基准 草图要求 单封闭性,草图要依附于某个位置 绘制大概形 ...

  3. 解决iframe重复嵌套登陆页面的问题

    在login.jsp中加入即可 // 在被嵌套时就刷新上级窗口 if(window.parent != window){ window.parent.location.reload(true); }

  4. 在Notepad++中快捷选中多行

    原文: 在Notepad++中快捷选中多行 Notepad++是Windows下一款很好用的文本编辑器. 如果需要在一个文档中选中指定的若干行,常见的办法是鼠标按住拖动.这样对于少数几行的选取还可以, ...

  5. json数据格式 net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of Error:(findColumns1)Read timed out

    substring(3)的用法http://www.w3school.com.cn/jsref/jsref_substring.asp 可能一:sb是要转化的数据,以sb是String为例       ...

  6. linux最大进程数、最大打开文件数

    ulimit 是一种 linux 系统的内键功能,它具有一套参数集,用于为由它生成的 shell 进程及其子进程的资源使用设置限制.本文将在后面的章节中详细说明 ulimit 的功能,使用以及它的影响 ...

  7. print2flash文档在线预览应用(java,.net)

    一.背景 前段时间,LZ的boss突然给了出了这样一个需求:将原项目中的所有文章关联的附件TXT.PDF.office相关文件全部以flash的形式在网页上进行展示,便于预览.看似简单的需求,整个研发 ...

  8. 集合(4)—Collection之Set的使用方法

    定义 set接口及其实现类–HashSet Set是元素无序且不可重复的集合,被称为集. HashSet是哈希集,是Set的一个重要实现类 set中循环只能使用foreach和iterator这两个, ...

  9. ArcGIS Pro 中不可用的工具

    有些可用于 ArcMap 之类的其他 ArcGIS Desktop 应用程序的地理处理工具在 ArcGIS Pro 中不可用.用于处理 ArcGIS Pro 所不支持的数据格式的地理处理工具已被移除, ...

  10. C#模拟HTTP请求Post JSON

    前言 因为接口是http的,我们站点是https的,不能https直接ajax请求http,所以需要在SharePoint中开发一个模拟请求Ajax的Service,分享一下. var httpWeb ...