Wednesday, May 7, 2008

Object and Collection Initializers

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 watch on whats new in c# 3.0. 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
1. if 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.

2. 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 property is to be set with what value. This helps to avoid
creating overloads for the constructor.

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

Friday, May 2, 2008

Extension methods with C# 3.5

Here is an 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. :-)

Whats new in C# 3.5 compared with C# 2.0

The key features that are intorduced in C# 3.5 are


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

Thursday, May 1, 2008

Basic sample of LINQ

Here is an sample which helps in understanding LINQ better..

In this sample we will list the items in the inventory and search for the items that are finished.



using System;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Products[] ProductsList = {
new Products(){ Name = "Laptop", RemainingCount = 1},
new Products(){ Name = "CD", RemainingCount = 0},
new Products{ Name = "DVD", RemainingCount = 0},
new Products{ Name = "Cabinet", RemainingCount = 2}
};

Console.WriteLine("Total Inventory Items ::");

var Inventory = from Product in ProductsList
orderby Product.Name
select new { Name = Product.Name, RemCount = Product.RemainingCount };

foreach (var Item in Inventory)
Console.WriteLine(" Inventory has {0} peice of {1}", Item.RemCount, Item.Name );

Console.WriteLine();
Console.WriteLine();

Console.WriteLine("Empty Inventory Items ::");

var EmptyInventory = from Product in ProductsList
orderby Product.Name
where Product.RemainingCount == 0
select new { Product.Name};

foreach (var Item in EmptyInventory)
Console.WriteLine(" {0}", Item.Name);

Console.Read();
}

class Products
{
public string Name{get; set;}
public int RemainingCount{get; set;}
}
}
}



Hope you got the example executed..

For More LINQ Functions click here

Keep Learning