Thursday, 18 October 2018

C# - IEnumerable++ Introduction

I am very curious about C# and its LINQ technology. My first guesses are that LINQ Is based on IEnumerable. I'm starting a series of posts based on IEnumerable and technologies based thereupon, hence IEnumerable++.

I'm placing some beginner code here because I want to revisit IEnumerable from the ground up. This is because it is (I believe) a key interface in the advanced topic of Linq. Here is a screenshot of the System.Linq.Enumerable metadata showing the prevalence of IEnumerable.

So in the screenshot one can see plenty of SQL keywords such as Select and OrderBy, off-screen are other keywords such as Join, GroupBy, Count, Distinct, Union, Where. It is clear to me that the beginner topic IEnumerable is a key interface worth knowing inside out because it will assist in understanding the advanced topic of System.Linq.Enumerable. I will work from beginner topics through to advanced in a series of posts.

So some of these posts may look too elementary but it is a question of dotting i's and crossing t's for a fuller understanding.

IEnumerable drives ForEach

So most people know IEnumerable as being that which drives foreach statement. Here is some code which shows an automatic implementation by virtue of the List<> class as well as a manual implementation explicitly implementing IEnumerable and IEnumerator. The code that follows is intended for a console .NET Framework project.

using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleEnumTut01
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = ListFactory.CreateCustomers();

            foreach (var customer in customers)
            {
                Console.WriteLine(customer);
            }

            Customers customers2 = new Customers();
            foreach (var customer2 in customers2)
            {
                Console.WriteLine(customer2);
            }
        }
    }

    class Customer
    {
        public int CustomerId { get; set; } // key field, unique identifier
        public string CustomerName { get; set; }
        public string ContactName { get; set; }
        public string Country { get; set; }
        public override string ToString() { return String.Format("{0} {1}", CustomerId, CustomerName); /* simple for now */ }
    }

    class Customers : IEnumerable<Customer>, IEnumerator<Customer>
    {
        // private members
        List<Customer> _customers;
        int _cursor;

        // constructors
        public Customers()
        {
            // delegate to Factory
            _customers = ListFactory.CreateCustomers();
            _cursor = -1;
        }

        //
        // Interfaces explicitly implemented
        //

        // IEnumerable<>
        IEnumerator<Customer> IEnumerable<Customer>.GetEnumerator()
        {
            return this;
        }

        // IEnumerable
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }


        // IEnumerator, IEnumerator<>
        Customer IEnumerator<Customer>.Current => _customers[_cursor];

        object IEnumerator.Current => _customers[_cursor];

        bool IEnumerator.MoveNext()
        {
            _cursor++;
            return (_cursor < _customers.Count);
        }

        void IEnumerator.Reset()
        {
            _cursor = -1;
        }

        // IDisposable
        void IDisposable.Dispose()
        {

        }
    }

    class Order
    {
        public int OrderId { get; set; }
        public int CustomerId { get; set; }
        public DateTime OrderDate { get; set; }
    }

    static class ListFactory
    {
        public static List<Customer> CreateCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { CustomerId = 1, CustomerName = "Big Corp", ContactName = "Mandy", Country = "USA" });
            customers.Add(new Customer { CustomerId = 2, CustomerName = "Medium Corp", ContactName = "Bob", Country = "Canada" });
            customers.Add(new Customer { CustomerId = 3, CustomerName = "Small Corp", ContactName = "Jose", Country = "Mexico" });
            return customers;
        }

        public static List<Order> CreateOrders()
        {
            List<Order> orders = new List<Order>();
            orders.Add(new Order { OrderId = 420, CustomerId = 2, OrderDate = new DateTime(2018, 10, 10) });
            orders.Add(new Order { OrderId = 421, CustomerId = 3, OrderDate = new DateTime(2018, 10, 11) });
            orders.Add(new Order { OrderId = 422, CustomerId = 1, OrderDate = new DateTime(2018, 10, 12) });
            orders.Add(new Order { OrderId = 423, CustomerId = 2, OrderDate = new DateTime(2018, 10, 13) });
            return orders;
        }
    }
}

I want to start running some LINQ queries, see next post.

Links

No comments:

Post a Comment