Search results for 'C# List'. 1 post(s) found.
- 2007/08/28 C Sharp Lists
It is a fairly common programming scenario to find ourselves with a List of identical objects. In the past,without adequate support from programming languages,we found ourselves writing a lot of searching and sorting code,and that may have put you off using Lists in favour of arrays. All that has changed with C# 2.0 - its implementation of a List makes handling such Lists remarkably easy
public class Person
{
public int age;
public string name;
public Person(int age,string name)
{
this.age = age;
this.name = name;
}
}
{
public int age;
public string name;
public Person(int age,string name)
{
this.age = age;
this.name = name;
}
}
We can create a List of Person objects and add six people like so:
List<Person> people = new List<Person>();
people.Add(new Person(50,"Fred"));
people.Add(new Person(30,"John"));
people.Add(new Person(26,"Andrew"));
people.Add(new Person(24,"Xavier"));
people.Add(new Person(5,"Mark"));
people.Add(new Person(6,"Cameron"));
people.Add(new Person(50,"Fred"));
people.Add(new Person(30,"John"));
people.Add(new Person(26,"Andrew"));
people.Add(new Person(24,"Xavier"));
people.Add(new Person(5,"Mark"));
people.Add(new Person(6,"Cameron"));
C# 2.0's List mechanism provides us with a number of useful methods. Personally,I find ForEach,FindAll and Sort to be very useful. ForEach allows us access to each item in the List. FindAll allows us to search for objects in the List that match a specific condition. Sort allows us to sort the objects in the List. The following code demonstrates how we might use each of these methods:
Console.WriteLine("Unsorted List");
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
List<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });
Console.WriteLine("Age is less than 25");
young.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
Console.WriteLine("Sorted List,by name");
people.Sort(delegate(Person p1,Person p2) { return p1.name.CompareTo(p2.name); });
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
people.Sort(delegate(Person p1,Person p2) { return p1.age.CompareTo(p2.age); });
Console.WriteLine("Sorted List,by age");
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
List<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });
Console.WriteLine("Age is less than 25");
young.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
Console.WriteLine("Sorted List,by name");
people.Sort(delegate(Person p1,Person p2) { return p1.name.CompareTo(p2.name); });
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
people.Sort(delegate(Person p1,Person p2) { return p1.age.CompareTo(p2.age); });
Console.WriteLine("Sorted List,by age");
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}",p.age,p.name)); });
And here is the output that we should expect:
Unsorted List
50 Fred
30 John
26 Andrew
24 Xavier
5 Mark
6 Cameron
Age is less than 25
24 Xavier
5 Mark
6 Cameron
Sorted List,by name
26 Andrew
6 Cameron
50 Fred
30 John
5 Mark
24 Xavier
Sorted List,by age
5 Mark
6 Cameron
24 Xavier
26 Andrew
30 John
50 Fred
50 Fred
30 John
26 Andrew
24 Xavier
5 Mark
6 Cameron
Age is less than 25
24 Xavier
5 Mark
6 Cameron
Sorted List,by name
26 Andrew
6 Cameron
50 Fred
30 John
5 Mark
24 Xavier
Sorted List,by age
5 Mark
6 Cameron
24 Xavier
26 Andrew
30 John
50 Fred
Another posts included in "C#"
| How To Print Text In C# (0) | 2007/08/28 |
| How To Check If Your Printer Is Connected Using C# (0) | 2007/08/28 |
| Simplified .NET Printing In C# (0) | 2007/08/28 |
| Simple Example Of IF/THEN Using C# (0) | 2007/08/28 |
| Simple C# CGI working on Apache (0) | 2007/08/27 |
| How to send binary data through C# CGI app? (0) | 2007/08/27 |
| Interacting With TinyPic From C# (0) | 2007/08/27 |
| Creating A Watched Folder With Assigned Events (0) | 2007/08/27 |
Trackback : Cannot send a trackbact to this post.
-
Subject Detox vicodin.
2009/05/04 12:25
Cheap vicodin. What is vicodin. Vicodin user message board. Vicodin. Vicodin dependence.
-
Subject different money making ideas
2010/01/29 04:10
moneyideas
-
Subject different money making ideas
2010/01/29 13:11
moneyideas
-
Subject different money making ideas
2010/01/31 16:44
moneyideas

Prev

Rss Feed