This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

COLLECTIONS IN C#
(1 vote, average: 5.00 out of 5)
Written by Christopher   
Sunday, 15 June 2008 18:34

INTRODUCTION

Sometimes is necessary to hold more than one object in your program as an object, a part, of a bigger collection. For example, one might want to store information about each book in his possession. So, a collection which represents these books must be defined. The C# language and the .Net environment offer you numerous techniques for implementing these kinds of collections in your code. One of those techniques is by using a collection structure.

COLLECTIONS

The main purpose of a collection is to supply a way of iterating through objects in a larger structure and perform some actions with them. This is accomplished with the foreach loop, the only advantage a collection offers. This can be accomplished only if the collection object can supply an enumerator reference in order for the iteration to take place. For example suppose the variable books is a collection. We can iterate through each object in the collection by using this template:

foreach (string myString in books)

            {

                DoSomething();

            }

 

The collections books contains some string objects. We iterate through each one of those objects and do something, for example add the string to a database considering that the string variable is the title of each book.

 

USING COLLECTIONS

When iterating through collections, you cannot predict the order that the objects will appear. If you want to retrieve the objects in a determined order you can use an array by supplying an index.  Besides, you are not allowed to add or remove objects to a collection, you can only access them. The foreach command also works with arrays; arrays are considered to be collections. In this case the enumerator iterates the objects in an orderly way from 0 till end. Finally, in arrays we can add and remove objects.

To actually use a collection you must implement the IEnumerable interface in it. This interface defines only one method named GetEnumerator(). If you want your custom structs or classes to implement this interface you have to write quite some code that is beyond the scope of this tutorial. As an example, we will define a collection of strings and we will iterate through them. For this example you also need to add the Using System.Collections statement:

 

  static void Main(string[] args)

        {

            ArrayList myArray = new ArrayList();

            myArray.Add(1);

            myArray.Add(2);

            myArray.Add(3);

            myArray.Add(4);

            myArray.Add(5);

 

            foreach (int i in myArray)

            {

                Console.WriteLine(i.ToString());

            }

            Console.ReadLine();

        }

A collection is defined as an array list. We then insert some integer values in it. We could also insert any other object available such as a string, a custom-made class etc… Next, we iterate through each integer object in our collection and write its value to the screen. Since it is an array the iteration is ordered. Try your own code and see what happens.

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options