Use of Generic Collection

Posted by Selvarajmgp under WCF on 3/14/2012 | Points: 10 | Views : 912 | Status : [Member] | Replies : 2
How to use Generic where have to use ?

Thanks & Regards,
Selvaraju.n




Responses

Posted by: Hmanjarawala on: 3/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,
here is some examples of generic List and Dictionary objects, which may helps you...


List<Employee> list = new List<Employee>(){
new Employee() { FirstName="Himanshu", LastName="Manjarawala", Age=30},
new Employee(){ FirstName="Hetal", LastName="Sangani", Age=26},
new Employee(){ FirstName="Viral", LastName="Sangani", Age=32},
new Employee(){ FirstName="Rajesh", LastName="Patel", Age=29},
new Employee(){ FirstName="Nehal", LastName="Thakkar", Age=30}
};

//Lemda notation
var n = list.Where(o => o.FirstName.StartsWith("H"));
//Notation using delegate
var d = list.Where(delegate(Employee o) { return o.FirstName.StartsWith("H"); });
//Notation using Func<> delegate
var f = list.Where(delegate(Employee o) { return MySelect(o); });

foreach (Employee o in n)
{
System.Diagnostics.Debug.Print("Lemda: [ FirstName:{0}, LastName:{1}, Age:{2} ]", o.FirstName, o.LastName, o.Age);
}

foreach (Employee o in d)
{
System.Diagnostics.Debug.Print("Delegate: [ FirstName:{0}, LastName:{1}, Age:{2} ]", o.FirstName, o.LastName, o.Age);
}

foreach (Employee o in f)
{
System.Diagnostics.Debug.Print("Func<> Delegate: [ FirstName:{0}, LastName:{1}, Age:{2} ]", o.FirstName, o.LastName, o.Age);
}

var dictObject = new Dictionary<object, object>(5);
dictObject.Add("a_1", "Himanshu");
dictObject.Add("a_2", 25);
dictObject.Add("a_3", 4.94D);
dictObject.Add("a_4", DateTime.Now);
dictObject.Add("a5", true);

var count = (from c in dictObject
where c.Key.ToString().StartsWith("a_")
select c).Count();




Mark this as answer, if it helps you................

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Selvarajmgp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

refer this link nice explanation

http://www.dotnetfunda.com/articles/article152.aspx

Thanks

R D Patel

Selvarajmgp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response