Q:

List Execution Order

I've got a generic interface with a method on it that returns a int value such as in the following (simplified) code. It's important to me that these should be called in a specific order (e.g. ClassA always needs to be called before ClassB). How would I go about ensuring this ordering is always correct. Relying on the list creator is not the best way to do it?
Thanks.
public interface IMyInterface
{
    int DoWork();
}

public class MyClassA : IMyInterface
{
    private int _myAccumulator = 100;

    public int DoWork()
    {
        _myAccumulator -= 1;

        return _myAccumulator;
    }
}

public class MyClassB : IMyInterface
{
    private int _myAccumulator = 50;

    public int DoWork()
    {
        _myAccumulator -= 1;

        return _myAccumulator;
    }
}

public class MyWorker
{
    private List<IMyInterface> _myAccumulatorClasses = new List<IMyInterface> { new MyClassA(), new MyClassB() }

    public void CallClasses()
    {
        foreach(var accumulator in myAccumulatorClasses)
        {
            var value = accumulator.DoWork();

            if(value > 0)
                break;  // Don't call next accumulator if we get a value greater than zero back.
        }

    }
}

A:

You could add an Order property to your interface:
public interface IMyInterface
{
    int DoWork();
    int Order { get; }
}

Then, in your implementation:
public class MyClassA : IMyInterface
{
    private int _myAccumulator = 100;

    public int DoWork()
    {
        _myAccumulator -= 1;

        return _myAccumulator;
    }

    public int Order {get { return 1;} }
}

And finally, OrderBy when you iterate:
public class MyWorker
{
    private List<IMyInterface> _myAccumulatorClasses = new List<IMyInterface> { new MyClassA(), new MyClassB() }

    public void CallClasses()
    {
        foreach(var accumulator in myAccumulatorClasses.OrderBy(x=>x.Order)))
        {
            var value = accumulator.DoWork();

            if(value > 0)
                break;  // Don't call next accumulator if we get a value greater than zero back.
        }

    }
}

This is the safest way to ensure order.
However, List<> guarantees order on insert.
So if you insert in a particular order, they will come out in that order:
var list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");
list.Add("4");

foreach (var element in list)
{
    Console.WriteLine(element);
}

Output:

1
  2
  3
  4  

