Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
C#

Flash Back
During the previous article we
started to put our attention towards array in C#.NET which is one of the most
important programming idioms available. Arrays will let you store large amount
of data (same data type) and provides functionality to use and access them
individually. Last week we an introduction was made about arrays explained the
available types of arrays. And to explain the content some sample codes were
given to explain it deeply.
In this article we would try to
learn more about the functionalities in arrays and how the ‘array’ class
available in C# API could be used in our work. Furthermore we would get to know
about some of the more complex data collectors such as vectors, iterations and
collection types as well. We are hoping to explain them through some code
examples as well. Unlike arrays which could contain only one data types, some
of these data structures can contain different data types that would be huge
asset when programming.
Array class in C#.NET
What is System.Array Class?
The type System.Array is the abstract base
type of all array types. An implicit reference conversion exists from any array
type to System.Array and
to any interface type implemented by System.Array.
An explicit reference conversion exists from System.Array
and any interface type implemented by System.Array
to any array type. System.Array
is not itself an array-type. Rather, it is a class-type from
which all array-types are derived.
The Array class, defined
in the System namespace, is the base class for arrays in C#. Array class is an
abstract base class but it provides CreateInstance method to construct an
array. The Array class provides methods for creating, manipulating, searching,
and sorting arrays.At run-time, a value of type System.Array
can be null or a
reference to an instance of any array type.
In the last article we discussed System.Array Class Properties. In the table mentioned below the methods which belongs to the class. The Array class provides methods for creating, manipulating, searching, and sorting arrays. Array class provides three boolean properties IsFixedSize, IsReadOnly, and IsSynchronized to see if an array has fixed size, read only or synchronized or not respectively. The Length property of Array class returns the number of items in an array and the Rank property returns number of dimensions in a multi-dimension array.
|
Method |
Description
|
|
BinarySearch |
This method searches a one-dimensional sorted Array for a value, using a binary search algorithm. |
|
Clear |
This method removes all items of an array and sets a range of items in the array to 0. |
|
Clone |
This method creates a shallow copy of the Array. |
|
Copy |
This method copies a section of
one Array to another Array and performs type casting and boxing
as required. |
|
CopyTo |
This method copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. |
|
CreateInstance |
This method initializes a new instance of the Array class. |
|
GetEnumerator |
This method returns an IEnumerator
for the Array. |
|
GetLength |
This method returns the number of
items in an Array. |
|
GetLowerBound |
This method returns the lower
bound of an Array. |
|
GetUpperBound |
This method returns the upper
bound of an Array. |
|
GetValue |
This method returns the value of
the specified item in an Array. |
|
IndexOf |
This method returns the index of
the first occurrence of a value in a one-dimensional Array or in a
portion of the Array. |
|
Initialize |
This
method initializes every item of the value-type Array by calling the
default constructor of the value type. |
|
LastIndexOf |
This
method returns the index of the last occurrence of a value in a
one-dimensional Array or in a portion of the Array. |
|
Reverse |
This
method reverses the order of the items in a one-dimensional Array or
in a portion of the Array. |
|
SetValue |
This
method sets the specified items in the current Array to the specified
value. |
|
Sort |
This method sorts the items in
one-dimensional Array objects. |
Table2: Methods in System.Array class
Previously we concentrated on a code sample which was based on standard data types, but now let’s put our attention on an example which uses objects to form an array. This could very important when you are applications like games where you would need to work with number of objects.
The assignment from dogs
to animals and from cats to animals is something that you definitely
can’t do in native
C/C++. Arrays are assignable as long as their rank matches and the contained
type is convertible from one to the other. Here both ‘Dogs’ and ‘Cats’ classes
are derived from ‘Animals’ class.
P.S: Since these are empty classes, the output won’t provide anything. These could be implemented and used in various application where handling of multiple of object sin necessary.

Figure 1: Array with Objects

Figure 2:
Various functionalities of System.Array
·
Here an array with a fixed number of elements is
created. Then with the help of an ‘if’ statement that condition is checked
using the ‘IsFixedSize’ method. Since
array has 2 elements which is a fixed value, the desired output will be
displayed.
·
This is a very important method to check the
stability of an array.

Figure 3:
Use of Binary Search in Array class
·
In this code Binary search is used to search for
the entered word. It would be search in the array. For that the ‘BinarySearch’ method is used.
·
The word which needs to be
searched would be entered as an object. Then it would be entered to the ‘BinarySearch’ as a parameter. If the value excits in
the array, zero or a positive value would be generated. If not it would be a
negative value.
·
Like wise the methods which comes
under ‘System.Array’ can be used for various operations that needs to be done
using arrays.
In the next article we would look into more data structure like Vectors, vectors are much more flexible than arrays, we would see the reason why and how it can be used in programming. Meanwhile hope that you would do more research inthese and get to know more things about arrays.
Post new comment