Vous êtes sur la page 1sur 30

Array

• An array is a collection of the variable of same data type.


• If you have to declare 1000 integer variables, then you can
declare an integer type array with 1000 elements
• The value of array can be accessed using index position of
array
• The first index position of array is zero
• In C#, there two types of array: Single Dimensional Array
and Multi-Dimensional Array.
Initialization And Declaration Of Array
• Sometimes, you need to declare multiple variable of same data type. It is complex
and time consuming process. Just for an example, you have to declare 100 integer
type of variable then what would you do? Will you declare variable as follow:
int num1,num2,num3,num4,num5,num6,num7,num8.... num100;
It is not a suitable way to declare multiple variable of same data type. To overcome
this situation, array came alive. An array is a collection of same data type. If you
have to declare 100 variable of same data type in C#, then you can declare and
initialize an array as follow.
• int[] num = new int[100];
STRUCTURE OF C# ARRAY
To understand concept of array, consider the following example. What happens in
memory when we declare an integer type array ?
int[] num = new int[6];
DESCRIPTION:
It stores the values in the index position of the array starting
with 0.
• num[0] = 6;
num[1] = 23;
num[2] = 12;
num[3] = 9;
num[4] = 14;
num[5] = 52;
• Now, you can access the value of array by index position. Such as if
you have to access 3rd index of value, then you can write as follow:
int i = num[2];
Output
Storing Value In Array

• After declaration of the array, you need to store values in an array.


• Either you can store values at compile time by writing in the program
or runtime by taking input from the user
Storing value directly in your C# program:
• In your program, you can directly store value in array. You can store value in array by two well known ways
1. INLINE:
int[] arr =new int[5] { 1, 2, 3, 4, 5 };
2. INDEX
int[] arr = new int[5]
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
• If you are storing value in array by taking users input, then you can use loop to accept value in array.
• The following C# example will focus how to get input from users at runtime and store in an array
Output
Accessing Arrays Value

• After storing values into the array, you need to access the values from
array and require some operation on value.
• As storing value in array is very simple, the accessing value from array
is also so simple.
• You can access array’s value by its index position.
• Each index position in array refers to a memory address in which your
values are saved.
OUTPUT
One Dimensional Array

• The one dimensional array or single dimensional array in C# is the


simplest type of array that contains only one row for storing data.
• It has single set of square bracket (“[]”).
• To declare single dimensional array in C#, you can write the following
code.
string[] Books = new string[5];
PROGRAMMING EXAMPLE OF ONE DIMENSIONAL
ARRAY:
OUTPUT
Multi Dimensional Array

• The multi-dimensional array in C# is such type of array that contains more


than one row to store data on it.
• The multi-dimensional array is also known as a rectangular array in c sharp
because it has the same length of each row.
• It can be a two-dimensional array or three-dimensional array or more. It
contains more than one comma (,) within single rectangular brackets (“[ , ,
,]”).
• To storing and accessing the elements from a multidimensional array, you
need to use a nested loop in the program.
• The following example will help you to figure out the concept of a
multidimensional array.
STRUCTURE:
Passing Array As Parameter

• An array can also be passed to a method as argument or parameter.


• A method process the array and returns output.
• Passing array as parameter in C# is pretty easy as passing other value
as a parameter.
• Just create a function that accepts an array as argument and then
processes them.
• The following demonstration will help you to understand how to pass
an array as an argument in C# programming
OUTPUT
Array Class And Function

• The Array class is the base class of all the arrays in C# programming.
• Array class is defined within the system namespace.
• Array class provides number of predefined functions and properties
to work with.
• Its function makes array much easier and simpler to work.
int[] arr = new int[4];
MOST COMMON PROPERTIES OF ARRAY CLASS
MOST COMMON FUNCTIONS OF ARRAY CLASS
Practice questions
• Qu 1 :
Write a program of sorting an array. Declare single dimensional array
and accept 5 integer values from the user. Then sort the input in
ascending order and display output

Vous aimerez peut-être aussi