1. Renas R. Rekany Object-Oriented-Programming
1
Arrays (declaration 1_D, 2_D, initialization)
1- 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];
2- 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];
Num Array:
2. Renas R. Rekany Object-Oriented-Programming
2
3- Declaration and Initialization of Array:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] array=new int [5];
array[0] = 3;
array[1] = 6;
array[2] = 2;
array[3] = 7;
array[4] = 9;
for (int i = 0; i < 5; i++)
{
textBox1.Text = textBox1.Text + array[i];
textBox1.AppendText(Environment.NewLine);
}
}
}
}
4- Storing value directly in C# program:
In your program, you can directly store value in array. You can store value in array
by two ways.
i. Inline:
int[] arr =new int[5] { 3, 6, 2, 7, 9 };
ii. Index:
int[] arr = new int[5]
arr[0] = 3;
arr[1] = 6;
arr[2] = 2;
arr[3] = 7;
arr[4] = 9;
3. Renas R. Rekany Object-Oriented-Programming
3
iii. Users input:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] array2 = new int[10];
for (int i = 0; i < 10; i++)
{
array2[i] = Convert.ToInt32(Interaction.InputBox("Enter The
array elements", "Input Box", "Write Here", 75, 75));
textBox1.Text = textBox1.Text + array2[i];
textBox1.AppendText(Environment.NewLine);
}
}
}
}
Hint: To define InputBox, you have to Add reference from .Net-> Microsoft.Visual Basic,
then write it's library.
4. Renas R. Rekany Object-Oriented-Programming
4
5- Accessing value from array:
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.
int[] arr=new int[6];
int num1, num2;
num1 = arr[1] * arr[3];
It is same as:
num1 = 23 * 9; Because, arr[1] refers to the value 23 and arr[3] refers to the value 9.
If you are required to access entire value of an array one after another, then you can
use loop constructs to iterate through array.
5. Renas R. Rekany Object-Oriented-Programming
5
Example3:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] id = new int[3];
string[] name = new string[3];
int i, j = 0;
string f;
for (i = 0; i < 3; i++)
{
id[i] = Convert.ToInt32(Interaction.InputBox("Enter the id",
"InputBox", "Enter Here", 75, 75));
name[i] = Interaction.InputBox("Enter the name", "InputBox",
"Enter Here", 75, 75);
}
f = Interaction.InputBox("Enter the name to find", "InputBox",
"Enter Here", 75, 75);
for (i = 0; i < 3; i++)
{
if (name[i] == f)
{
MessageBox.Show(Convert.ToString(id[i]), "The Id is:");
j++;
}
}
if (j == 0)
MessageBox.Show("Not Found");
}
}
}
Hint: To define InputBox, you have to Add reference from .Net-> Microsoft. Visual Basic,
then write it's library.
6. Renas R. Rekany Object-Oriented-Programming
6
6- Array dimensions:
A. One dimensional:
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.
Example4:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] books = new string[5] { "C#", "VB", "JAVA", "C++", "MATLAB"
};
textBox1.Text = "First Second Third Four Five";
textBox1.AppendText(Environment.NewLine);
textBox1.Text = textBox1.Text + "_______________________________";
textBox1.AppendText(Environment.NewLine);
for (int i = 0; i < 5; i++)
{
textBox1.Text = textBox1.Text + books[i] + " ";
}
}
}
}
7. Renas R. Rekany Object-Oriented-Programming
7
OUTPUT:
B. 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 rectangular array
in c sharp because it has same length of each row. It can be two dimensional array or
three dimensional array or more. It contains more than one coma (,) within single
rectangular brackets (“[ , , ,]”). To storing and accessing the elements from
multidimensional array, you need to use nested loop in program. The following
example will help you to figure out the concept of multidimensional array.
Example5:
8. Renas R. Rekany Object-Oriented-Programming
8
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[,] books = new string[3,3]{ { "C#", "VB", "JAVA"},{".NET",
"C++", "DB" },{"SQL","HTML","XML"}};
textBox1.Text = "First Second Third ";
textBox1.AppendText(Environment.NewLine);
textBox1.Text = textBox1.Text + "___________________________";
textBox1.AppendText(Environment.NewLine);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
textBox1.Text = textBox1.Text + books[i, j] + " ";
}
textBox1.AppendText(Environment.NewLine);
}
}
}
}
OUTPUT:
9. Renas R. Rekany Object-Oriented-Programming
9
7- Passing array:
An array can also be passed to 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 parameter. Just create a function that accepts array as argument and then
process them. The following demonstration will help you to understand how to pass
array as argument in C# programming.
Example4:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void printarray(int[] print)
{
int i;
for (i = 0; i < 5; i++)
{
textBox1.Text = textBox1.Text + print[i];
textBox1.AppendText(Environment.NewLine);
}
}
private void button1_Click(object sender, EventArgs e)
{
int[] num = new int[5] { 2, 4, 6, 8, 10 };
printarray(num);
}
}
}
10. Renas R. Rekany Object-Oriented-Programming
10
8- Most common properties of Array class
Properties Explanation Example
Length Returns the length of array. Returns integer value. int i = arr1.Length;
Rank
Returns total number of items in all the dimension. Returns
integer value.
int i = arr1.Rank;
IsFixedSize
Check whether array is fixed size or not. Returns Boolean
value
bool i = arr.IsFixedSize;
IsReadOnly
Check whether array is ReadOnly or not. Returns Boolean
value
bool k =
arr1.IsReadOnly;
H.M) Apply these properties in arrays (Max, Min, Sum, Average, First, Last).
9- Most common functions of Array class:
Function Explanation Example
Sort Sort an array Array.Sort(arr);
Clear Clear an array by removing all the items Array.Clear(arr, 0, 3);
GetLength Returns the number of elements arr.GetLength(0);
GetValue Returns the value of specified items arr.GetValue(2);
IndexOf Returns the index position of value Array.IndexOf(arr,45);
Copy Copy array elements to another elements Array.Copy(arr1,arr1,3);
Example5:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void arr_mul(int[] array1)
{
int i;
double mul = 1;
for (i = 0; i < 5; i++)
{
textBox8.Text = textBox8.Text + array1[i];
mul *= array1[i];
}
11. Renas R. Rekany Object-Oriented-Programming
11
for (i = 0; i < 5; i++)
{
textBox9.Text = textBox9.Text+array1[i].ToString();
}
Array.Sort(array1);
for(i=0;i<5;i++)
{
textBox6.Text = textBox6.Text + array1[i].ToString();
}
textBox11.Text = mul.ToString() ;
}
private void button1_Click(object sender, EventArgs e)
{
int[] arr1 = new int[5] { 7, 3, 12, 8, 11 };
int[] arr2 = new int[5];
int len, r,gl,gt;
bool fixedsize, read_only;
Array.Copy(arr1, arr2, arr1.Length);
len = arr1.Length;
textBox1.Text = len.ToString();
r=arr1.Rank;
textBox2.Text = r.ToString();
fixedsize = arr1.IsFixedSize;
textBox3.Text = fixedsize.ToString();
read_only = arr1.IsReadOnly;
textBox4.Text = read_only.ToString();
gl=arr1.GetLength(0);
textBox5.Text = gl.ToString();
textBox10.Text = arr1.Sum().ToString();
gt=Convert.ToInt32(textBox7.Text);
textBox8.Text = arr1.GetValue(gt).ToString();
arr_mul(arr1);
}
}
}
Q) Write a program in C# to find the following in array, (Copy, Reverse, Find, Resize).