Archive for the ‘Research Ideas’ Category.

Generic Operational Switch in C#

 The basis of this code derives from this posting on MSDN entitled Case of Construct where the original poster described a switch statement that had its roots in Lisp. Obviously anything Lisp-ish is not going to be found in C# but that doesn’t mean the bridge can’t be built.

The idea was that unlike a normal switch that keys off of one value, what if an operation could be executed for each of the case statements. If that operation is true, the body of the case would be executed and operations would be stopped. If it was false, the body would not be executed and the next case value would be checked.

The thought intrigued me enough, for I was working on a Dictionary of Delegates which I hope to blog about later, and it seemed to blend in with that topic I decided to do it in C#. At the risk of losing sleep I achieved my goal. (I still get this odd feeling that someone will post, “but it can be done like…”).

Now granted this is more an esoteric exercise than something of use…but hey one never knows.


Phase 1
Create generic structure to hold the case check and case operation

My design has a generic struct that will hold two delegates. Technically this can hold any item but I pulled it from another area and did not want to change it

public struct Operation<T1, T2>
{
    public Operation(T1 f, T2 s)
    {
        first = f;
        second = s;
    }
    public T1 First
    {
        get { return first; }
    }
 
    public T2 Second
    {
        get { return second; }
    }
 
    private readonly T1 first;
    private readonly T2 second;
 
}

Phase 2
Define The Delegates

This is self explanatory, the first delegate is the check method and the second is the operation to be performed if the sibling works.

public delegate bool EventHandlerCheck();     // Delegate to do the check returns
public delegate void EventHandlerOperation(); // Delegate to do the work

 Phase 3
Define the Operations Switch Class This is where the magic happens. It holds the generic struct of delegates and ultimately performs the switch test operation.

public class OperationsSwitch
{
    // Holds the operations and checks.
    private List<Operation<System.Delegate, System.Delegate>> _Operations = new List<Operation<Delegate, Delegate>>();
 
    // Load the checks and operations here.
    public Operation<System.Delegate, System.Delegate> Operations
    {
        set
        {
            _Operations.Add(value);
        }
    }
 
    // The primary workhorse of the class to do the switch operations.
    public void DoSwitch()
    {
 
        foreach (Operation<System.Delegate, System.Delegate> opPair in _Operations)
            if (((EventHandlerCheck)opPair.First)())
            {
                ((EventHandlerOperation)opPair.Second)();
                break;
            }
    }
}

Phase 4
Create a Test Class This class will create the main switch object and load it with two cases. The first case test will return false and the second returns true and will have its operation done.

public static class TestSwitch
{
    public static void RunTest()
    {
        OperationsSwitch lSwitch = new OperationsSwitch();
 
        lSwitch.Operations
            = new Operation<System.Delegate, System.Delegate>
               (new EventHandlerCheck(TestSwitch.Delegate1Check),
                new EventHandlerOperation(TestSwitch.Delegate1Op));
 
        lSwitch.Operations
           = new Operation<System.Delegate, System.Delegate>
              (new EventHandlerCheck(TestSwitch.Delegate2Check),
               new EventHandlerOperation(TestSwitch.Delegate2Op));
 
        lSwitch.DoSwitch();
 

    }
 
#region Case 1
    public static bool Delegate1Check()
    {
        return false;
    }
 
    public static void Delegate1Op()
    {
        System.Console.WriteLine("I have been switched 1");
    }
#endregion
 
 
#region Case 2
    public static bool Delegate2Check()
    {
        return true;
    }
 
    public static void Delegate2Op()
    {
        System.Console.WriteLine("I have been switched 2");
    }
#endregion
 
}

Running
What did I learn

 Its an interesting exercise, and yes it does seem to be esoteric, but if one could incorporate it into some event structure…it might have some value in a class…your thoughts?

Share