O'Reilly logo

C# Design Patterns: A Tutorial by James W. Cooper

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Arrays, Files, and Exceptions in C#

C# makes handling arrays and files extremely easy and introduces exceptions to simplify error handling.

Arrays

In C#, all arrays are zero based. If you declare an array as

int[] x = new int[10];

such arrays have 10 elements, numbered from 0 to 9. Thus, arrays are in line with the style used in C, C++, and Java.

const int MAX = 10; float[] xy = new float[MAX]; for (int i = 0; i < MAX; i++ ) {        xy[i] = i; }

You should get into the habit of looping through arrays from zero to the array bounds minus one, as we did in the preceding example.

All array variables have a length property so you can find out how large the array is.

float[] z = new float[20]; for (int j = 0; j< z.Length ; j++) {        z[j] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required