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 ...
随机推荐
- XHR对象
一.XMLHttpRequest对象 var xhr = new XMLHttpRequest(), i = 0; for(var key in xhr){ if(xhr.hasOwnProperty ...
- [CF896C]Willem, Chtholly and Seniorious(珂朵莉树)
https://www.cnblogs.com/WAMonster/p/10181214.html 主要用于支持含有较难维护的区间操作与查询的问题,要求其中区间赋值操作(assign())是纯随机的. ...
- Codeforces.1051F.The Shortest Statement(最短路Dijkstra)
题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...
- Codeforces.833B.The Bakery(线段树 DP)
题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...
- Java并发程序设计(十一)设计模式与并发之生产者-消费者模式
设计模式与并发之生产者-消费者模式 生产者-消费者模式是一个经典的多线程设计模式.它为多线程间的协作提供了良好的解决方案.在生产者-消费者模式中,通常由两类线程,即若干个生产者线程和若干个消费者线程. ...
- Unity 鼠标个性化
最初的版本,API是可以直接设置鼠标显示与否的,新版本就改了,先上下旧店的版本的: 1.思路: 在某些游戏下,经常会隐藏鼠标,或者有绚丽的动画来代替鼠标显示. 原理就是将鼠标隐藏(不显示)起来,设置一 ...
- 关闭Ubuntu桌面版GUI
个人用于实验的Ubuntu虚拟机不需要GUI,且要复制多个,但是又懒得重新装个Ubuntu,打算把现有的带桌面的直接装好克隆起来用,但是多个GUI一起开电脑吃不消,思前想后找了下关闭GUI的方法如下: ...
- AIX中vi编辑器使用
3.1 vi 简介 vi 是 UNIX 世界里使用非常普遍的全屏幕文本编辑器,几乎任何一种UNIX系统都会提供这套软件.AIX当然也支持这种编辑器.熟悉DOS下的文本处理后,用户在开始接触vi时也许会 ...
- CentOS 6.8 安装 Erlang 及 RabbitMQ Server
安装 Erlang 19.3 # 安装依赖包 yum install -y gcc gcc-c++ unixODBC-devel openssl-devel ncurses-devel # 下载 er ...
- [Android] 判断手机上是否安装了某个程序
http://blog.csdn.net/xiaodongrush/article/details/9320135 1. 首先得到该程序的包名 这个连上手机ADB,看logcat就能看到.比如:QQ客 ...