.Net Code Monkey RSS 2.0
 Friday, April 11, 2014

IEnumerableExtensions.ForEach<T>


I recently required to be able to iterate through an IEnumerable object and perform an action so have built a simple extension for the IEnumerable class.

using System;
using System.Collections.Generic;

namespace Dibware.Extensions.System.Collections
{
    /// <summary>
    /// Provides extension methods to the IEnumerable class
    /// </summary>
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="action"></param>
        public static void ForEach<T>(this IEnumerable<T> source,
            Action<T> action)
        {
            foreach (T element in source)
            {
                action(element);
            }
        }
    }
}

Unit Tests


using Dibware.Extensions.System.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Dibware.Extensions.Tests.Collections
{
    /// <summary>
    /// Tests extensions to the IEnumerable class
    /// </summary>
    [TestClass]
    public class IEnumerableExtensionsTests
    {
        #region ForEach

        [TestMethod]
        public void Test_ForEach_PerformActionOnListUsingForEach_ResultsIn_TheActionBeingPerformed()
        {
            // Arrange
            IEnumerable<String> list =
                new List<String>() { "One", "Two", "Three" };
            var result = String.Empty;

            // Act
            // Create an action that concatenates each item in 
            // the list using commas.  A Trailing comma will be present.
            Action<String> action = s => result = String.Format("{0}{1},", result, s);

            // Perform the action on the list using the ForEach we are testing
            list.ForEach(action);

            // Remove the last comma
            result = result.TrimEnd(',');

            // Assert
            Assert.AreEqual("One,Two,Three", result);
        }

        #endregion
    }
}

GitHub

This project is available on GitHub, here: https://github.com/dibley1973/Dibware.Extensions

Friday, April 11, 2014 4:39:42 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
C# | On My GitHub | Extensions | IEnumerable
 Wednesday, March 26, 2014
Part of the project I have been working on has required some simple helper classes , so I have split these out of the main project into their own free standing assembly and added them to GitHub here:

https://github.com/dibley1973/Dibware.Helpers

At the time of writing the solution only has some basic helpers, but all future useful helpers that I write will be added to this project.

A unit test project is provided in the solution. Please feel free to browse, comment or even use.

Guard.cs

The static Guard class was written to make writing argument guard clauses at the top of methods much more succinct like so.

public override void OnAuthorization(AuthorizationContext filterContext)
{
    Guard.ArgumentIsNotNull(filterContext, "filterContext");
    .
    .
    .
}

EnumHelper.cs

The static EnumHelper class was written to encapsulate enum iteration and also some functionality that I can never remember the syntax for enums! They could probably dow with being written as extension methods of System.Enum




Wednesday, March 26, 2014 6:45:45 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
C# | Enum | Guard Clauses | Unit Tests
 Monday, March 24, 2014
Part of the project I have been working on has required some simple extensions, so I have split these out of the main project into their own free standing assembly and added them to GitHub here:

https://github.com/dibley1973/Dibware.Extensions

At the time of writing the solution only has some basic extensions, but all future useful extensions that i write will be added to this project.

A unit test project is provided in the solution. Please feel free to browse, comment or even use.

Monday, March 24, 2014 5:00:47 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.Net | C# | GitHub | Unit Tests
 Friday, March 21, 2014
I have been working on a private project for the last couple of months but one of my work colleagues suggested I break the project down into elements that can be made public and get them on to GitHub to allow people to use or contribute. The first of these elements is now up. Dibware.Web.Security

https://github.com/dibley1973/Dibware.Web.Security

Feel free to browse, comment or even use.

At the time of this post there are only two objects "WebPrinciple" and "WebIdentity". More will be added as they are worked upon.

Friday, March 21, 2014 5:35:07 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Asp.Net | C# | GitHub
Archive
<April 2014>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Blogroll
 Clemens Vasters
 Harry Pierson
Passion * Technology * Ruthless Competence
 Joshua Flanagan
A .NET Software Developer
 Michael Schwarz's Blog
Developing applications on the Microsoft platform since Windows 3.1!
 Omar Shahine
Yet another Microsoft blogger
 Scot GU
Scott Guthrie lives in Seattle and builds a few products for Microsoft
 Scott Hanselman
Programming Life and the Zen of Computers
 Tom Mertens
Tom's corner
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Duane Wingett
Sign In
Statistics
Total Posts: 49
This Year: 6
This Month: 1
This Week: 0
Comments: 43
Themes
Pick a theme:
All Content © 2014, Duane Wingett
DasBlog theme 'Business' created by Christoph De Baene (delarou)