The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Generics in C#  
 
Generics
 
When we think about the word Generic outside of the programming world it simply means that the object is not tied to any specific kind. An example is a bank account. We may not know which bank it is associated too, if it is a checking or savings account, however we do know that we can generally deposit or withdrawl from one. Therefore we agree on the standard function and properties of a bank account, but perhaps don't know all the specifics.
 
Generics has the similiar context. Meaning that a class is not related to any specific type (bank, savings, checking, investment), but we still want it to do what the class should do (give us money).
 
Generics is an older term (in internet years anyway) and in my opinion, you probably wouldn't want to imnplement something like this becuase in the newer versions of .Net you are provided with List, SortedList, Dictionary, ArrayList, etc. Most are found within the System.Collections .Net namespace. However, in your quest for a total understanding of C# and/or programming concepts, I can recommend at least understanding what is happening 'behind the scenes'. Becuase List, SortedList, Dictionary, etc. implement the IEnumerable interface and are therefore using what is described here. It has just been abstracted away for simplicity reasons.
 
In this example we will create a GenericList class:
 
                            public class GenericsList : IEnumerable
                            { 
                                protected Element head;
                                protected Element current = null;

                                protected class Element
                                {
                                    public Element nextElement {get; set;}
                                    public T elementData { get; set; }

                                    public Element(T t)
                                    {
                                        nextElement = null;
                                        elementData = t;
                                    }
                                }

                                public GenericsList() { head = null; }

                                public void AddElementToList(T t)
                                {
                                    Element element = new Element(t);
                                    element.nextElement = head;
                                    head = element;
                                }

                                public IEnumerator GetEnumerator()
                                {
                                    Element current = head;
                                    while (current != null)
                                    {
                                        yield return current.elementData;
                                        current = current.nextElement;
                                    }
                                }

                                IEnumerator IEnumerable.GetEnumerator()
                                {
                                    return GetEnumerator();
                                }
                            }
 
The above class provides the framework for for adding and reading the data elements within the list. For example, the AddElementToList(T) adds the element to a list. If it is the first element then the Element.nextElement is equal to NULL. This will tell us when we have have reached the last record in the list. We will generally read the elements from within a foreach statement. The IEnumerator GetEnumerator() method provides this capability for us. Since the release of .Net 2.0, we are no longer required to implement a Current or MoveNext method.
 
You can think of a GenericList as a Russian Matryoshka doll. I.e. a set of dolls of graduated size, each one fitting inside the next larger doll. As this picture illistrates. Element data is stored, then an instance of the next element is stored within itself, so on and so on and so on. Approach it from starting with the smallest doll being put into the next larger doll, so on and so on and so on.
 
 
We will next create a simple BankBalance class. This class provides the structure of the elements we store in the list.
 
                            public class BankBalance
                            {
                                string accountNumber;
                                double accountBalance;

                                public BankBalance(string acctNum, double acctBal)
                                {
                                    accountNumber = acctNum;
                                    accountBalance = acctBal;
                                }

                                public override string ToString()
                                {
                                    return accountNumber + "\t:\t" + accountBalance;
                                }
                            }
 
Lastly, we will create the Main program that will:
1. Provide the data for our BankBalance class
2. Load the data into a GenericList of type BankBalance (ADD)
3. Write the data to the console via a foreach statement (READ)
 
                            class GenericsProgram
                            {
                                static void Main(string[] args)
                                {
                                    GenericsList list = 
                                        new GenericsList();

                                    //Create accountNumber and accountBalance 
                                    //values to initialize BankBalance objects.
                                    string[] accountNumber = 
                                        new string[] 
                                            { "A12345-98", "B34565-64", "L98756-32" };
                                    double[] accountBalance = 
                                        new double[] { 125.12, 748.54, 42343.56 };

                                    //Populate the list.
                                    for (int row = 0; row < accountNumber.Length; row++)
                                    {
                                        list.AddElementToList(new 
                                            BankBalance(accountNumber[row], 
                                                accountBalance[row]));
                                    }

                                    Console.WriteLine("Bank Account Balance List:");
                                    foreach (BankBalance b in list)
                                    {
                                        Console.WriteLine(b.ToString());
                                    }

                                    Console.ReadLine();
                                }
                            }
 
The Generics concepts is one of the more powerful tools you can use when managing data or in memory states of objects. They allow the programmer to store groups of similar data elements together in a type safe and very effecient (performance aware) format.
 
 
Download the source
 
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben