Hiya. It's good to hear that you're interested in programming. It's loads of
fun, even when you have no idea what you're doing. (You usually get better
results if you know what's going on, though. ;-) )
Arrays are the most basic type of collection that exists. Arrays hold
multiple values, all of which are a single type. They're also the fastest
type of collection, so if speed is important, an array is usually a good
choice. The big disadvantage of arrays is that they can't be resized. Once
you pick the size of an array, you can't change it. So when would you use an
array?
(1) The most common situation for using an array arises when you need to
perform the same operation on a large amount of data, and you know in
advance how large the size of the data will be. For instance, let's suppose
you wanted to triple the value of every integer of a 5-number sequence. If
you'd never heard of arrays, you might decide to do it this way:
// Begin code.
int i1 = 45;
int i2 = 81;
int i3 = 18;
int i4 = 29;
int i5 = 10;
i1 *= 3;
i2 *= 3;
// etc.
// End code.
This is painful and tedious. Programmers hate pain and tedium. Hence,
arrays!
// Begin code.
int[] values = new int[] {45, 81, 18, 29, 10};
for (int i = 0; i < values.Length; i++)
{
values[i] *= 3;
}
// End code.
This is half as much code and much easier to change. For instance, if you
made a mistake in reading your project specifications, and you were really
supposed to multiply by 4, then you only need to change one line. In the
more complicated code, you'd need to change 5 lines.
When would be a bad idea to use an array? In general, any time you don't
know the size of the data is a good choice not to use arrays, or when you'll
need to change the size and add and remove a lot of stuff. Arrays are not
very dynamic.
As for indexers, that's a slightly more advanced topic. Basically, an
indexer is a special way to look up things for a collection. An array has an
integer indexer. Other collections have integer indexers too, and many have
special string indexers and things like that. Here's an example. Let's say
you wanted to make a list of some of the zip codes of the United States. You
want to store these zip codes:
45678 - Splitsville, IN
12345 - New York, NY
22936 - Charlottesville, VA
29130 - Foobar, FL
19381 - Sacramento, CA
If you'd only heard of arrays, you might try something like this:
// Begin code.
// Make space for every possible zip code.
string[] zipCodes = string[100000];
zipCodes[45678] = "Splitsville, IN";
zipCodes[12345] = "New York, NY";
// etc.
Console.WriteLine(zipCodes[45678]); // prints "Splitsville, IN"
// End code.
This works just fine, but notice that you've only used 5 out of the 100,000
elements in the array. The other 99,995 are totally wasted and taking up
memory for no good reason. Clearly an array isn't a great choice here.
A collection that could help you out here is the Hashtable; it has a string
indexer. When you put things in a Hashtable, you only take up as much space
as is needed for one of the items, so you don't have those huge gaps of
space like in the earlier array example. (Actually, that's not quite true,
but this is a simplistic explanation. =) ). Here's what a Hashtable in
action looks like:
Hashtable myHT = new Hashtable();
myHT["45678"] = "Splitsville, IN";
// etc.
Console.WriteLine((string)myHT["45678"]); // prints "Splitsville, IN"
You'll have to experiment to get a feel for the true power of C#. The best
way to learn is through experimentation yourself. Good luck!
- k^2
"mdub317" <md*****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
I'm totally new to programming and I am wondering; when would be a good
time to use an array or an indexer? I want to know what types of applications
would make good use of arrays or indexers. There seems to be other ways
of doing the jobs of the two and less confusing.
The books I read don't provide good examples of situations when I would
need an array or indexer. I don't really need a definition of them as I
already have that. I just need to know what which well known apps have uses for
arrays and indexer?