Codeproject article about Template method pattern in C#

I posted another article about Template method pattern and Action in C# in the Codeproject. It could be found in here.

Codeplex - .NET Utility Library project

I just published a project in Codeplex. The project name is DotNetUtility - a .NET Utility Library for C#. The project URL is DotNetUtility - a .NET Utility Library for C#. This is open source and if any one willing to contribute please let me know.

Code project - Caching uses with Proxy pattern in C#

I posted another article about improvement of Proxy Pattern by using Caching technique in C#. It could be found in Caching uses with Proxy pattern in C#.

return new Lazy(() => new T()); /* Lazy initialization of objects in C# */

We should try to initialize objects when we need it otherwise the objects will reside in the memory. Probably it won't matter when the object is not that expensive otherwise it really matters. So if we could use something which give us the opportunity to initialize objects only when we need it or probably Singleton pattern or something like this. In .NET 4.0, there is new feature which will do the job for us. That feature is Lazy<T>, following code block will show the use of it,
static Lazy<T> LazyLoader<T>() where T : new()
{
    return new Lazy<T>(() => new T());
}
 
static Lazy<T> LazyLoader<T>(Func<T> lazyInitialisationBlock) where T : new()
{
    return new Lazy<T>(lazyInitialisationBlock);
}
 
static List<string> InitialisationBlock()
{
    return new List<string> { "One""Two" };
}
Usage of the above code,
static void Main(string[] args)
{
   Lazy<ClassForLazyLoad> loader = LazyLoader<ClassForLazyLoad>();
   Console.WriteLine("{0}", loader.Value.Name);
   Lazy<List<string>> listLoader = LazyLoader<List<string>>(InitialisationBlock);
   Console.WriteLine("{0}", ReferenceEquals(listLoader, null) ? "Loading hasn't been finished yet" : "Loading has been finished");
   Array.ForEach(listLoader.Value.ToArray(), item => Console.WriteLine(item.ToString()));
}
related classes,
public class ClassForLazyLoad
{
   public string Name { get { return GetType().FullName; } }
}

Codeproject articles - Strategy method or Switch statement in C#

I just published another article in the code project. This article is about Strategy method which can be used to replace the switch statement in C#. That article could be found,

Strategy method or Switch statement in C#

A Generic Strategy Pattern implementation using C#

Strategy Pattern - according to this pattern, there will be an interface, few concrete classes which will implement the interface and there will be selection process of the concrete classes. There is good definition in here.

I created a Generic class for Strategy Pattern. Generic Strategy Pattern class will accept the type of Concrete class and types of the parameters and it will take parameter for the argument of the concrete class.

Usage of the StrategyPattern,
 class Program
 {
   static void Main(string[] args)
   {
     int resultOfInt = StrategyPattern.StrategyAddExecutor<intIntProgrammingCalculatorImp>(1, 2);
     string resultString = StrategyPattern.StrategyAddExecutor<stringStringProgrammingCalculatorImp>("1AA2AA""2");
   }
 }
The Generic StrategyPattern class will implement the access of different methods defined in the IProgrammingCalculator interface for instance in here Add and Sub methods. In here T denotes the type of parameter of the methods and S denotes the concrete class type.
 public static class StrategyPattern
 {
    public static T StrategyAddExecutor<T, S>(T firstItem, T secondItem)
        where S : IProgrammingCalculator<T>, new()
   {
        return new S().Add(firstItem, secondItem);
   }
 
   public static T StrategySubExecutor<T, S>(T firstItem, T secondItem)
       where S : IProgrammingCalculator<T>, new()
   {
       return new S().Sub(firstItem, secondItem);
   }
 }
IProgrammingCalculator defines the definition of the calculator. There three different types of calculator IntProgrammingCalculator, LongProgrammingCalculator and StringProgrammingCalculator with its own implementation of the methods defined in the IProgrammingCalculator interface. 
 public interface IProgrammingCalculator<T>
 {
     T Add(T firstItem, T secondItem);
     T Sub(T firstItem, T secondItem);
 }
 
 public class IntProgrammingCalculatorImp : IProgrammingCalculator<int>
 {
     public int Add(int firstItem, int secondItem)
     {
         return firstItem + secondItem;
     }
 
     public int Sub(int firstItem, int secondItem)
     {
         return secondItem - firstItem;
     }
 }
 
 public class LongProgrammingCalculatorImp : IProgrammingCalculator<long>
 {
     public long Add(long firstItem, long secondItem)
     {
         return firstItem + secondItem;
     }
 
     public long Sub(long firstItem, long secondItem)
     {
        return secondItem - firstItem;
     }
 }
 
 public class StringProgrammingCalculatorImp : IProgrammingCalculator<string>
 {
     public string Add(string firstItem, string secondItem)
     {
       return firstItem + secondItem;
     }
 
     public string Sub(string firstItem, string secondItem)
     {
       return firstItem.Replace(secondItem, string.Empty);
     }
 }
There are few good articles in the web for further reading,

dofactory.com
Strategy pattern in the wikipedia

Codeplex, few new extension methods in .NET Extensions Methods Library for C# and VB.NET

I have added few extension methods to the project. For instance, in the BooleanExtensions class I added ToBinaryTypeNumber and CombineWith method in the ArrayExtension class.

Source code is in the Change Set 62750 and 62790.