c#中的擴展方法學習筆記
前言
最近在看王清培前輩的.NET框架設計時,當中有提到擴展方法 .
開頭的一句話是:擴展方法是讓我們在不改變類原有代碼的情況下動態(tài)地添加方法的方式,這給面向對象設計 模塊設計帶來了質的提升
很明顯,擴展方法在框架設計或者平時碼代碼中,是能夠提升我們整個架構的靈活性的
簡介
擴展方法被定義為靜態(tài)方法,但它們是通過實例方法語法進行調用的。 它們的第一個參數指定該方法作用于哪個類型,并且該參數以 this 修飾符為前綴。 擴展方法當然不能破壞面向對象封裝的概念,所以只能是訪問所擴展類的public成員。
擴展方法使您能夠向現有類型“添加”方法,而無需創(chuàng)建新的派生類型、重新編譯或以其他方式修改原始類型。擴展方法是一種特殊的靜態(tài)方法,但可以像擴展類型上的實例方法一樣進行調用。
C#擴展方法第一個參數指定該方法作用于哪個類型,并且該參數以 this 修飾符為前綴。
擴展方法的目的就是為一個現有類型添加一個方法,現有類型既可以是int,string等數據類型,也可以是自定義的數據類型。
一..net自帶擴展方法和自定義擴展方法
在使用linq時就能夠使用到很多.net自帶的擴展方法,比如where select等等
where的擴展方法定義
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
Select的擴展方法定義
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
(1)自己實現where和select的擴展方法
// where自實現
public static IEnumerable<TSource> ExtenSionWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
{
throw new Exception(nameof(source));
}
if (predicate == null)
{
throw new Exception(nameof(predicate));
}
List<TSource> satisfySource = new List<TSource>();
foreach (var sou in source)
{
if (predicate(sou))
{
satisfySource.Add(sou);
}
}
return satisfySource;
}
// select 自實現
public static IEnumerable<TResult> ExtenSionSelect<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
if(source==null)
{
throw new Exception(nameof(source));
}
if(selector==null)
{
throw new Exception(nameof(source));
}
List<TResult> resultList = new List<TResult>();
foreach(var sou in source)
{
resultList.Add(selector(sou));
}
return resultList;
}
(2)自實現where和select調用
static void Main(string[] args)
{
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
//常規(guī)寫法
var selectList = list.ExtenSionWhere(p => p > 3).ExtenSionSelect(p => p.ToString()).ToList();
//自定義泛型委托寫法
Func<int, bool> whereFunc = (num) => num > 3;
Func<int, string> selectFunc = (num) => num.ToString();
var selectList1 = list.ExtenSionWhere(p => whereFunc(p)).ExtenSionSelect(p => selectFunc(p)).ToList();
}
二.使用擴展方法實現鏈式編程
我在項目中經常使用開源的Flurl進行http請求,在進行拼裝請求報文時,就會使用到鏈式編程
如下代碼所示
以上代碼就是使用了擴展方法進行鏈式編程,從而使得整個請求信息可以在一句代碼中體現出來
接下來,我們自己實現鏈式代碼
public static class ContextExtension
{
public static RectangleContext SetLength(this RectangleContext context,int num)
{
RectangleContext.Config.Length = num;
return context;
}
public static RectangleContext SetWideth(this RectangleContext context, int num)
{
RectangleContext.Config.Wideth = num;
return context;
}
public static RectangleContext SetHeight(this RectangleContext context, int num)
{
RectangleContext.Config.Height = num;
return context;
}
}
public class RectangleContext
{
public static RectangleContext Config=new RectangleContext();
public int Length { get; set; }
public int Wideth { get; set; }
public int Height { get; set; }
}
調用和執(zhí)行結果

總結
1.使用擴展方法能在不修改原有類型的基礎下,動態(tài)添加方法,這使得整個框架更具有靈活性
2.在使用上下文信息的時候,可以使用鏈式編程,使得調用時能夠在一句代碼中完成所有屬性設置
3.擴展方法不能濫用.添加擴展方法應當使用最小影響原則,即盡量不要在父類使用擴展方法,比如object,這將影響性能
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
C#入門之checked和unchecked的區(qū)別實例解析
這篇文章主要介紹了C#中checked和unchecked的區(qū)別,是學習C#必須要牢固掌握的,需要的朋友可以參考下2014-08-08

