Professional Documents
Culture Documents
Self Studies 10.2
Self Studies 10.2
Self-studies 10.2
E166457 DiTEC195-B
1) What is an Array? What the benefits of using them? In what type of situation would you use
them?
What is an array?
In C#, an array is a structure that represents an ordered collection of fixed-length values or objects of the
same type. Arrays make it easy to organize and manipulate large amounts of data. For example, instead
of creating 100 integer variables, you can create an array to store all those integer values.
C# array variables are declared like non-array variables, with square brackets ([]) added after the type
specifier to indicate that they are arrays.
The new keyword is required when instantiating a new array to assign to a variable, along with the
length of the array in square brackets. Arrays can also be instantiated by value using curly braces ({}). In
this case you don't need the length of the array.
In C#, you can simultaneously declare and initialize an array by assigning the newly declared array to a
comma-separated list of values enclosed in braces ({}). Note that this syntax allows you to omit the type
signature and new keyword on the right side of the assignment. This is only possible while declaring the
array.
1. It is used to store similar types of multiple data items using a single name.
2. We can use arrays to implement other data structures such as linked lists, trees, graphs, stacks,
queues, etc.
3. The two-dimensional arrays in C# are used to represent matrices.
4. The Arrays in C# are strongly typed. That means they are used to store similar types of multiple
data items using a single name.
E166457 DiTEC195-B
2) Explain the differences between a single-dimensional array and a multi-dimensional array. Write
3 code examples to illustrate the differences.
1D array or single dimensional array stores a list of variables of the same data type. It is possible to
access each variable using the index.
We can combine the above two statements together and write as follows.
numbers ={1,2,3,4,5,6,7,8,9,10};
The starting index of an array is 0. Therefore, the element in the 0th index is 1. The element in the 1st
index is 2. The element in the 2nd index is 3, etc. The index of the final element is 9.
If the programmer wants to store number 50 on the 2nd index, he can write the statement as follows.
numbers[2] = 50;
2D array or multi-dimensional array stores data in a format consisting of rows and columns.
The above statement allocates memory for a 2D array of 2 rows and 3 columns.
We can combine the above two statements together and write the statement as follows.
Similar to a 1D array, the starting index of the 2D array is also 0. This array has two rows and three
columns. The indexes of the rows are 0 and 1 while the indexes of columns are 0, 1 and 2. The element
10 is in the 0th row 0th column position. Number 20 is in the 0th row, 1st column position. Number 70 is
in 1st row, 2nd column position.
numbers[1][2] = 50;
Above statement assigns the number 50 to the 1st row, 2nd column position.
E166457 DiTEC195-B
int arr[5]; //an array with one int arr[2][5]; //an array with two rows
row and five columns will be and five columns will be created.
Example created. a b c d e
{a , b , c , d , e} f g h i j
3) Study different types of properties and methods available in C#.Net when working with arrays.
Give code samples for each one that you explain and show the output.
C# Array Properties
Property Description
IsFixedSize It is used to get a value indicating whether the Array has a fixed size or not.
Length It is used to get the total number of elements in all the dimensions of the Array.
LongLength It is used to get a 64-bit integer that represents the total number of elements in all the dimensions
of the Array.
SyncRoot It is used to get an object that can be used to synchronize access to the Array.
E166457 DiTEC195-B
C# Array Methods
Method Description
Copy(Array,Array,Int32) It is used to copy elements of an array into another array by specifying starting
index.
CopyTo(Array,Int32) It copies all the elements of the current one-dimensional array to the specified
one-dimensional array starting at the specified destination array index
CreateInstance(Type,Int32) It is used to create a one-dimensional Array of the specified Type and length.
Find<T>(T[],Predicate<T>) It is used to search for an element that matches the conditions defined by the
specified predicate.
IndexOf(Array,Object) It is used to search for the specified object and returns the index of its first
occurrence in a one-dimensional array.
Initialize() It is used to initialize every element of the value-type Array by calling the
default constructor of the value type.
Reverse(Array) It is used to reverse the sequence of the elements in the entire one-dimensional
Array.
C# Array Example
using System;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
// Creating an array
int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };
// Creating an empty array
int[] arr2 = new int[6];
// Displaying length of array
Console.WriteLine("length of first array: "+arr.Length);
// Sorting array
Array.Sort(arr);
Console.Write("First array elements: ");
// Displaying sorted array
PrintArray(arr);
// Finding index of an array element
Console.WriteLine("\nIndex position of 25 is "+Array.IndexOf(arr,25));
// Coping first array to empty array
Array.Copy(arr, arr2, arr.Length);
Console.Write("Second array elements: ");
// Displaying second array
PrintArray(arr2);
Array.Reverse(arr);
Console.Write("\nFirst Array elements in reverse order: ");
PrintArray(arr);
}
// User defined method for iterating array elements
static void PrintArray(int[] arr)
{
E166457 DiTEC195-B
Output :
E166457 DiTEC195-B
4) Develop code that outputs the following Star Patterns using C#.Net Console applications.
A)
E166457 DiTEC195-B
B)
E166457 DiTEC195-B
C)
E166457 DiTEC195-B
D)
E166457 DiTEC195-B