Enumerating Through Dream.In.Code in C#

The year is 2056 and governments have fallen. Anarchy reigns across the land and humanity has plunged into eternal darkness. People have turned on one another to survive and the streets are owned by the ruthless warlords with their armies of n00bs. No one is safe from the plaguing questions of “How do I read a textfile in VB.NET?”, “Why won’t clrscr work?”, or even the dreaded “Anyone know why this code won’t print a triangle?” A woman cries out after her son has been found dead in their burning car, apparently murdered because he said something bad about some gay guy’s picture named Sloth. “WHY!!!! WHY HAVE YOU DAMNED ME!!!” Find out the conclusion on this entry of the Programmer’s Underground!

As the woman clutches her son tightly, tears falling onto his battered and half burned face, she ponders taking her own life. What else is there to live for? More n00bs keep showing up around her asking their questions. No where is safe. “Can I have the source codez?”, “Want to build a mafia game?”, and “This is due in 10 minutes, can you email me the solution ASAP! k thnx bai”

Under the crackle of the fires in the street, a soft whisper can be heard lingering on the wind. Its words were as elegant as a binary sort on a sorted list of 100 integers. The whispers said “Call the DIC team and they will come”. The woman at first didn’t understand what it meant and thought there was someone playing a trick on her. “Hello? Is someone there?” The whisper replies “Call the DIC team and they will come”. Baffled by the voice at first she asks “How would I go about doing that?” After a brief pause the voice answers “Call them with some C# and they will come”.

The woman had no idea who the DIC team were but took the voice’s advice and broke out her copy of Visual C# 2008 and began writing some code that would assemble a team to fight the n00bs and take back the world. But how would she do it? After a few minutes she had an Eureka! moment and thought “If I create a DIC object which enumerated through a collection of Person objects, using a generic IEnumerable interface, it just might work!”

This is what she wrote….

private void button1_Click(object sender, EventArgs e)
			// Create an instance of our team
			DIC ourMembers = new DIC();

			// Lets loop through some of the members using a foreach
			foreach (Person member in ourMembers)
				// For each member, show their nickname and occupation
				// These are properties setup in our "Person" custom class.
				MessageBox.Show(member.nickname + " - " + member.occupation);
			MessageBox.Show("They are... the DIC TEAM!");

// Here is our DIC community class which implements the IEnumerable<Person> generic
// interface.
public class DIC : IEnumerable<Person>
		// Lets create an array of 5 people
		Person[] members = new Person[5];

		public DIC()
			// Fill the membership with some members
			members[0] = new Person("Skyhawk","The Web Masta");
			members[1] = new Person("PsychoCoder", "The Crazy Moderator");
			members[2] = new Person("no2pencil", "The n00b eraser");
			members[3] = new Person("Jayman9", "Da Jay Man");
			members[4] = new Person("Amadeus", "The Canadian");


		// Here is our generic GetEnumerator method which we must implement to meet
		// our interface's contract.
		public IEnumerator<Person> GetEnumerator()
			foreach (Person DICMember in members)
				// yield return is a keyword which helps maintain state from one call to the next
				// each time returning the next member in our members group.
				yield return DICMember;


		// In addition to the generic version we have to have the non-generic version. This simply
		// calls our generic version to return the appropriate name.
		IEnumerator IEnumerable.GetEnumerator()
			return GetEnumerator();

// This is a simple custom class we called "Person" which simply lists their nickname
// and occupation here on the DIC community board. It consists of two data members, two properties,
// and a constructor
public class Person
		String _nickname, _occupation;

		public Person(String nickname, String occupation)
			_nickname = nickname;
			_occupation = occupation;

		public String nickname
			get { return _nickname; }
			set { _nickname = value; }

		public String occupation
			get { return _occupation; }
			set { _occupation = value; }


She starts out by creating a Person class which would contain each team members name and occupation on the team. She figured if she could then create a DIC class which implemented an IEnumerable interface with the generic type “Person” should could then create the team and pull them into existence using a foreach loop! It was a brilliant idea!

So as part of her DIC class she sets up the interface IEnumerable, but made it of type “Person”… the class she made earlier. As part of the IEnumerable interface contract, she implements not just one, but two versions of the GetEnumerator method. One which handles basic (non-generic) enumeration and is wired to the generic version which returns Person class instances. This generic version then iterates through the internal array structure, and using the special yield keyword, returns each instance of the team.

The last bit the crying woman had left to do, besides burying her son, was to create a button click event which would setup the DIC team object and iterate through the Person instances of the team.

As soon as she put in the last lines of the code she paused and wished for this program to work. She had realized that if it didn’t work, the n00bs would take over the world and she would be destined to answer questions for the rest of her life. Or worse, end up like her son.

After looking at the compile button in the toolbar, she crosses her fingers and presses it ever so gently. The program begins to compile and then enters run mode. It was at that moment that the computer began to shake and fell out of her lap. It started to glow orange and laser beams were shooting out from the screen in rays of orange and white light. Suddenly members of the DIC team began to spring forth! First it was Skyhawk, The web masta! Ruler of the web world and leader of the DIC team. Next was his faithful team of PsychoCoder The Crazy Moderator who was itching to knock some n00bs with his crazy antics, no2pencil who was known as the n00b eraser for having killed so many and removing the remains. Jayman9 was next, simply known as “Da Jay Man” and who is said to have taken out 10 n00b questions at once. Last but not least was Amadeus “The Canadian” and being Canadian everyone knows he is rough, tough, loves hockey but also loves crushing n00bs on and off the ice!

The woman, shocked at what just had happened looked up at the team with amazement. Skyhawk leans down and takes her hand while saying “Thank you miss, it will be alright. Don’t let the orange spandex and the lettering DIC on my crotch area scare you. It represents Dream.In.Code and we are here to help!”

She smiled and looked at the team and asks “Will you avenge my son’s death and take out the n00bs?” Amadeus replies “We shall kill them for sure eh?” Psycho answered “I have the VB tear gas, C# stun gun, and the ASP NET to trap them. Its just how I roll”

Then without another word the team went into action and began to knock out the n00bs. “Can you show me how to fix my loop?” *BLAM!* that n00b was knocked out by PyschoCoder in one hit. “Does anyone know of a good C++ book?” and as the words were spoken Da Jay Man swoops in with the double fists *BOOM*. “Does anyone know of a good IDE for linux?” and just then no2pencil drop kicks the n00b through a nearby store window, sending glass everywhere. Then a big evil looking n00b comes rolling down the street in a n00b mobile and starts firing questions “What are code tags?”, “Why is the code snippets all messed up?”, “Can you help me with SEO?” and in jumped Skyhawk who throws a grenade under their vehicle. It explodes and the n00b is sent flying into a pile of VB6 trash.

Skyhawk: That should do it miss. Looks like you are going to be ok. Take care. If you ever need us again, be sure to rebuild the solution and we will be here.

Then, just as they came into the world, they jumped back into the computer in a flash of orange and white light. The woman, still in shock by the whole experience, continued to smile for that day forward all n00bs were handled promptly and never again did they bother the Internet world.

The End

Thanks for reading! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 25 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.