c#中扩展方法有哪些-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

c#中扩展方法有哪些

这篇文章主要介绍了c#中扩展方法有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

成都创新互联服务项目包括凤泉网站建设、凤泉网站制作、凤泉网页制作以及凤泉网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,凤泉网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到凤泉省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

简介

扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。 扩展方法当然不能破坏面向对象封装的概念,所以只能是访问所扩展类的public成员。

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。

C#扩展方法第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。

扩展方法的目的就是为一个现有类型添加一个方法,现有类型既可以是int,string等数据类型,也可以是自定义的数据类型。

一..net自带扩展方法和自定义扩展方法

在使用linq时就能够使用到很多.net自带的扩展方法,比如where select等等

where的扩展方法定义 

public static IEnumerable Where(this IEnumerable source, Func predicate);

Select的扩展方法定义

public static IEnumerable Select(this IEnumerable source, Func selector);

(1)自己实现where和select的扩展方法

// where自实现
 public static IEnumerable ExtenSionWhere(this IEnumerable source, Func predicate)
 {
  if (source == null)
  {
  throw new Exception(nameof(source));
  }
  if (predicate == null)
  {
  throw new Exception(nameof(predicate));
  }
  List satisfySource = new List();
  foreach (var sou in source)
  {
  if (predicate(sou))
  {
   satisfySource.Add(sou);
  }
  }
  return satisfySource;
 }

 
 // select 自实现
 public static IEnumerable ExtenSionSelect(this IEnumerable source, Func selector)
 {
  if(source==null)
  {
  throw new Exception(nameof(source));
  }
  if(selector==null)
  {
  throw new Exception(nameof(source));
  }

  List resultList = new List();
  foreach(var sou in source)
  {
  resultList.Add(selector(sou));
  }
  return resultList;
 }

(2)自实现where和select调用

static void Main(string[] args)
 {
  List list = new List() { 1, 2, 3, 4, 5, 6 };
  
  //常规写法
  var selectList = list.ExtenSionWhere(p => p > 3).ExtenSionSelect(p => p.ToString()).ToList();
 
  //自定义泛型委托写法
  Func whereFunc = (num) => num > 3;
  Func selectFunc = (num) => num.ToString();
  var selectList1 = list.ExtenSionWhere(p => whereFunc(p)).ExtenSionSelect(p => selectFunc(p)).ToList();
 
 }

二.使用扩展方法实现链式编程

我在项目中经常使用开源的Flurl进行http请求,在进行拼装请求报文时,就会使用到链式编程

如下代码所示

c#中扩展方法有哪些 

以上代码就是使用了扩展方法进行链式编程,从而使得整个请求信息可以在一句代码中体现出来

接下来,我们自己实现链式代码

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

 }

调用和执行结果

 c#中扩展方法有哪些

总结

1.使用扩展方法能在不修改原有类型的基础下,动态添加方法,这使得整个框架更具有灵活性

2.在使用上下文信息的时候,可以使用链式编程,使得调用时能够在一句代码中完成所有属性设置

3.扩展方法不能滥用.添加扩展方法应当使用最小影响原则,即尽量不要在父类使用扩展方法,比如object,这将影响性能

感谢你能够认真阅读完这篇文章,希望小编分享的“c#中扩展方法有哪些”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


当前题目:c#中扩展方法有哪些
本文URL:http://kswsj.cn/article/johdso.html

其他资讯