Click here to Skip to main content
11,488,646 members (75,514 online)
Click here to Skip to main content
Add your own
alternative version

Comparison between Different Methods to Iterate Over a List of Items

, 7 Mar 2011 CPOL 15.5K 130 17
Comparison between different methods to iterate over a list of items and see which method is the most effective
LoopExecutionComparison.zip
LoopExecutionComparison
Local.testsettings
LoopExecution.Application
LoopExecution.Application.csproj.vs10x
Properties
LoopExecution.Library
LoopExecution.csproj.vs10x
LoopExecution.Library.csproj.vs10x
Properties
LoopExecution.Test
LoopExecution.Test.csproj.vs10x
Properties
LoopExecutionComparison.suo
LoopExecutionComparison.vsmdi
TestResults
ThreadResultsWriteLine.csv
ThreadReultsSleep.csv
TraceAndTestImpact.testsettings
using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace LoopExecution.Application
{
    /// <summary>

    /// Arguments class

    /// </summary>

    public class Arguments
    {
        // Variables

        private StringDictionary Parameters;

        // Constructor

        public Arguments(string[] Args)
        {
            Parameters = new StringDictionary();
            Regex Spliter = new Regex(@"^-{1,2}|^/|=|:",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);

            Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);

            string Parameter = null;
            string[] Parts;

            // Valid parameters forms:

            // {-,/,--}param{ ,=,:}((",')value(",'))

            // Examples: 

            // -param1 value1 --param2 /param3:"Test-:-work" 

            //   /param4=happy -param5 '--=nice=--'

            foreach (string Txt in Args)
            {
                // Look for new parameters (-,/ or --) and a

                // possible enclosed value (=,:)

                Parts = Spliter.Split(Txt, 3);

                switch (Parts.Length)
                {
                    // Found a value (for the last parameter 

                    // found (space separator))

                    case 1:
                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter))
                            {
                                Parts[0] =
                                    Remover.Replace(Parts[0], "$1");

                                Parameters.Add(Parameter, Parts[0]);
                            }
                            Parameter = null;
                        }
                        // else Error: no parameter waiting for a value (skipped)

                        break;

                    // Found just a parameter

                    case 2:
                        // The last parameter is still waiting. 

                        // With no value, set it to true.

                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter))
                                Parameters.Add(Parameter, "true");
                        }
                        Parameter = Parts[1];
                        break;

                    // Parameter with enclosed value

                    case 3:
                        // The last parameter is still waiting. 

                        // With no value, set it to true.

                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter))
                                Parameters.Add(Parameter, "true");
                        }

                        Parameter = Parts[1];

                        // Remove possible enclosing characters (",')

                        if (!Parameters.ContainsKey(Parameter))
                        {
                            Parts[2] = Remover.Replace(Parts[2], "$1");
                            Parameters.Add(Parameter, Parts[2]);
                        }

                        Parameter = null;
                        break;
                }
            }
            // In case a parameter is still waiting

            if (Parameter != null)
            {
                if (!Parameters.ContainsKey(Parameter))
                    Parameters.Add(Parameter, "true");
            }
        }

        // Retrieve a parameter value if it exists 

        // (overriding C# indexer property)

        public string this[string Param]
        {
            get
            {
                return (Parameters[Param]);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Bookatz
Software Developer
United Kingdom United Kingdom
Software Engineer with 10 years of experience. Core focus is to meet clients real business needs through initial consultation, followed by a communicative and collaborative approach. Delivers applications using C# .net and MS SQL Server. Comfortable working independently, in a team, or mentoring to meets deadlines irrelevant of pressure.
Follow on   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 8 Mar 2011
Article Copyright 2011 by Michael Bookatz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid