Material : tinyurl.com/89 sj qvn

Iterators and anonymous methods

- yield keyword - internal and external iterations - anonymous methods : for throwaway methods ...
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (int i in DivisibleBy(0, 50, 5))
            {
                Console.WriteLine(i);
            }
        }

        static IEnumerable DivisibleBy(int min, int max,int divisor) {
            for (int i = min; i < max; i++)
            {
                if (i % divisor == 0) {
                    yield return i;
                }
            }
        }

    }




    class X_Enumerable : IEnumerable { 

        private int min;
        private int max;
        private int divisor;

        public X_Enumerable(int min, int max, int divisor)
        {
            this.min = min;
            this.max = max;
            this.divisor = divisor;
        }
 
        public IEnumerator GetEnumerator() {
            return new X_Enumerator(min, max, divisor); 
        } 
    }

    class X_Enumerator : IEnumerator
    {
        int min;
        int max;
        int divisor;
        int current;

        public X_Enumerator(int min, int max,int divisor)
        {
            this.min=min;
            this.max = max;
            this.divisor = divisor;
        }

        public Object Current
        {
            get { return current; }
        }

        public bool MoveNext()
        {
            this.current++;
            while (current % divisor != 0) {
                this.current++;
                if (current == max) {
                    return false;
                }
            }
            return true;
        }

        public void Reset()
        {
            this.current=min;
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
 
}

LINQ to Objects

- anonymous types :
var p = new { Name = "Andy", Age = 21 };@@
using System;
using System.Collections.Generic;
using System.Text;

namespace LINQBase
{
    class Program
    {
        static void Main(string[] args)
        {
            var people = new List();
            people.Add(new Person { Name = "Nick", Height = 1.30m });
            people.Add(new Person { Name = "Jay", Height = 1.80m });
            people.Add(new Person { Name = "Bob", Height = 2.30m });

            foreach (var littlePerson in people.Where(p => p.Height < 2))
            {
                Console.WriteLine(littlePerson.Name);
            }
        }
    }


    public class Person
    {
        public decimal Height { get; set; }
        public string Name { get; set; }
    }

    public static class Utils {
        public static IEnumerable Where(this IEnumerable items, Predicate condition)
        {
            foreach (T item in items)
            {
                if (condition(item))
                    yield return item;
            }
        }
    }
}

LINQ to XML



  
    CLR via C#, Second Edition
    
      Jeffrey Richter
    
    .Net 2.0
    .Net General
    http://www.amazon.com/gp/product/0735621632/sr=8-1/qid=1141842947/ref=pd_bbs_1/103-3403410-7338268?_encoding=UTF8
    35.99
  
  
    Creating Workflow Services and Durable Services (MSDN)
    
      Anonymous
    
    .Net 3.5
    Service Composition
    http://msdn2.microsoft.com/en-us/library/bb412181.aspx
    0.0
  
  
    Data Binding with Windows Forms 2.0
    
      Brian Noyes
    
    .Net 2.0
    Windows Forms
    http://www.amazon.com/Data-Binding-Windows-Forms-2-0/dp/032126892X/ref=pd_bxgy_b_img_b
    32.99
  
  
    Essential ASP.NET 2.0
    
      Fritz Onion
      Keith Brown
    
    .Net 2.0
    Web Apps
    http://www.amazon.com/Essential-ASP-NET-2-0-Fritz-Onion/dp/0321237706/ref=pd_bbs_sr_1/103-2749730-3599818?ie=UTF8&s=books&qid=1176902837&sr=8-1
    36.99
  
  
    Essential ASP.NET With Examples in C#
    
      Fritz Onion
    
    .Net 2.0
    Web Apps
    http://www.amazon.com/Essential-ASP-NET-Examples-Fritz-Onion/dp/0201760401/ref=pd_bbs_sr_2/103-2749730-3599818?ie=UTF8&s=books&qid=1176902837&sr=8-2
    31.49
  
  
    Essential C# 2.0
    
      Mark Michaelis
    
    .Net 2.0
    C# Language
    http://www.amazon.com/Essential-2-0-Microsoft-NET-Development/dp/0321150775/ref=pd_bxgy_b_text_b/102-4792916-8428135?ie=UTF8&qid=1174210886&sr=1-2
    37.79
  
  
    Essential Windows Presentation Foundation
    
      Chris Anderson
    
    .Net 3.0
    Presentation Foundation
    http://www.amazon.com/Essential-Presentation-Foundation-Microsoft-Development/dp/0321374479/ref=sr_1_1/002-9650384-5136050?ie=UTF8&s=books&qid=1188296520&sr=8-1
    31.49
  
  
    Essential Windows Workflow Foundation
    
      Dharma Shukla
      Bob Schmidt
    
    .Net 3.0
    Workflow Foundation
    http://www.amazon.com/Essential-Workflow-Foundation-Microsoft-Development/dp/0321399838/ref=pd_bbs_sr_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436658&sr=8-1
    31.49
  
  
    Learning WCF
    
      Michele Bustamante
    
    .Net 3.0
    Communication Foundation
    http://www.amazon.com/Learning-WCF-Hands-Michele-Bustamante/dp/0596101627/ref=pd_bbs_sr_1/103-2749730-3599818?ie=UTF8&s=books&qid=1176904112&sr=8-1
    29.69
  
  
    LINQ in Action
    
      Fabrice Marguerie
      Steve Eichert
      Jim Wooley
    
    .Net 3.5
    LINQ
    http://www.amazon.com/LINQ-Action-Fabrice-Marguerie/dp/1933988169/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1202326210&sr=1-1
    29.69
  
  
    Programming .NET Components, 2nd Edition
    
      Juval Lowy
    
    .Net 2.0
    .Net General
    http://www.amazon.com/gp/product/0596102070/ref=pd_kar_gw_1/103-3403410-7338268?_encoding=UTF8&v=glance&n=283155
    29.67
  
  
    Programming WCF Services
    
      Juval Lowy
    
    .Net 3.0
    Communication Foundation
    http://www.amazon.com/Programming-WCF-Services-Juval-Lowy/dp/0596526997/ref=pd_bbs_sr_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436749&sr=8-1
    29.69
  
  
    Programming Windows Workflow Foundation
    
      K. Scott Allen
    
    .Net 3.0
    Workflow Foundation
    http://www.amazon.com/Programming-Windows-Workflow-Foundation-Techniques/dp/1904811213/ref=sr_1_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436585&sr=8-1
    44.99
  
  
    Smart Client Deployment with ClickOnce
    
      Brian Noyes
    
    .Net 2.0
    Windows Forms
    http://www.amazon.com/Smart-Client-Deployment-ClickOnce-Applications/dp/0321197690/ref=pd_bbs_sr_1/102-5394843-6848112?ie=UTF8&s=books&qid=1175345601&sr=8-1
    29.69
  
  
    Understanding Windows CardSpace
    
      Vittorio Bertocci
      Garrett Serack
      Caleb Baker
    
    .Net 3.0
    CardSpace
    http://www.amazon.com/Understanding-Windows-CardSpace-Introduction-Independent/dp/0321496841/ref=sr_1_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436821&sr=8-1
    32.84
  
  
    Windows Forms 2.0 Programming (2nd Edition)
    
      Chris Sells
      Michael Weinhardt
    
    .Net 2.0
    Windows Forms
    http://www.amazon.com/gp/product/0321267966/qid=1141845579/sr=1-1/ref=sr_1_1/103-3403410-7338268?s=books&v=glance&n=283155
    32.39
  
  
    Windows Presentation Foundation Unleashed
    
      Adam Nathan
    
    .Net 3.0
    Presentation Foundation
    http://www.amazon.com/Windows-Presentation-Foundation-Unleashed-WPF/dp/0672328917/ref=pd_bbs_sr_1/103-2749730-3599818?ie=UTF8&s=books&qid=1176903814&sr=8-1
    34.49
  

public class Book
    {
        // Book properties
        public string Category { get; set; }
        public string Topic { get; set; }
        public string Title { get; set; }
        public decimal Price { get; set; }
        public string[] Authors { get; set; }
        public string WebSite { get; set; }

        // Book description
        public override string ToString()
        {
            string output = string.Format("{0} by {1}",
                Title, string.Join(", ", this.Authors));
            output += string.Format("
{0}
{1}
{2}", Category, Topic, Price.ToString("c")); return output; } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LinqToXmlLab
{
    static class RssFeed
    {
        public static XElement GetBooksXml(string url)
        {
            XElement xml = XElement.Load(url); 
            return xml;
        }

        public static List GetBooks(XElement xml)
        {
            XNamespace ns = "http://develop.com";

            IEnumerable booksQuery = from b in xml.Elements(ns + "book") select
                new Book { Title = (string)b.Element(ns + "title"),
                           Category = (string)b.Element(ns + "category"),
                           Topic = (string)b.Element(ns+"topic"),
                           Price = (decimal)b.Element(ns + "price"),
                           WebSite = (string)b.Element(ns + "link"),
                           Authors = (from a in b.Element(ns + "authors").Elements(ns + "author") select (string)a).ToArray()
                };

            return booksQuery.ToList();
        }

        public static XElement GetFeedXml(List books) { 
            XElement xml = new XElement("rss", 
                new XAttribute("version", "2.0"), 
                new XElement("channel", 
                    new XElement("title", "Essential .Net Reading"), 
                    new XElement("link", "http://localhost:12345/books/"), 
                    new XElement("description", ".Net developer reading"), 
                    from b in books orderby b.Category, b.Topic, b.Title select new XElement(
                        "item", 
                        new XElement("title", b.Title), 
                        new XElement("link", b.WebSite), 
                        new XElement("description", b)))); 
            return xml; 
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;

namespace LinqToXmlLab
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:12345/books/");
            listener.Start();
            Console.WriteLine("Rss Feed Service is running");
            Console.WriteLine("Press Ctrl+C to terminate ...");

            while (true)
            {
                HttpListenerContext ctx = listener.GetContext();
                using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
                {
                    // Load xml from file
                    string url = @"..\..\books.xml";
                    XElement booksXml = RssFeed.GetBooksXml(url);

                    // Get books list
                    List books = RssFeed.GetBooks(booksXml);

                    // Convert books list to xml
                    XElement feedXml = RssFeed.GetFeedXml(books);

                    // Send xml to the reponse stream
                    writer.Write(feedXml);
                }
            }
        }
    }
}