{"id":255,"date":"2006-03-24T22:12:36","date_gmt":"2006-03-24T21:12:36","guid":{"rendered":"http:\/\/www.craigmurphy.com\/blog\/?p=255"},"modified":"2006-04-23T10:59:25","modified_gmt":"2006-04-23T09:59:25","slug":"c-lists-add-some-elegance-to-your-code","status":"publish","type":"post","link":"https:\/\/www.craigmurphy.com\/blog\/?p=255","title":{"rendered":"C# lists: add some elegance to your code"},"content":{"rendered":"<p>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# (particularly 2.0) &#8211; its implementation of a list makes handling such lists remarkably easy. <\/p>\n<p>For example, given the following class Person: <\/p>\n<p>[code lang=&#8221;C#&#8221;]<br \/>\n   public class Person<br \/>\n        {<br \/>\n            public int age;<br \/>\n            public string name;<\/p>\n<p>            public Person(int age, string name)<br \/>\n            {<br \/>\n                this.age = age;<br \/>\n                this.name = name;<br \/>\n            }<br \/>\n        }<br \/>\n[\/code]<\/p>\n<p>We can create a list of Person objects and add six people like so:<\/p>\n<p>[code lang=&#8221;C#&#8221;]<br \/>\nList<Person>people = new List<Person>();<\/p>\n<p>people.Add(new Person(50, &#8220;Fred&#8221;));<br \/>\npeople.Add(new Person(30, &#8220;John&#8221;));<br \/>\npeople.Add(new Person(26, &#8220;Andrew&#8221;));<br \/>\npeople.Add(new Person(24, &#8220;Xavier&#8221;));<br \/>\npeople.Add(new Person(5, &#8220;Mark&#8221;));<br \/>\npeople.Add(new Person(6, &#8220;Cameron&#8221;));<br \/>\n[\/code]<\/p>\n<p>C#&#8217;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:<\/p>\n<p>[code lang=&#8221;C#&#8221;]<\/p>\n<p>Console.WriteLine(&#8220;Unsorted list&#8221;);<\/p>\n<p>people.ForEach(delegate(Person p)<br \/>\n  { Console.WriteLine(String.Format(&#8220;{0} {1}&#8221;, p.age, p.name)); });<\/p>\n<p>\/\/ Find the young<br \/>\nList<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });\n\nConsole.WriteLine(\"Age is less than 25\");\n\nyoung.ForEach(delegate(Person p) \n  { Console.WriteLine(String.Format(\"{0} {1}\", p.age, p.name)); });\n\n \n\/\/ Sort by name\nConsole.WriteLine(\"Sorted list, by name\");\n\npeople.Sort(delegate(Person p1, Person p2) \n  { return p1.name.CompareTo(p2.name); });\n\npeople.ForEach(delegate(Person p) \n  { Console.WriteLine(String.Format(\"{0} {1}\", p.age, p.name)); });\n\n\n\n\/\/ Sort by age\nConsole.WriteLine(\"Sorted list, by age\");\n            \npeople.Sort(delegate(Person p1, Person p2) \n  { return p1.age.CompareTo(p2.age); });\n\npeople.ForEach(delegate(Person p) \n  { Console.WriteLine(String.Format(\"{0} {1}\", p.age, p.name)); });\n[\/code]\n\nAnd here is the output that we should expect:\n\n<strong>Unsorted list<\/strong><br \/>\n50 Fred<br \/>\n30 John<br \/>\n26 Andrew<br \/>\n24 Xavier<br \/>\n5 Mark<br \/>\n6 Cameron<\/p>\n<p><strong>Age is less than 25<\/strong><br \/>\n24 Xavier<br \/>\n5 Mark<br \/>\n6 Cameron<\/p>\n<p><strong>Sorted list, by name<\/strong><br \/>\n26 Andrew<br \/>\n6 Cameron<br \/>\n50 Fred<br \/>\n30 John<br \/>\n5 Mark<br \/>\n24 Xavier<\/p>\n<p><strong>Sorted list, by age<\/strong><br \/>\n5 Mark<br \/>\n6 Cameron<br \/>\n24 Xavier<br \/>\n26 Andrew<br \/>\n30 John<br \/>\n50 Fred<\/p>\n<p>Lists are powerful and result in fewer, and more elegant, lines of code.  Hopefully this short example has demonstrated their ease and you will find yourself using them in your day-to-day development activities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#&#8217;s support for lists make sorting and searching [large] collections very easy, here&#8217;s a brief demonstration&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-255","post","type-post","status-publish","format-standard","hentry","category-net","category-development"],"_links":{"self":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=255"}],"version-history":[{"count":0,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}