当前位置: 首页 > news >正文

微山网站建设/网络营销顾问是做什么的

微山网站建设,网络营销顾问是做什么的,网络设计的最后一个步骤是,html5个人网站模板在微软的.net framework框架中我们知道String有一个Replace的方法&#xff0c;今天我们来实现一个LINQ风格的基于集合Replace元素的扩展方法。首先来看一下&#xff0c;简单类图: 下面是主要代码片断&#xff1a; 1: /// <summary> 2: /// ReplaceExtensions w…

       在微软的.net framework框架中我们知道String有一个Replace的方法,今天我们来实现一个LINQ风格的基于集合Replace元素的扩展方法。首先来看一下,简单类图:

2012-12-22_210823
     下面是主要代码片断:

   1:     /// <summary>
   2:      /// ReplaceExtensions which is LINQ Style Replace Operator
   3:      /// </summary>
   4:      public static class ReplaceExtensions
   5:      {
   6:          /// <summary>
   7:          /// Replaces the specified sequence.
   8:          /// </summary>
   9:          /// <typeparam name="T"></typeparam>
  10:          /// <param name="sequence">The sequence.</param>
  11:          /// <param name="find">The find.</param>
  12:          /// <param name="replaceWith">The replace with.</param>
  13:          /// <param name="comparer">The comparer.</param>
  14:          /// <returns>Collection of type T</returns>
  15:          public static IEnumerable<T> Replace<T>(
  16:      this IEnumerable<T> sequence, T find, T replaceWith, IEqualityComparer<T> comparer)
  17:          {
  18:              if (sequence == null) throw new ArgumentNullException("sequence");
  19:              if (comparer == null) throw new ArgumentNullException("comparer");
  20:   
  21:              return ReplaceImpl(sequence, find, replaceWith, comparer);
  22:          }
  23:   
  24:          /// <summary>
  25:          /// Replaces the specified sequence.
  26:          /// </summary>
  27:          /// <typeparam name="T"></typeparam>
  28:          /// <param name="sequence">The sequence.</param>
  29:          /// <param name="find">The find.</param>
  30:          /// <param name="replaceWith">The replace with.</param>
  31:          /// <returns>Collection of type T</returns>
  32:          public static IEnumerable<T> Replace<T>(
  33:      this IEnumerable<T> sequence, T find, T replaceWith)
  34:          {
  35:              return Replace(sequence, find, replaceWith, EqualityComparer<T>.Default);
  36:          }
  37:   
  38:          /// <summary>
  39:          /// Replaces the impl.
  40:          /// </summary>
  41:          /// <typeparam name="T"></typeparam>
  42:          /// <param name="sequence">The sequence.</param>
  43:          /// <param name="find">The find.</param>
  44:          /// <param name="replaceWith">The replace with parameter</param>
  45:          /// <param name="comparer">The comparer.</param>
  46:          /// <returns>Collection of type T</returns>
  47:          private static IEnumerable<T> ReplaceImpl<T>(
  48:      IEnumerable<T> sequence, T find, T replaceWith, IEqualityComparer<T> comparer)
  49:          {
  50:              foreach (T item in sequence)
  51:              {
  52:                  bool match = comparer.Equals(find, item);
  53:                  T x = match ? replaceWith : item;
  54:                  yield return x;
  55:              }
  56:          }
  57:      }


从代码应该不难看懂,我们使用IEqualityComparer<T>,  然后Equals, 接着使用了yield, 您可以根据需求实现自己的实现类。这样我们就能实现类似LINQ风格的Replace集合元素扩展,看下面的UnitTest:

   1:          [TestMethod]
   2:          public void Single_IntCollection_Replace_Should_Same()
   3:          {
   4:              //Arranage
   5:              int[] values = new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
   6:   
   7:              //Act
   8:              int[] replaced = values.Replace(3, 0).ToArray();
   9:   
  10:              //Assert
  11:              CollectionAssert.AreEqual(new int[] { 1, 2, 0, 4, 5, 4, 0, 2, 1 }, replaced,"Results should be same.");
  12:          }
  13:   
  14:          [TestMethod]
  15:          public void StringCollection_Replace_Should_Same()
  16:          {
  17:              //Arrange
  18:              string[] strings = new string[] { "A", "B", "C", "D", "a", "b", "c", "d" };
  19:   
  20:              //Act
  21:              string[] replacedCS = strings.Replace("b", "-").ToArray();
  22:              string[] replacedCI = strings.Replace("b", "-", StringComparer.InvariantCultureIgnoreCase).ToArray();
  23:   
  24:              //Assert
  25:              CollectionAssert.AreEqual(new string[] { "A", "B", "C", "D", "a", "-", "c", "d" }, replacedCS, "Results should be same.");
  26:              CollectionAssert.AreEqual(new string[] { "A", "-", "C", "D", "a", "-", "c", "d" }, replacedCI, "Results should be same.");
  27:          }


基于MsTest的单元测试,您了解的话,应该不难看懂上面的代码。
希望对您CSharp编码有帮助。

您可能感兴趣的文章:

为IEnumerable<T>增加Combine的扩展方法
IEnumerable的扩展方法


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog。

转载于:https://www.cnblogs.com/wintersun/archive/2012/12/22/2829506.html

http://www.lbrq.cn/news/1618831.html

相关文章:

  • 群晖wordpress设为首页/seo比较好的优化方法
  • 希爱力/安徽seo
  • 嵊州网站建设/陕西网站推广公司
  • 网络运营商有哪几家/seo对各类网站的作用
  • 个人网站开发要多久/模板建站的网站
  • 优化疫情防控举措/关键词优化公司哪家强
  • 怎么做传奇私服广告网站/会计培训班
  • 最新互联网平台项目/关键词优化包年推广
  • 云南建设委员会官方网站/app推广一手单平台
  • 拔别人的网站做网站合法吗/网站制作公司排名
  • 网站新建网页/百度关键词搜索排名多少钱
  • 建设网站熊掌号/全国疫情最新消息今天实时
  • 网站友情链接怎么做/刷神马关键字排名软件
  • 检查部门网站建设/无锡网站制作优化
  • 微信网站建设和维护/seo建站的步骤
  • 哪些网站可以做设计方案/关键词排名点击
  • 彩票网站开发制作h5/seo搜索引擎优化案例
  • 吉林网站建设找哪家/北京搜索关键词优化
  • 重庆建设工程信息网入渝备案查询/网站优化外包找谁
  • 上海网站建设联/域名信息查询
  • 如何鉴别建设银行网站真伪/企业网站推广注意事项
  • 企业请别人做网站/91关键词
  • 全屏网站设计尺寸/私人网站管理软件
  • 纵横天下网站建设/汕头seo网络推广
  • 网页设计与制作教程刘瑞新/百度seo查询
  • 南昌房产网官方网站/nba最快的绝杀
  • tk域名网站/网络营销策划的流程
  • 如何在国外网站做推广/餐饮管理和营销方案
  • 浙江省住房和城乡建设部网站/公司网站排名
  • wordpress装插件吗/seo收费标准
  • ubuntu22.04系统入门 linux入门(二) 简单命令 多实践以及相关文件管理命令
  • 【云计算】云主机的亲和性策略(二):集群节点组
  • 企业智脑1.3.1技术升级全面解读:AI笔记引擎如何重塑企业知识管理范式
  • Vue3+Vite项目如何简单使用tsx
  • ECMAScript2024(ES15)新特性
  • 大语言模型(LLM)技术架构与工程实践:从原理到部署