溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c#中擴(kuò)展方法有哪些

發(fā)布時(shí)間:2021-08-18 14:08:16 來源:億速云 閱讀:153 作者:小新 欄目:編程語言

這篇文章主要介紹了c#中擴(kuò)展方法有哪些,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

簡(jiǎn)介

擴(kuò)展方法被定義為靜態(tài)方法,但它們是通過實(shí)例方法語法進(jìn)行調(diào)用的。 它們的第一個(gè)參數(shù)指定該方法作用于哪個(gè)類型,并且該參數(shù)以 this 修飾符為前綴。 擴(kuò)展方法當(dāng)然不能破壞面向?qū)ο蠓庋b的概念,所以只能是訪問所擴(kuò)展類的public成員。

擴(kuò)展方法使您能夠向現(xiàn)有類型“添加”方法,而無需創(chuàng)建新的派生類型、重新編譯或以其他方式修改原始類型。擴(kuò)展方法是一種特殊的靜態(tài)方法,但可以像擴(kuò)展類型上的實(shí)例方法一樣進(jìn)行調(diào)用。

C#擴(kuò)展方法第一個(gè)參數(shù)指定該方法作用于哪個(gè)類型,并且該參數(shù)以 this 修飾符為前綴。

擴(kuò)展方法的目的就是為一個(gè)現(xiàn)有類型添加一個(gè)方法,現(xiàn)有類型既可以是int,string等數(shù)據(jù)類型,也可以是自定義的數(shù)據(jù)類型。

一..net自帶擴(kuò)展方法和自定義擴(kuò)展方法

在使用linq時(shí)就能夠使用到很多.net自帶的擴(kuò)展方法,比如where select等等

where的擴(kuò)展方法定義 

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Select的擴(kuò)展方法定義

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

(1)自己實(shí)現(xiàn)where和select的擴(kuò)展方法

// where自實(shí)現(xiàn)
 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 自實(shí)現(xiàn)
 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)自實(shí)現(xiàn)where和select調(diào)用

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

二.使用擴(kuò)展方法實(shí)現(xiàn)鏈?zhǔn)骄幊?/strong>

我在項(xiàng)目中經(jīng)常使用開源的Flurl進(jìn)行http請(qǐng)求,在進(jìn)行拼裝請(qǐng)求報(bào)文時(shí),就會(huì)使用到鏈?zhǔn)骄幊?/p>

如下代碼所示

c#中擴(kuò)展方法有哪些 

以上代碼就是使用了擴(kuò)展方法進(jìn)行鏈?zhǔn)骄幊?從而使得整個(gè)請(qǐng)求信息可以在一句代碼中體現(xiàn)出來

接下來,我們自己實(shí)現(xiàn)鏈?zhǔn)酱a

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

 }

調(diào)用和執(zhí)行結(jié)果

 c#中擴(kuò)展方法有哪些

總結(jié)

1.使用擴(kuò)展方法能在不修改原有類型的基礎(chǔ)下,動(dòng)態(tài)添加方法,這使得整個(gè)框架更具有靈活性

2.在使用上下文信息的時(shí)候,可以使用鏈?zhǔn)骄幊?使得調(diào)用時(shí)能夠在一句代碼中完成所有屬性設(shè)置

3.擴(kuò)展方法不能濫用.添加擴(kuò)展方法應(yīng)當(dāng)使用最小影響原則,即盡量不要在父類使用擴(kuò)展方法,比如object,這將影響性能

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“c#中擴(kuò)展方法有哪些”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI