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()Usage of the above code,
{ 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" }; }
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; } } }
Subscribe to:
Posts (Atom)