0% found this document useful (0 votes)
35 views

Progamming PPT

Arrays allow storing multiple values of the same type in a single variable. They can be single-dimensional, multidimensional, or jagged. Arrays are declared with syntax like type[] name and instantiated with new. Elements are accessed by index and properties provide metadata like length.

Uploaded by

Bhuwan Acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Progamming PPT

Arrays allow storing multiple values of the same type in a single variable. They can be single-dimensional, multidimensional, or jagged. Arrays are declared with syntax like type[] name and instantiated with new. Elements are accessed by index and properties provide metadata like length.

Uploaded by

Bhuwan Acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Array

C# Programming
Arrays in C#
• Arrays are a fundamental data structure in programming.
• They allow us to store multiple values of the same type in a single variable.
• An array can be single-dimensional, multidimensional , or jagged.
• The number of dimensions and the length of each dimension are established
when the array instance is created. These values can't be changed during the
lifetime of the instance.
• The default values of numeric array elements are set to zero, and reference
elements are set to null.
• Arrays are zero-indexed: an array with n elements is indexed from 0 to n-1.
Declaring Arrays
• Arrays are declared using the following syntax:
type[ ] array-name;
• Where type is the data type of the array elements.
• The following examples show how to declare different kinds of arrays:
• Single-dimensional arrays:
int[] numbers;
• Multidimensional arrays:
int[,] matrix;
• Array-of-arrays (jagged):
byte[][] scores;
Declaring Arrays cont…
• In C#, arrays are objects. That means that declaring an array
doesn't create an array. After declaring an array, you need to
instantiate an array by using the "new" operator.

• The following syntax defines arrays of different data types


double[] doubleArray = new double[5];
char[] charArray = new char[5];
bool[] boolArray = new bool[2];
string[] stringArray = new string[10];
Initializing Arrays
• Once an array is declared, the next step is to initialize the array. The initialization
process of an array includes adding actual data to the array.
• The following sample syntaxes creates an array of 3 items and the values of these
items are added when the array is initialized.
int[] sArray = new int[3] {1, 3, 5};
• Alternative, we can also add array items one at a time as listed in the following code
snippet.
int[ ] sArray = new int[3];
sArray[0] = 1;
sArray[1] = 3;
sArray[2] = 5;
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
Accessing array elements
• We can access an array item by passing the item index in the array.
• Access array elements using subscripts or indexes:
value = arrayName[index];
• The following syntax creates an array of three items and displays those items on the
console.
int[] staticIntArray = new int[3];
staticIntArray[0] = 1;
staticIntArray[1] = 3;
staticIntArray[2] = 5;
Console.WriteLine(staticIntArray[0]);
Console.WriteLine(staticIntArray[1]);
Console.WriteLine(staticIntArray[2]);
• We can also use loop to access data from an array.
string[ ] strArray = new string[]{ “Bhuwan”, “Sabin”, “Ajin”, “Ram", “Harry”};
foreach (string str in strArray)
{
Console.WriteLine(str);
}
Retrieving Array
Metadata
• The Array class provides several properties
for retrieving metadata about an array
• Length: Returns the total number of
elements in all dimensions of an array.
• GetLength: Returns the number of
elements in the specified dimension of an
array
• Rank: Returns the number of dimensions
of an array
• GetType: Returns the Type of the current
array instance
Example

int[] numbers = { 1, 2, 3, 4, 5 };

Console.WriteLine("Integer Array:");
Console.WriteLine("Length: " +
numbers.Length);
string[] names = { "John", "Jane",
"Alice" };
Console.WriteLine("String Array:");
Console.WriteLine("Length: " +
names.Length);
Array Methods
• C# provides methods like Sort()
and Reverse() for sorting and
reversing arrays.
• Sort() arranges the elements in
ascending order.
• Reverse() reverses the order of
elements.
Example
int[] numbers = { 5, 2, 8, 1, 4 };
// Sorting the array in ascending order
Array.Sort(numbers);
Console.WriteLine("Sorted Array (Ascending Order):");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
Console.WriteLine();
// Reversing the array
Array.Reverse(numbers);

Console.WriteLine("Reversed Array:");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
Console.WriteLine();
Summary
• Today, we covered important concepts related to the C# programming
language.
• We learned about declaring and assigning values to arrays, accessing
elements using subscripts, and using the Length property.
• We also explored the Sort() and Reverse() methods for array
manipulation.
• Additionally, we discussed other useful array methods.
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy