C# for Java Programmers

Arrays

As was the case with strings, C# s Array class shares many similarities with its Java counterpart. You are most likely familiar with the concept of an array; an array is a series of objects consisting of the same data types. Individual objects in the series, called elements, are referenced by their index into the array.

This concept of an array holds true for most programming languages. The similarities specifically between Java and C# arrays deal with the methods and properties of their classes. Both C# and Java provide a length property, which provides the number of elements in an array. They also provide similar methods for sorting or searching arrays. We will discuss these similarities in more detail after we go over how to declare and initialize arrays in the next section.

Declaring and Initializing Arrays

Arrays are declared using the same syntax as in Java:

// data_type[] arrayname;int[] IntegerArray;

Arrays can be initialized in several different ways. The most common way is by using the new operator, which will initialize an array, but not assign any values to it. The following code will create an integer array with four elements:

int[] integerArray = new int[4];

If you knew the values you wished to assign the array elements at the time you created the array, you can also use the following syntax. This method will create an integer array with four elements, and assign the values 1, 2, 3, and 4 to the elements, respectively.

int[]...

UNLIMITED FREE ACCESS TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Diode Arrays
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.