Naveen's Weblog

Bridge to future

Archive for December 24th, 2008

Whats new in C# 3.5

Posted by codingsense on December 24, 2008

The key features that are intorduced in C# 3.5 are

We will explore them in more depth in coming posts.. Till then let me find some simpler Samples to make you understand concepts better.. :-)

Posted in C# | Tagged: | Leave a Comment »

Object and Collection Initializers

Posted by codingsense on December 24, 2008


This is an interesting feature that will seem more valuable once you go through the below example.

Imagine a class Employee which has the properties like Name and ID To add two employees and to list them required the following code till c# 2.0.

using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp1 = new Employee("Naveen",1);
            Employee emp2 = new Employee("Jonny", 2);

            Console.WriteLine("Employee List :");
            Console.WriteLine("----------------");
            Console.WriteLine("ID          Name");
            Console.WriteLine("----------------");
            Console.WriteLine("{0}          {1}", emp1.ID, emp1.Name);
            Console.WriteLine("{0}          {1}", emp2.ID, emp2.Name);
            Console.Read();
        }

        public class Employee
        {
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }

            private int id;
            public int ID
            {
                get { return id; }
                set { id = value; }
            }

            public Employee(string Name, int ID)
            {
                this.Name = Name;
                this.ID = ID;
            }
        }
    }
}

Now have a look on whats new in c# 3.5. Life is simplified with the the Object Initailizers.

using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp1 = new Employee() { Name = "Naveen", ID = 1 };
            Employee emp2 = new Employee() { Name = "Jonny" };

            Console.WriteLine("Employee List :");
            Console.WriteLine("----------------");
            Console.WriteLine("ID          Name");
            Console.WriteLine("----------------");
            Console.WriteLine("{0}          {1}", emp1.ID, emp1.Name);
            Console.WriteLine("{0}          {1}", emp2.ID, emp2.Name);
            Console.Read();
        }

        public class Employee
        {
            public string Name { get; set; }
            public int ID{get;set;}
        }
    }
}

The improvements:

  • if we check the employee class you will find it much simpler, with no much code.The constructor is removed, as well as the private variables are missing. Still the code is running smoothly with data.When the C# “Orcas” compiler encounters an empty get/set property implementation like above, it will now automatically generate a private field for you within your class, and implement a public getter and setter property implementation to it.
  • When creating a instance of the Employee class we can see some new things. Here comes the concept of object Initializers. while creating the instance itself we can specify which public property is to be set with what value. This helps to avoid creating overloads for the constructor.

Try some more and explore the concepts.Catch you soon with some other stuffs. :-)

Posted in C# | Tagged: , | Leave a Comment »

Extension methods

Posted by codingsense on December 24, 2008

Here is a small example of Extension methods using C# 3.5

In this example we will put our own function in Frameworks string data type.

This will help user to print the the content of the stirng to console.

using System;
namespace ConsoleApplication1
{
public static class ExtendString
{
public static void PrintToConsole(this string str)
{
Console.WriteLine(str);
}
}
class Program
{
static void Main(string[] args)
{
string s = "Naveen.Prabhu";
s.PrintToConsole();
Console.Read();
}
}
}

Hope this will help you to understand the use of extension methods.

Create your own samples and take maximum use of this feature.

Happy Learning :-)

Posted in C# | Tagged: | Leave a Comment »