Skip to main content

Posts

Showing posts from November, 2012

Knowing Singelton

Singelton pattern in one of the most commonly used and talked about design pattern . Many appreciates it usefulness and many advocate against it. In this post we are not going to discuss all that but will concentrate on its implementation in c#. Type: It is a creational design pattern . Intent: It ensures that only one instance of the class exists. Usage: It is generally used when only one instance of a class is required. Thus, it can be seen to be used in logging frameworks, configuration objects etc. The ownership of instance creation lies with the class itself. This is because we can not ensure that only one instance will ever exists for the class if the responsibility lies with someone else. The class will instantiate the object when it will be used for the first time. This also ensures lazy initialization. Point to consider: Singelton object is mostly made accessible globally and thus being abused by being used as global variable. We must keep in mind that not onl

C# Polymorphism - handle with care

Download Overriding is the base for most of the design patterns which exists. It provides us with an essential tool called  Polymorphism  . What is  Polymorphism  ? Polymorphism means one interface and many forms. It is a characteristics of being able to assign a different meaning or usage to something in different contexts specifically to allow an entity such as a variable, a function or an object to have more than one form.  There are two types of Polymorphism.  Compile time:  function or operator overloading  Runtime:  Inheritence & virtual functions Here, we are going to talk about the Runtime Polymorphism.  If you read the definition carefully you will see that there is going to be some type casting and compiler is going to made some assumptions. These assumptions may fail on execution. Let me explain this further. Type casting: There will be a parent interface (Interface, abstract class, class) and may be many child classes. These child classes will impl

c# reference types passed by value or reference

Download Time and again I have heard that in C#, method arguments are passed based upon there type. i.e. Value types are passed by value and reference types are passed by reference. This is so untrue. Reference types have nothing to do with pass by value. Let us talk some basic. Consider the assignment first. When you assign a value type to another its value is copied. This is because value type variable contains its data directly. When you assign a reference type variable to another only the reference gets copied. This is because a reference type variable does not contains its data directly. It only holds reference to data. Thus, when we pass a value type, its value gets copied and when we pass a reference type its reference gets copied. That is why if we change the value of a value type variable in the block (method), the change is not seen outside of it. On the other hand if we change the value of member of reference passed to the block from within the block the change persi

Callback in c#

Download Running tasks in background is the need of the hour when working on real world applications. There are few tasks which we can fire and forget but for few of them we will like to receive the feedback. This is when callbacks comes into the picture and provides a channel for these objects to communicate with each other. These tasks are very common in desktop and mobile applications. In this post we will talk about callback mechanism in c#. In future post we will build upon this and do lot more. Using interfaces for callbacks in c# using System; using System.Threading; namespace Callbacks { class Program { static void Main(string[] args) { var consumer = new Consumer(); consumer.DoWork(); Console.ReadLine(); } } interface IOnProcessCompleteListener { void OnProcessComplete(string message); } internal class Consumer: IOnProcessCompleteListener { private Work