Implicitly Typed Local Variables

Earlier we saw a new feature “Automatic Properties” in C# 3.0. Today we are going to see the use of new ‘var’ keyword.

C# 3.0 allows local variables in methods to have an implicit type by identifying it from the type of its initial value. For instance,

var str = “Adil“; // str is compiled as a string

var col = new[] { 0, 1, 2 }; // col is compiled as int[]

var anoType= new { Name = “Terry“, Age = 34 }; //anonymous type

When you call the GetType method on str, it will return System.String type.

A point of consideration is that var keyword is not like var in javascript and it does not mean that variable is late-bound. It is actually a keyword for compiler to determine and assign the type with respect to the value initialized.

Also note that var keyword cannot be used in class variables and also in passing variables to method or returning from method. It does not support nullable type as well. Further you need to give it some value at the time of declaring otherwise it will be an error. Let say,

var someVar;  someVar= 1; // not allowed

The var keyword does not impress until it is used with LINQ. We will see it’s use with LINQ in some later post.

Design Patterns

Design Patterns are reusable solutions to a commonly occurring problem in software. It’s not a ready to use code instead it is more like a general template or solution for a particular problem. When you get understanding of Design Patterns then at a time designing solutions you can come across a situation like “Oh Yes! I can apply here that particular design pattern” 🙂

Design Patterns provides you a proven and tested solution to a particular problem. They speed up design process and hence enhances productivity. I’m not going to give detail introduction to Design Patterns as purpose here is to understand and see how we can apply a particular design pattern in a language here C#. Briefly, there are four basic categories of Design Patterns

  • Creational Patterns
  • Behavior Patterns
  • Structural Patterns
  • Concurrency Patterns

Personally, I have more hands on of Creational Patterns. There are numerous design patterns in each category. We will look some of the more common in them.

So Let’s begin with a very simple design pattern which I learnt first and i.e. Singleton.

Singleton Design Pattern

Singleton ensures a single instance of the class and have global access. To apply singleton to a class we make it’s constructor(s) private and the class itself creates its object. For external world to get the object of the class it uses a static method that returns the object. Lets see the code below:

public class Singleton
private static Singleton instance;

private Singleton() {}

public static Singleton GetInstance()
if (instance == null)
instance = new Singleton();
return instance;

Here you can see, the class has a static object of itself. Whenever the want the instance of that class you will call GetInstance Method. If the object is not initialized, the method will initialize itself or it’s done already it will give you the same object. In this way, the class will ensure a single instance and will provide a global access through static method GetInstance.

Note that it is the simple way of creating Singleton class, There are optimized version as well that provides multi-thread safe version by using lock mechanism of C# and .NET. For details please visit Implementing Singleton in C#

In the coming posts, we will see different design patterns belonging to different categories. Besides, a very good material dedicated to Patterns and Practices is available on MSDN.

Bye for now! Have a good time.

Seminar on Microsoft Certifications

I recently gave a session on ‘Microsoft Certifications’ at DeXpert, Adil-PresentingKalSoft. This talk was arranged under ‘Emerging .NET Devs’ a chartered group under INETA. The targeted audience were developers.  The seminar was open and free registration was required to attend the show 🙂

For details, please visit “Emerging .NET Devs” user group blog here. The next event will be announced on yahoo group.


Adil Mughal

Binding Enum to ComboBox


Whether it’s Windows Forms or Windows Presentation Foundation While working with ComboBox, we usually want to bind any list as ComboBox Items.

For example,

//In windows forms
string[] names = { “Microsoft”, “IBM”, “Apple” };
cmbNames.DataSource = names;  //cmbNames is comboBox ID


//In windows presentation foundation
string[] names = { “Microsoft”, “IBM”, “Apple”};
cmbNames.ItemsSource = names;  //cmbNames is comboBox ID

That’s simple! But today I wanted to convert enum constants into an array of string and I wanted to make it ItemsSource of combo. So I created an enum type “Names” and tried that

public enum Names


Names nameList;
cmbNames.ItemsSource = nameList ; //compile time error-

A compile time error is raised claiming that Cannot implicitly convert type ‘Names’ to ‘System.Collections.IEnumerable’. So i searched for that and find a good single line solution 🙂

//For WPF
cmbNames.ItemsSource = Enum.GetNames(typeof(Names));

//and In windows forms
cmbNames.DataSource = Enum.GetNames(typeof(Names));

It will convert enum contants into string[]. To convert back from string to constant you can use Enum.Parse(…).

Nice Feature! Isn’t it?

INETA User Group


Few weeks earlier I talked to Microsoft MVP Mr. Wajahat Abbas for I wanted to contribute to community in the form of seminars and workshop specially for the student developers. So INETA User group provides a way for that and he told me that he can arrange my sessions at Microsoft Innovation Center at Karachi. Thanks for your kind advice Wajahat 🙂

After that I communicate within my friends circle and they also appreciated it. So I registered an INETA User Group “Emerging .NET Devs”. For details please visit and to collaborate further join our yahoo group

We intend to arrange a good seminar at Department of Computer Science, University of Karachi in the late February or start of March. Also in the coming week or next one, I’m going to deliver a session of “Microsoft Certifications”.

