Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать
Write-

MORE ABOUT COMPONENTS

Chapter 4

 

71

 

 

 

 

 

Array.Sort (Integer);

Consider an example of an array that stores the marks of students. This data is sorted to know the maximum and minimum marks obtained by the students.

int [] Marks = {70,62,53,44,75,68}; int I = Marks.Length;

Array.Sort (Marks);

for (int x = 0; x < I; x++)

{

Console.WriteLine (x);

}

This code initializes an integer array with the values as specified in the program code.The code then calculates the size of the one-dimensional array Integer and stores its value in the variable I. The size of the array is determined using the Length property. The Sort() method is then used to sort the elements of Integer. The sorted elements are displayed in the Console window by using the Line() method of the Console class.

The output of the previous code is:

44,53,62,68,70,75

In this section, you learned about arrays. An array is a special type of collection in C#. The next section will look at collections in C#.

Collections

A collection is defined as a group of objects that you can access using the foreach loop. For example, look at the following code:

foreach (string str1 in collection1)

{

Console.WriteLine (str1):

}

In this code, collection1 is a collection, and the foreach loop is used to access

objects of collection1.

72

Part II

HANDLING DATA

 

 

 

Creating Collections

All collections in C# are implemented by the System.Collections.IEnumerable interface. You have learned about interfaces in Chapter 3 in the section “Interfaces.” Interfaces are components used to declare a set of methods that are never implemented. There are several predefined interfaces provided by C#. One of these predefined interfaces is the IEnumerable interface that has a GetEnumerator() method. This method returns an object of the type enumerator. Therefore, every collection has one or more enumerator objects associated with it. These objects are used to access data from the associated collection. You can use the enumerator object only to read data from a collection, not to modify the collection.

To access the elements of a collection, you create an object that implements the IEnumerable interface. To initialize this object, the MoveNext() method is called. This method is used to move across the elements of the collection. When the MoveNext() method is called for the first time, it moves the enumerator object to the first element of the collection.

Once the enumerator object is initialized with the first element of the collection, you can then move across the elements of the collection by calling the MoveNext() method. The value referred by the enumerator object can be read by the Current property. This property returns only a reference to the elements of the collection. Therefore, to get the actual value of the element, you can type cast the reference to the type of the element. To find out more about collections, consider the following code sample.

public interface IEnumerable

{

IEnumerator GetEnumerator ();

}

public interface IEnumerator

{

bool MoveNext(); object Current

{

get;

}

void Reset();

}

MORE ABOUT COMPONENTS

Chapter 4

73

 

 

 

This code declares the GetEnumerator() method of the IEnumerable interface. Next, the MoveNext() method of the IEnumerator interface, which returns a Boolean type variable, is called.The Current property of the type object is used to read the current element of the collection. You use the get property to read the elements of a collection. You can use the Reset() method to reset the value of the

enumerator object.

Working with Collections

After creating a collection, you can work with it. To do this, you can use the interfaces provided by C#. Figure 4-1 lists some of the interfaces that you can use to work with collections.

FIGURE 4-1 Interfaces used with collections

Each of these interfaces is present in the System.Collections namespace. These interfaces have several classes and methods associated with them. The ArrayList class will be discussed in detail.

ArrayList is an important class present in the System.Collections namespace that you can use to create a dynamically increasing array. The ArrayList class implements the IList interface. When you create an object of the ArrayList class, C#

74

Part II

HANDLING DATA

 

 

 

allocates memory to this object. You can specify the initial size of the ArrayList object while creating the instance of the ArrayList class by using the new keyword. You can then add elements to this object. However, if you add more elements to the ArrayList than its capacity (the number of elements that an object of ArrayList can hold), C# automatically allocates more memory to the ArrayList object. Consider the following example to learn about the ArrayList class.

using System;

using System.Collections; public class ArrayList1

{

public static void Main()

{

ArrayList list1 = new ArrayList(); list1.Add(“This”); list1.Add(“is”);

list1.Add(“a”); list1.Add(“sample”); list1.Add(“ArrayList.”);

}

}

This code creates an object of the ArrayList class with the name list1 and then adds elements to this object by using the Add() method.

Some of the methods present in the interfaces used with collections are discussed in the following list.

ICollection Interface:

CopyTo(). The CopyTo() method is used to copy the elements of the ICollection interface to a specified array. You can also specify the starting index from which you want to copy the elements.

IDictionary Interface:

Add(). The Add() method is used to add an element to the IDictionary interface. You can specify the key and value of the element that is added.

MORE ABOUT COMPONENTS

Chapter 4

75

 

 

 

Remove(). The Remove() method is used to delete an element from the IDictionary interface. You need to specify the key of the element to be deleted.

Clear(). The Clear() method is used to delete all the elements from the IDictionary interface.

GetEnumerator(). The GetEnumerator() method is used to return an IDictionaryEnumerator object for the IDictionary interface.

Contains(). The Contains() method is used to locate a particular element in the IDictionary interface. You need to specify the key of the element to be located.

IList Interface:

Add(). The Add() method of the IList interface is used to add elements to the IList interface.

Remove(). The Remove() method is used to delete the first occurrence of the object from the IList interface.

RemoveAt(). The RemoveAt() method is used to delete the element present at the index value that you specify.

Clear(). The Clear() method is used to delete all the elements from the IList interface.

Insert(). The Insert() method is used to insert an element at the specified index in the IList interface.

IndexOf(). The IndexOf() method is used to find the index value of the specified element.

ICloneable Interface:

Clone(). The Clone() method is used to create clones of an existing instance of a class.

Having learned about arrays and collections, you need to learn about indexers. Indexers are members that allow you to access objects as if they were the elements of an array.