Execute code in parallel using Parallel.For

If you’ve ever had code that executes repetitive tasks, consider executing them in parallel using the ParallelFor method in the System.​Threading.​Tasks Namespace.

In this example, I’ve made a dinner decision maker, that randomly decides what to eat for dinner for the next N days.  In our program, we’ll randomly select from dinner choices, and add them to a dictionary.

Its best to use a Concurrent Dictionary as a thread safe collection to add items to during a parallel loop.   Additions to the collection are performed by TryAdd().  We’ll add unique keys and values to the collection potentially at the same time.  The TryAdd() method returns true if the key/value pair was added successfully, false if already exists.  Keep in mind executing  code in parallel is not always the right choice, and can lead to some pitfalls.

In the code below, we’re executing a code block in parallel.  I’ve made the current thread sleep for a random amount of time inside the parallel block, as it seems to generate a more wide variety of dinners.  The code block could have been a named function instead.  The point is that the operating system will decide if and how to execute the code at run time.  It may result in a not so improved performance, or it may not.

 

Abstract Classes vs Interfaces in C#

When should you use an abstract class, when should you use an interface?  Here’s the lowdown on some of the reasons to choose one over another.  First, take a look at a very simple example of an abstract class and two classes that inherit from it.

   public abstract class ToastBase
    {
        public ToastBase()
        {
            Console.WriteLine("Toast bread in toaster");
        }

        public abstract void AddSpread();
    }

    public class PeanutButterToast : ToastBase
    { 
        public override void AddSpread()
        {
            Console.WriteLine("Spread peanut butter on toast");
        }
    }

    public class JellyToast : ToastBase
    {
        public override void AddSpread()
        {
            Console.WriteLine("Spread jelly on toast");
        }
    }

Setting aside technical reasons, one of the strongest benefits to using an abstract class is the ability to provide an invariant implementation of some functionality across all sub classes, while leaving other implementations up to the subclass.  In our example above, all types of toast must first be toasted in a toaster.  Then, depending on the type of toast, the subclass can add whatever type of spread it wants.

On to the technical side.  An abstract class cannot be instantiated.  It is meant to be inherited.  If a method is not marked as abstract in an abstract class, the implementation belongs to the abstract class.  Abstract methods are implicitly virtual, and are meant to be overridden by the subclass.  They have no implementation in the base class, and therefore no method body, just a signature.  An class can inherit from multiple interfaces, but only one abstract class.  Here’s a more real world example.  Imagine a website back end where we want to collect and process credit cards for online purchases.  We might implement an abstract class such as the following:

  public abstract class PaymentBase
    {

        public void ProcessPayment(int paymentID)
        {
            PaymentProcessor.SendPaymentRequest(paymentID);
            PaymentProcessor.HandlePaymentResult(paymentID);             
        }

        public abstract void CollectCardInfo();
    }

The abstract PaymentBase class dictates that no matter what way we collect card information in the front end website, we must always process the payment the same way.

Interfaces are like contracts that dictate all the functionality a class must have that implements it.  When you implement an interface, you must define a method body for all the method signatures in the definition.  Interfaces are great for dependency injection, which states that we should always program to an interface, not an implementation.  This is beyond the scope of this article, but warrants further reading if you’re not familiar with it.

Interfaces enable you do decorate additional classes of the same concept, adding functionality.  Consider the example below.  We have a Customer and a LoyalCustomer class.  They both implement the ICustomer interface.  While we can place an order for each type of customer using the same interface, we can apply a discount to a loyal customer.  Meanwhile, all throughout our application, we can use the ICustomer interface anywhere we refer to a customer, and pass either Customer or LoyalCustomer to the method.  This also allows us to extend Customer to additional custom types down the road.

   public interface IOrder
    {
       int OrderID { get; set; }
       decimal orderAmount { get; set; }
    }

    public interface ICustomer
    {
        void PlaceOrder(IOrder order);
    }
   
    public class Customer : ICustomer
    {
        public void PlaceOrder(IOrder order)
        {
            //work with order properties, etc
            int i = order.OrderID;
        }
    }

    public class LoyalCustomer : ICustomer
    {
        public void PlaceOrder(IOrder order)
        {
            //apply discount
            order.orderAmount -= 5;
        }
    }

That’s all for now.  I hope you enjoyed this post!