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. c#操作数据库的增删改查语句及DataGridView简单使用

    下面是要用户名和密码连接数据库的操作: 一.定义连接字符串,用来链接SQL Server string str_con = "server=.(服务器名称一般为 . );database=W ...

  2. navLI鼠标滑上显示下拉导航

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>l ...

  3. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  4. 构造函数与getter和setter的区别

    构造函数是用于初始化类的属性,且只有在创建对象时才会调用构造函数,用于给对象分配地址 无参的构造函数,创建对象时默认调用,当程序没有明确写出有参的构造函数,系统会默认的创建一个. 有参的构造函数,创建 ...

  5. HTML 5 断点续上传

    断点上传,java里面比较靠谱一点的,一般都会选用Flex.我承认,Flex只是摸了一下,不精通.HTML 5 有个Blob对象(File对象继承它),这个对象有个方法slice方法,可以对一个文件进 ...

  6. Kubernetes基础:编排调度的那些Controllers

    0. 概述 Kubernetes提供了很多Controller资源来管理.调度Pod,包括Replication Controller.ReplicaSet.Deployments.StatefulS ...

  7. C# 获取文件名及扩展名【转】

    https://www.cnblogs.com/libushuang/p/5794976.html C# 获取文件名及扩展名 string aFirstName = aFile.Substring(a ...

  8. [Android] 关于Android的问号?和@符号的用法

    @表示引用资源,声明这是一个资源引用-随后的文本是以@[package:]type/name形式提供的资源名.@android:string表明引用的系统的(android.*)资源@string表示 ...

  9. 高性能JavaScript之DOM编程

    我们知道.DOM是用于操作XML和HTML文档的应用程序接口,用脚本进行DOM操作的代价非常昂贵. 有个贴切的比喻.把DOM和JavaScript(这里指ECMScript)各自想象为一个岛屿,它们之 ...

  10. Spark2.2+ES6.4.2(三十一):Spark下生成测试数据,并在Spark环境下使用BulkProcessor将测试数据入库到ES

    Spark下生成2000w测试数据(每条记录150列) 使用spark生成大量数据过程中遇到问题,如果sc.parallelize(fukeData, 64);的记录数特别大比如500w,1000w时 ...