Skip to main content

Posts

The Dynamic Primitive Type in C#

Runtime type identification made easy! Read on how new ‘dynamic’ type helps. Let us look at the problems in versions prior to 4.0. A lot of times, we create programs that act on information unaware of its type until at runtime. A type-safe programming language like C# works with runtime-determined information during reflection and has to work a lot with strings which looks clumsy and cause performance problems as shown in the below example. class  Program {            public class  MyType          {              public string  MyProperty { get; set; }           }           static void  Main( string [] args)          {              var myType = new MyType();              var property = typeof(MyTestType).GetProperty("MyPro...

Task Parallel Library

Problem: One of the clients was running the multi threaded application on a single core machine, he upgraded his system to Quadra-core and he observed that the application did not improve or started degrading the performance. The development team later found the reason that applications were not utilizing the benefits of cores and it was still running in single core. Solution: .Net 4.0 has brought a new Task Parallel library which uses the benefits of multi core. The Library efficiently utilizes the system resources to give better performance. Internally it uses Threadpool with improved algorithm with .Net 4.0 Namespace :  System .Threading. Tasks Class  - Task Create Task Task t1 = new Task(() => Add(1,100)); t1.Start(); t1.Wait(); //Wait till task t1 is completed and then starts t2 Task t2 = new Task(() => Add(101, 200)); t2.Start(); //Other way of creating multiple task var  tasks = new Task[2]; tasks[0] = Task.Factory.StartNew(() => Add(1, 100...

Finalize, Dispose and Unmanaged Resources

There may be situations where some class has control over resources that are not managed by the runtime such as window handles, database connections, etc. and these resources need to be released as they occupy more memory or affect performance. It can be done explicitly by calling Dispose () to release these objects immediately when they are no more needed. Similarly these objects can also be released implicitly by implementing Finalize() so that they get collected by GC in case someone forgets to call dispose () on these objects. Also since calling Finalize impacts the performance, calls to Finalize can be prevented by invoking the GC.SuppressFinalize method but it should be made sure that it only gets called after Dispose method has completed successfully. Public class  Base:IDisposable {        Private bool disposed = false;       //Implement IDisposable.        Public void  Dispose()       ...

SOLID - Single Responsibility Principle(SRP)

The Single Responsibility Principle is one of the SOLID design principles. We can define it in the following ways, A reason to change A class or method should have only one reason to change. Single Responsibility A class or method should have only a single responsibility. When a class has more than one responsibility, there are also more reasons to change that class. Figure 1 - Single Responsibility Principle SRP Benefits The SRP has many benefits to improve code complexity and maintenance. Some benefits of SRP are following, Reduction in complexity of a code A code is based on its functionality. A method holds logic for a single functionality or task. So, it reduces the code complexity. Increased readability, extensibility, and maintenance As each method has a single functionality so it is easy to read and maintain.  Reusability and reduced error As code separates based functionality so if the same functionality uses somewhere else in an application then don...