Sunday, April 10, 2011

Overview of the C# version 1.0 to 4.0

Shout it kick it on DotNetKicks.com
In this article I am going to discuss about the some of the changes in C# language from the beginning to up till i.e C#1.0 to C#4.0. I am just sharing my personal experience what the evolution did by C# up till.

This post is just show the evolution in C# programming language. I am not going to show all features of the each version.

One day I and my (Girl Friend)GF went to the Cafe coffee day to drink coffee after completing office. At that day I carried the C# book in my bag, which I took office to explain the newer version to my colleague. She suddenly looked in my bag and which started below conversation.

GF: why do you always carry the C# book which marked with the different version on it?
Me: The C# developer team released around 4 major version of C# language. Every version comes up with some new feature(s) which makes development easy and simple. As well as cause me to do the change the code accordingly.
GF: How that can be possible every version release causes you to change your code?
ME: (on my laptop) ok. I will show what the revolution in language.

C#1.0
Specification of Version 1
Following is code for the Employee class for my system.
public class EmployeeCompare : IComparer
    {
        public int Compare(object x, object y)
        {
            Employee emp1 = (Employee)x;
            Employee emp2 = (Employee)y;
            return emp1.Name.CompareTo(emp2.Name);
        }
    }
Above code is for sorting employee on name property. You can see, I need to convert from general/generic object type into employee first, then i need to compare name property of employee objects

Note here I have implemented IComparer to sort employee.
public class Employee
    {
        string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        int _id;
        public int ID
        {
            get
            {
                return _id;
            }
        }
Above I have created two properties to store employee name and id, actually here private variable holding value of the property.
One more thing to note here is I have written get block to make ID readonly.
public Employee(int id, string name)
        {
            this._id = id;
            this._name = name;
        }
Below method create the list of the employee and storing it in the ArrayList.
public ArrayList GetEmployee()
        {
            ArrayList al = new ArrayList();
            al.Add(new Employee(1, "pranay"));
            al.Add(new Employee(2, "kruanal"));
            al.Add(new Employee(2, "hanika"));
            return al;
        }
But when I am adding employee object actually I am converting it in the object.
public void displayEmployee()
        {
            ArrayList alEmployee = GetEmployee();
            alEmployee.Sort();
            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }
Above I am requesting the employee class to display all Employee in sorted order by calling displayEmployee method.
Note here I am converting each object of ArraList in employee to display data.


GF: it’s some what similar to java /c++ class like other programming language.
Me:Now we moving to second version of language which change this class.
GF: let's see.

C#2.0
Specification of Version 2

As you see here now I can created IComparer with the specific Type like employee. This is because of the C#2.0 included new feature called Generic.
public class EmployeeCompare : IComparer<Employee>
    {
        public int Compare(Employee x, Employee y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }
Now to compare two employee object I no need to convert object to employee.
public class Employee
    {
        string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        int _id;
        public int ID
        {
            get
            {
                return _id;
            }
            private set
            {
                _id = value;
            }
        }
As you can see the properties above I can able to set the the access level to private which make the property readonly.
public Employee(int id, string name)
        {
            this._id = id;
            this._name = name;
        }
In GetEmployee now I can put the Employee objects directly as Employee because of the Generic feature.
public List<Employee> GetEmployee()
        {
            List<Employee> al = new List<Employee>();
            al.Add(new Employee(1, "pranay"));
            al.Add(new Employee(2, "kruanal"));
            al.Add(new Employee(2, "hanika"));
            return al;
        }
As Generic feature make the List typesafe and there is no need of converting object to employee. So to display employee I no need to covert List object to employee again.
public void displayEmployee()
        {
            List<Employee> alEmployee = GetEmployee();
            //alEmployee.Sort();

            alEmployee.Sort(delegate(Employee x, Employee y)
            { return x.Name.CompareTo(y.Name); });

            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }
As you can there is no need of the comparer to sort the object I can do it easily with the anonymous method feature of the language.

Me: So got something new and change of things.
GF: yeah......it's look like big change and cool.

Me: so we are moving on version three of the language.
GF : go on I like it eager to know what new now in this version.

C#3.0
Specification of Version 3
Now the below code is with the next new version of the language.
public class Employee
    {
        public string Name
        {
            get;
            set;
        }

        public int ID
        {
            get;
            private set;
        }
Well as you can see here there is no need of the private variable to hold the value of the property. This is because the new feature automic property of the language. This is usefull when I don’t have any logic to manipulate with the value.
Employee() { }

        public Employee(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
In GetEmployee now I do not need to add the employee object one by one that I can do easily with the object Inialization feature of new version.
public List<Employee> GetEmployee()
        {
            return new List<Employee>
            {
                new Employee{ Name="pranay", ID=1},
                new Employee{ Name="krunal", ID=2},
                new Employee{ Name="hanika", ID=3}
            };
        }
Now displayEmployee method here I do not need to implement IComparare or any anonymous method to sort data I can do it easily with the lamda expression or by using extension method provided by the this version of language.
public void displayEmployee()
        {
            List<Employee> alEmployee = GetEmployee();
            alEmployee.Sort((x,y) => x.Name.CompareTo(y.Name) );
            //alEmployee.OrderBy(e => e.Name);
            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }

Me:so like this new feature of this version.
GF:yeah...LINQ feature is just awesome. I just liked it much.

Me: Now the latest version of this language. This version is not doing major changes as its did by the previous version because the latest version more towards the dynamic types in language.
GF: Lets see.

C#4.0
Specification of Version 4
public class Employee
    {
        public string Name
        {
            get;
            set;
        }

        public int ID
        {
            get;
            private set;
        }
Now this version allows you to set the default parameter value for the argument in the function which is not possible with the previous version (Optional parameter). So the constructor for the employee looks like as below.
public Employee(string name, int id = 0)
        {
            this.ID = id;
            this.Name = name;
        }
Note by this feature I do not have to create extra overloading methods in my class.
In GetEmployee method I can set the value of variable by specifying name of the parameter of the function.
public List<Employee> GetEmployee()
        {
            return new List<Employee>           {
                new Employee(name : "pranay", id :1 ),
                new Employee(name : "krunal", id :2 ),
                new Employee(name : "hanika", id :3 )
            };
        }

        public void displayEmployee()
        {
            List<Employee> alEmployee = GetEmployee();
            //alEmployee.Sort((x,y) => x.Name.CompareTo(y.Name) );
            alEmployee.OrderBy(e => e.Name);
            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }
Me: so this version do the changes in the way I declare parameter in the method and the way I pass value to the method when call.
GF: I like this feature of the optional parameter and the way i can assign the value to parameter. This is really awesome each version of C# came up with the new feature and change the things.
GF: I just say woooo.... this means we can spend more time because each new version save your time.

3 comments:

  1. Very nice explanation.

    Could you please do the same for other features also.

    ReplyDelete
  2. Why don't you write a whole book on version comparison for C#.
    In that you should not teach C# but just have sections of what changed from 1.0 to till date version.
    Trust me it will be a big hit. Also big publishing companies or authors who write on C# would like to let their readers refer your book to know how and why the change took place.

    ReplyDelete