在微软的框架中我们知道有一个的方法,今天我们来实现一个LINQ风格的基于集合Replace元素的扩展方法。首先来看一下,简单类图:
下面是主要代码片断:
1: ///
2: /// ReplaceExtensions which is LINQ Style Replace Operator
3: ///
4: public static class ReplaceExtensions
5: {
6: ///
7: /// Replaces the specified sequence.
8: ///
9: ///
10: /// The sequence.
11: /// The find.
12: /// The replace with.
13: /// The comparer.
14: ///Collection of type T
15: public static IEnumerableReplace (
16: this IEnumerablesequence, T find, T replaceWith, IEqualityComparer 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: ///
25: /// Replaces the specified sequence.
26: ///
27: ///
28: /// The sequence.
29: /// The find.
30: /// The replace with.
31: ///Collection of type T
32: public static IEnumerableReplace (
33: this IEnumerablesequence, T find, T replaceWith)
34: {
35: return Replace(sequence, find, replaceWith, EqualityComparer.Default);
36: }
37:
38: ///
39: /// Replaces the impl.
40: ///
41: ///
42: /// The sequence.
43: /// The find.
44: /// The replace with parameter
45: /// The comparer.
46: ///Collection of type T
47: private static IEnumerableReplaceImpl (
48: IEnumerablesequence, T find, T replaceWith, IEqualityComparer 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: }
从代码应该不难看懂,我们使用 然后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: }
基于的单元测试,您了解的话,应该不难看懂上面的代码。 希望对您CSharp编码有帮助。
您可能感兴趣的文章:
作者: 出处: 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 该文章也同时发布在我的独立博客中-。