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); }
}
Top useful .Net extension methods的更多相关文章
- C# Extension Methods
In C#, extension methods enable you to add methods to existing class without creating a new derived ...
- 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 ...
- Extension Methods
Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...
- C# -- 扩展方法的应用(Extension Methods)
当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...
- (转)C# -- 扩展方法的应用(Extension Methods)
本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...
- Extension Methods "点"函数方法 扩展方法
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...
- Extension Methods(扩展方法)
在 OOPL 中,有静态方法.实例方法和虚方法,如下: public sealed class String { public static bool IsNullOrEmpty(st ...
- Extension Methods (C# Programming Guide)
https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...
- C# Extension Methods(C#类方法扩展)
使用Extension methods 可以在已有的类型(types)中添加方法(Methods),而无需通过增加一种新的类型或修改已有的类型. 比如说,想要给string类型增加一个PrintStr ...
随机推荐
- ARM总线架构
S3C2440集成了丰富了外设控制器(LCD控制器.USB Device控制器.USB Host控制器.NAND FLASH控制器.I2C控制器.SPI控制器等).要控制这些外设就要设置相应控制器的寄 ...
- AGC 016C.+/- Rectangle(思路 构造)
题目链接 首先想到同样例1的构造方式.即不得不填负的格子填\(-h*w\),其余填\(1\).直接这样能过二三十个点. 只这样不对.比如1 4 1 3,会输出无解(会填[1 1 -3 1]).怎么改呢 ...
- 洛谷P1395 会议(CODEVS.3029.设置位置)(求树的重心)
To 洛谷.1395 会议 To CODEVS.3029 设置位置 题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会 ...
- WCF、WebAPI、WCFREST和Web服务的差异 ASP.NETMVC和ASP.NETWebAPI的差异
WCF.WebAPI.WCFREST和Web服务的差异: Web服务 它是基于SOAP和XML的形式返回数据. 它仅支持HTTP协议. 它是开放源,但是不消耗任何客户端可以同时理解XML. 它可以仅在 ...
- 虚拟机搭建和安装Hadoop及启动
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- 第二章 flex处理二义性
大多数flex程序有二义性,相同的输入可能被多种模式匹配 flex通过下面2个规则来解决 匹配尽可能长的字符 如果2个模式都可以匹配, 匹配更早出现的那个模式 例子 "+" { r ...
- 在windows 10下使用docker
准备工作 Windows 10下的Docker是依赖于Hyper-v的,首先我们需要启用它:控制面板 -> 程序 -> 启用或关闭Windows功能 -> 选中Hyper-V 安装D ...
- mysql5.7报err 1055错误 sql_mode=only_full_group_by
vim /etc/my.cnf 末尾增加 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_B ...
- windows 2003 iis 360防黑加固后不能使用
最近在使用360的防黑加固加固2003系统,发现IIS居然不能够使用了,报401.1错误,查找解决方案如下: 1.我的电脑-〉属性-〉管理-〉本地用户和组,查看IUSER用户是否开启,如果未开启开启后 ...
- [Python设计模式] 第2章 商场收银软件——策略模式
github地址: https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序, 模拟商场收银软件,根据客户购买商品的单价和数量,计 ...