To stay update to date, please visit our user group blog

Have a great day!

Generic Methods in C#


One of the great features of C# is Generic Method. Back in sometime I wrote a Generic Method which serializes or de-serializes an object. The Generic methods are declared with type parameters. So here is the code snippet I wrote some time earlier:

public static void DoSerialization(Stream stream, SerializationType sType, ref T objToUse)
if (objToUse == null)
throw new Exception(“Missing object to serialize/De-serialize”);
XmlSerializer serializer = new XmlSerializer(typeof(T));
if (sType == SerializationType.Serialization)
serializer.Serialize(stream, objToUse);
else if (sType == SerializationType.DeSerialization)
objToUse = (T)serializer.Deserialize(stream);
catch (Exception ex)
{ //catching of exceptions

Let me explain this code. The method takes stream to be de-serialized or serialized depending upon the second argument. SerializationType is enum Type something like

public enum SerializationType

and finally the reference of object to serialize or de-serialize. Well let me tell you due to support of type parameter, I was able to make a general function which will serialize or de-serialize any type of object and saved me from re-writing methods for different data type and at the same time its type safe as well 🙂

Isn’t it a great feature. If you do not use Generic Method, start considering them.

Guideline to Prepare for Microsoft Certifications


Couple of days ago my friend ask me about preparation of MCTS 70-536 and its material. As promised in earlier post “Getting Started with Microsoft Certifications Part II” , I’m just going to share my personal experience and perception. So lets discuss some key points in preparation for certifications

  • Make up track in your mind
    • First of all, you must have a proper track to follow. When I say track I mean which area your interested in and also in which you are most interested as well
    • Start with the program in which you are most interested, For this will ease in your preparation and will definitely give you a good boost 🙂
  • Start reading training kit
    • If you have done some work on that technology, obviously which is recommended as well, for instance WPF Now for just clearing certifications I believe Training Kits are good enough to prepare
    • In order to have some good understanding of technology I think one should go for other books as well instead of just going through Self Paced Training Kits
  • Self motivation
    • Until or unless you do have motivate yourself for preparing and giving for exam, You are definitely tend to be sluggish learner.
    • For me It was my profile, I wanted to MCP or MCTS etc 🙂
    • Thanks to my workplace as well i.e. KalSoft for where I found number of MCTS’s, MCPD’s and MVP’s as well, They definitely gives you inspiration
  • Allocate some time on regular basis
    • Once you are ready to start, I would recommend you to allocate some time on regular basis
    • For instance, one hour daily after dinner or in the evening
  • It’s difficult to get out time
    • With job its not easy to take out time, so what if you can read while traveling than its a good way to utilize time and study
  • Register your self and Enforce preparation
    • If you find a good offer like second shot etc then register your self within 1-1.5 month(s) ahead.
    • That would pressurize you and will made you prepare for the exam

These were some of the points I wanted to share with you all and that’s how I succeeded. Remember it varies from individual to individual, every one has their own way of learning.

That’s it for now. Take care


Getting Started with Microsoft Certification Part II


Back in some time, I wrote a blog on Getting Started with Microsoft Certification but as time changes, things changes. So I want to modify information regarding Microsoft certification program and want to add further emphasis on Visual Studio 2008 Track.

First the overall program showed in earlier post requires a little amendment, Now Microsoft has included Master series in the overall certification track

           Microsoft Certifications

We will now talk about MCTS for Visual Studio 2008. For overall glossary of Microsoft Certifications please visit

Microsoft Certified Technology Specialist (MCTS):

MCTS basically highlights your area of expertise so that you can demonstrate your breadth of knowledge on Visual Studio. You stand out to your managers and you implicitly tell them that you know 🙂

The Microsoft Visual Studio 2008 certification tracks are based on six (MCTS) certifications. Each certification focuses on a particular key technology area(s) in the Microsoft .NET Framework 3.5.

For different credentials, there are different exams required to pass.


So the first step for Developers must be the pre-requisite of all MCTS 70-536 exam. This exam is designed to measure your knowledge of .NET development fundamentals. So whether you are interested in WPF, WCF, ASP.NET or any other, you are required to give MCTS 70-536


For details related to MCTS 70-536 exam. Please visit The link will lead you to a page having details of 70-536 exam including the skills that are measured in this exam.

Although the order are NOT important in MCTS exams. Like for example, you can give MCTS 70-562 first and then 70-536 and once you will pass both exam you will earn MCTS ASP.NET 3.5 credential. So order does not matter here. Though some people suggest that since 70-536 covers wide areas of .NET framework, its better to give other exam first so that it can give you boost but I didn’t take it that way 🙂 I think one should give 70-536 first so that for every next paper you give, it will give you respective credential. Does this make sense to you people? 🙂

This is it for now. In this post we saw the new Visual Studio 2008 track, MCTS in particular and we saw that for every credential MCTS 70-536 is pre-requisite. If you have any query against this post, please feel free to ask. You can comment or send me an email at

In the next post on Microsoft Certifications, We will see some of my experiences related to preparing for exam(s). Till then Good bye and Enjoy Programming 🙂