Visual Programming Notes
Visual Programming Notes
Week No. 02
1. Arrays
A console application, in the context of C#, is an application that takes input and displays
output at a command line console with access to three basic data streams: standard input,
standard output and standard error.
A console application facilitates the reading and writing of characters from a console -
either individually or as an entire line. It is the simplest form of a C# program and is
typically invoked from the Windows command prompt. A console application usually
exists in the form of a stand-alone executable file with minimal or no graphical user
interface (GUI).
Software required for completing this tutorial successfully
Types of Arrays In C#
2. Arrays in C#
Arrays are vital for most programming languages. An array is a number of object of a
specific type than can be referenced with a common name and the individual objects are
referenced by that name and an index. An array’s elements in C# are numbered with 0, 1, 2, … N-1.
Those numbers are called indices. The total number of elements in a given array we call length of an
array.
All elements of a given array are of the same type, no matter whether they are primitive or reference types. This
allows us to represent a group of similar elements as an ordered sequence and work on them as a whole.
Arrays can be in different dimensions, but the most used are the one-dimensional and the two-dimensional arrays.
One-dimensional arrays are also called vectors and two-dimensional are also known as matrices.
In C# the arrays have fixed length, which is set at the time of their instantiation and determines the total
number of elements. Once the length of an array is set we cannot change it anymore.
int[] stdMarks;
When we declare an array type variable, it is a reference, which does not have a value (it points to null). This is
In C# we create an array with the help of the keyword new, which is used to allocate memory:
In the above example, after the allocation of memory for the array the variable myArray points to an address in the
dynamic memory, where the values are. In C#, the elements of an array are always stored in the dynamic memory
(called also heap).
next figure
above. Enter the name of the Application as Week02 as shown below and press the OK
Button
Following output will be shown when you compile and run the application.
Figure 8 Output of Array Read and Output Source Code
In the next example Array elements are initialized at the time of declaration.
When you run the application, the output is given in next figure.
Figure 10 Output of Array Initialization Source Code
Next Source Code shows the use of array property “Length”. This property is used to find
the size of the array. Here is the source code
Figure 11 Use of Length Property of Array to find the capacity of the array
Multidimensional Array in C#
The one-dimensional arrays are known also as vectors in mathematics. Often we need arrays with more than one
dimension. For example we can easily represent the standard chess board as a two-dimensional array with size 8 by
8 (8 cells in a horizontal direction and 8 cells in a vertical direction).
We declare a one-dimensional array of integer numbers using int[], and we declare a two-dimensional with int[,].
This example shows that:
int[,] stdSubjectWiseMarks;
Those arrays we will call two-dimensional, because they have two dimensions. They are also known as matrices (it
is mathematical term). In general arrays with more than one dimension we will call multidimensional.
This way we can declare three-dimensional arrays as we add one more dimension:
int[,,] stdSubjectWiseMarks;
In theory there is no limit for an array dimensions, but in practice we do not use much arrays with more than two
dimensions therefore we will focus on two-dimensional arrays.
We are allocating memory for multidimensional arrays by using the keyword new and for each dimension we set
a length in the brackets as shown:
int[,] stdSubjectWiseMarks;
This will create a two dimensional array with 3 Rows and 3 Columns.
Initializing Two Dimensional Array:
int[,] stdSubjectWiseMarks = {
{65, 55, 50},
{70, 68, 45}
{50, 60, 70}
};
0 1 2
0 65 55 50
1 70 68 45
2 50 60 70