
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialArrays contain a collection of data elements of the same type. These data elements are stored in contiguous memory locations. Some of the arrays in C# are 1-D arrays, 2-D arrays, Jagged arrays, Param arrays etc. Details about these are given as follows:
One dimensional arrays consist of a single row with as many elements as required. These arrays can be declared using the following syntax:
data_type[ ] name_of_array;
In the above syntax, data_type is the data type of array elements and name_of_array is the name given to the array.
The syntax to initialize the array is given as follows:
data_type[ ] name_of_array = new data_type[array_size];
In the above syntax, data_type is the data type of array elements, name_of_array is the name given to the array, new is a keyword that creates an instance of the array and array_size is the size of the array.
There are many methods to assign values to the array. Some of these are given as follows:
int[ ] arr = { 1, 2, 3, 4, 5};
int[ ] arr = new int[] { 1, 2, 3, 4, 5};
int[ ] arr = new int[5] { 1, 2, 3, 4, 5};
A program that demonstrates one dimensional arrays is given as follows:
using System;
namespace ArrayDemo
{
class Example
{
static void Main(string[ ] args)
{
int [ ] arr = new int[5] {1, 2, 3, 4, 5};
int i;
Console.WriteLine("Elements in the array are:");
for ( i = 0; i < 5; i++ )
{
Console.WriteLine(arr[i]);
}
}
}
}
Source Code: Program that demonstrates one dimensional arrays in C#
This is the output of the program:
Elements in the array are:
1
2
3
4
5
Two dimensional arrays contain multiple rows and columns and each element of the array is identified as arr[i,j] where i and j are the subscripts for row and column index respectively and the name of the array is arr.
The 2-D arrays can be declared using the following syntax:
data_type[ , ] name_of_array = new data_type[row_size, column_size];
In the above syntax, data_type is the data type of array elements, name_of_array is the name given to the array, new is a keyword that creates an instance of the array,row_size is the number of rows and column_size is the number of columns.
One of the methods to assign values in a 2-D array is given as follows:
int[ , ] arr = new int [2,2] { {1,4} , {8,3} };
A program that demonstrates two dimensional arrays is given as follows:
using System;
namespace ArrayDemo
{
class Example
{
static void Main(string[] args)
{
int[,] arr = new int[2, 3] { {4,1,7}, {3, 8, 2} };
int i, j;
Console.WriteLine("The elements in the 2-D array are:");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write(arr[i,j]);
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}
Source Code: Program that demonstrates two dimensional arrays in C#
This is the output of the above program:
The elements in the 2-D array are:
4 1 7
3 8 2
Jagged arrays are arrays of arrays. The arrays in a jagged array can be of different sizes.
The declaration of elements in a jagged array are given as follows:
int[ ][ ] myArr = new int [4][ ];
In the above initialization, myArr is the name of the jagged array. There are 4 elements in the jagged array and each of these elements are 1-D integer arrays.
Element initialization in jagged arrays is given as follows:
myArr[0] = new int[ ] { 7, 1, 5, 9 };
myArr[1] = new int[ ] { 2, 4, 8 };
myArr[2] = new int[ ] { 1, 5, 8, 2, 9};
myArr[3] = new int[ ] { 4, 3 };
Here, each of the array elements are 1-D arrays with sizes 4, 3, 5, 2 respectively.
A program that demonstrates jagged arrays is given as follows:
using System;
namespace ArrayDemo
{
class Example
{
static void Main(string[] args)
{
int[][] myArr = new int[4][];
myArr[0] = new int[ ] { 7, 1, 5, 9 };
myArr[1] = new int[ ] { 2, 4, 8 };
myArr[2] = new int[ ] { 1, 5, 8, 2, 9};
myArr[3] = new int[ ] { 4, 3 };
for (int i = 0; i < myArr.Length; i++)
{
System.Console.Write("Element {0}: ", i);
for (int j = 0; j < myArr[i].Length; j++)
{
System.Console.Write( myArr[i][j] );
System.Console.Write(" ");
}
System.Console.WriteLine();
}
}
}
}
Source Code: Program that demonstrates jagged arrays in C#
This is the output of the above program:
Element 0: 7 1 5 9
Element 1: 2 4 8
Element 2: 1 5 8 2 9
Element 3: 4 3
Param arrays are used if the number of arguments are not fixed in a function. So the user can pass as many arguments as required.
A program that demonstrates param arrays is given as follows:
using System;
namespace ArrayDemo
{
class Example
{
static int SumOfElements(params int[] p_arr)
{
int sum=0;
foreach (int i in p_arr)
{
sum += i;
}
return sum;
}
static void Main(string[] args)
{
int sum=0;
sum = SumOfElements(4, 8, 7, 3, 1);
Console.WriteLine("Sum of 5 Parameters: {0}", sum);
sum = SumOfElements(5, 7);
Console.WriteLine("Sum of 2 Parameters: {0}", sum);
sum = SumOfElements(10, 7, 9);
Console.WriteLine("Sum of 3 Parameters: {0}", sum);
}
}
}
Source Code: Program that demonstrates param arrays in C#
This is the output of the program:
Sum of 5 Parameters: 23
Sum of 2 Parameters: 12
Sum of 3 Parameters: 26
Arrays can be passed as parameters to functions. An example of this is as follows:
int[ ] arr = {1, 2, 3, 4, 5};
func(arr);
In the above example, the array arr is passed to the function func().
A program that demonstrates passing arrays to functions is given as follows:
using System;
namespace ArrayDemo
{
class Example
{
static void func(int[] arr, int n)
{
int i, sum=0;
Console.WriteLine("Array Elements are:");
for (i = 0; i < n; ++i)
{
Console.Write(arr[i]);
Console.Write(" ");
}
Console.WriteLine();
for (i = 0; i < n; ++i)
{
sum += arr[i];
}
Console.WriteLine("Sum of array elements is: {0}",sum);
}
static void Main(string[] args)
{
int[] arr= new int[] {12, 56, 3, 81, 9, 13, 76, 99, 5, 10};
func(arr, 10 ) ;
}
}
}
Source Code: Program that demonstrates passing arrays to functions in C#
The output of the above program is as follows:
Array Elements are:
12 56 3 81 9 13 76 99 5 10
Sum of array elements is: 364
We will look at some of the various operations that can be done on arrays.
The program that demonstrates finding the common elements in three arrays that are sorted is given as follows:
using System;
class Demo
{
static void commonElements(int []arr1, int []arr2, int []arr3)
{
int i = 0, j = 0, k = 0;
while (i < arr1.Length && j < arr2.Length && k < arr3.Length)
{
if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
{
Console.Write(arr1[i] + " ");
i++;
j++;
k++;
}
else if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr3[k])
j++;
else
k++;
}
}
public static void Main()
{
int []arr1 = {1, 11, 45, 89, 90};
int []arr2 = {1, 23, 45, 56, 89, 90};
int []arr3 = {1, 8, 11, 45, 67, 89, 90, 95};
Console.Write("The common elements in the arrays are: ");
commonElements(arr1, arr2, arr3);
}
}
Source Code: Program that finds common elements in three sorted arrays in C#
The output of the above program is as follows:
The common elements in the arrays are: 1 45 89 90
The program that demonstrates the dynamic creation of arrays is given as follows:
using System;
using System.Collections;
namespace CollectionApplication
{
class Example
{
static void Main(string[] args)
{
ArrayList myAL = new ArrayList();
myAL.Add(6);
myAL.Add(1);
myAL.Add(9);
myAL.Add(2);
myAL.Add(4);
Console.WriteLine("The number of elements are: {0}", myAL.Count);
Console.Write("The ArrayList is: ");
foreach (int i in myAL)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}
Source Code: Program that demonstrates the dynamic creation of arrays in C#
The output of the above program is as follows:
The number of elements are: 5
The ArrayList is: 6 1 9 2 4
To concatenate two arrays, use the Join() method in C#.
using System;
class Example
{
static void Main()
{
string[] str = new string[] { "Concatenation", "in C#" };
string result = string.Join(" ", str);
Console.WriteLine(result);
}
}
Source Code: Program that demonstrates the concatenation of two arrays in C#
The output of the above program is as follows:
Concatenation in C#
To perform intersection of two arrays, use the Intersect() method in C#.
using System;
using System.Linq;
class Example
{
static void Main()
{
int[] arr1 = { 56, 12, 89, 67, 1, 45, 9};
int[] arr2 = { 4, 98, 56, 45, 9, 89 };
var arr_intersect = arr1.Intersect(arr2);
Console.WriteLine("The elements in the intersection of the two arrays are:");
foreach (int i in arr_intersect)
{
Console.Write(i + " ");
}
}
}
Source Code: Program that demonstrates the intersection of two arrays in C#
The output of the above program is as follows:
The elements in the intersection of the two arrays are:
56 89 45 9
Mixed arrays in C# are a combination of multidimensional arrays and jagged arrays. They are currently obsolete as they were removed by the .NET 4.0 update. A program that demonstrates mixed arrays is given as follows:
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
List<string> list = new List<string>()
{
"Harry",
"Amy",
"Micheal",
"Lucy",
"Susan",
"Peter"
};
Console.WriteLine("The element count in List is: {0}", list.Count);
}
}
Source Code: Program that demonstrates mixed arrays in C#
The output of the above program is as follows:
The element count in List is: 6
Two arrays in C# can be compared using SequenceEqual(). The program that demonstrates this is given as follows:
using System;
using System.Linq;
namespace ArrayDemo
{
class Example
{
static void Main(string[] args)
{
int[] arr1 = new int[] { 9, 7, 5, 6};
int[] arr2 = new int[] { 9, 7, 5, 6};
Console.WriteLine("The two arrays are equal? {0}", arr1.SequenceEqual(arr2));
}
}
}
Source Code: Program that demonstrates comparison of two arrays in C#
The output of the above program is as follows:
The two arrays are equal? True
A program that splits the even and odd numbers in an array into 2 different arrays is given as follows:
using System;
namespace ArrayDemo
{
public class Example
{
public static void Main(string[] args)
{
int[] arr = new int[] { 45, 12, 9, 36, 77, 54, 7 };
int[] arr1 = new int[5];
int[] arr2 = new int[5];
int i, j = 0, k = 0;
for (i = 0; i < 7; i++)
{
if (arr[i] % 2 == 0)
{
arr1[j] = arr[i];
j++;
}
else
{
arr2[k] = arr[i];
k++;
}
}
Console.Write("Even numbers are: ");
for (i = 0; i < j; i++)
{
Console.Write(arr1[i] + " ");
}
Console.Write("\nOdd numbers are: ");
for (i = 0; i < k; i++)
{
Console.Write(arr2[i] + " ");
}
}
}
}
Source Code: Program that splits the even and odd numbers in an array in C#
The output of the above program is as follows:
Even numbers are: 12 36 54
Odd numbers are: 45 9 77 7
A program that merges two sorted arrays into a single array is given as follows:
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
int i = 0;
int j = 0;
int[] arr1 = new int[4] { 1, 23, 35, 49 };
int[] arr2 = new int[4] { 51, 66, 89, 95 };
int[] mergeArr = new int[8];
for (i = 0, j = 0; i < 4; i++)
{
mergeArr[j++] = arr1[i];
}
for (i = 0; i < 4; i++)
{
mergeArr[j++] = arr2[i];
}
Console.Write("The elements of merged array are: ");
for (i = 0; i < 8; i++)
{
Console.Write(mergeArr[i] + " ");
}
}
}
Source Code: Program that merges two sorted arrays into a single array in C#
The output of the above program is as follows:
The elements of merged array are: 1 23 35 49 51 66 89 95
The program that merges two sorted arrays into a single array using lists is given as follows:
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6 };
var list = new List<int>();
for (int i = 0; i < arr1.Length; i++)
{
list.Add(arr1[i]);
list.Add(arr2[i]);
}
int[] arr3 = list.ToArray();
Console.WriteLine("The merged array is:");
foreach(int i in arr3)
{
Console.WriteLine(i);
}
}
}
Source Code: Program that merges two sorted arrays into a single array using lists in C#
The output of the above program is as follows:
The merged array is:
1
4
2
5
3
6
The program that merges two arrays using AddRange() method is given as follows:
using System;
using System.Collections.Generic;
class Demo
{
static void Main()
{
int[] arr1 = { 1, 10, 17, 26, 30 };
int[] arr2 = { 35, 55, 67, 80, 99 };
Console.Write("The first array is: ");
foreach(int i in arr1)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.Write("The second array is: ");
foreach(int i in arr2)
{
Console.Write(i + " ");
}
Console.WriteLine();
var myList = new List<int>();
myList.AddRange(arr1);
myList.AddRange(arr2);
int[] arr3 = myList.ToArray();
Console.Write("The merged array is: ");
foreach (int i in arr3)
{
Console.Write(i + " ");
}
}
}
Source Code: Program that merges two arrays using AddRange() method in C#
The output of the above program is as follows:
The first array is: 1 10 17 26 30
The second array is: 35 55 67 80 99
The merged array is: 1 10 17 26 30 35 55 67 80 99
The program that obtains the last element of an array is given as follows. We have used the Last() method here to get the last element.
using System;
using System.Collections.Generic;
using System.Linq;
class Demo
{
static void Main()
{
int[] arr = { 1, 6, 24, 25, 37, 40, 49, 50 };
int lastElement = arr.AsQueryable().Last();
Console.WriteLine("The last element of the array is: " + lastElement);
}
}
Source Code: Program that obtains the last element of an array in C#
The output of the above program is as follows:
The last element of the array is: 50
The program that prints the contents of an array horizontally is given as follows:
using System;
using System.Linq;
using System.IO;
class Example
{
static void Main()
{
int[] arr = new int[] { 5, 1, 9, 7, 3 };
Console.Write("The array elements are: ");
Console.WriteLine(string.Join(" ", arr));
}
}
Source Code: Program that prints the contents of an array horizontally in C#
The output of the above program is as follows:
The array elements are: 5 1 9 7 3
The program that displays the first element from an array is given as follows. We have used the First() method to get the first element.
using System;
using System.Linq;
using System.Collections.Generic;
class Example
{
static void Main()
{
int[] arr = {1, 3, 4, 7, 9};
int first = arr.AsQueryable().First();
Console.WriteLine("The first element in the array is: {0}", first);
}
}
Source Code: Program that displays the first element from an array in C#
The output of the above program is as follows:
The first element in the array is: 1
The program that orders the elements in the array is given as follows:
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
string[] str = { "Harry", "Jack", "Amy", "Matthew", "Joseph" };
IEnumerable<string> strResult =
str.AsQueryable()
.OrderBy(alp => alp.Length).ThenBy(alp => alp);
foreach (string s in strResult)
Console.WriteLine(s);
}
}
Source Code: Program that orders the elements in the array in C#
The output of the above program is as follows:
Amy
Jack
Harry
Joseph
Matthew
The program that orders the elements in the array in descending order is given as follows:
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
string[] str = { "Harry", "Jack", "Amy", "Matthew", "Joseph" };
IEnumerable<string> strResult =
str.AsQueryable()
.OrderByDescending(ch => ch.Length).ThenBy(ch => ch);
foreach (string s in strResult)
Console.WriteLine(s);
}
}
Source Code: Program that orders the elements in the array in descending order in C#
The output of the above program is as follows:
Matthew
Joseph
Harry
Jack
Amy
The program that reverses an array is given as follows. Use Array.Reverse() method to perform reverse.
using System;
class Example
{
static void Main()
{
int[] arr = { 12, 3, 67, 54, 99};
Console.Write("The original array is: ");
foreach (int i in arr)
{
Console.Write(i + " ");
}
Array.Reverse(arr);
Console.WriteLine();
Console.Write("The reversed array is: ");
foreach (int i in arr)
{
Console.Write(i + " ");
}
}
}
Source Code: Program that reverses an array in C#
The output of the above program is as follows:
The original array is: 12 3 67 54 99
The reversed array is: 99 54 67 3 12
The Array class is defined in the System namespace. Using Array class, you can easily work with arrays. It is the base class for all the arrays in C#.
The different properties and their description is given as follows:
Properties | Description |
|---|---|
IsFixedSize | This property gets a value indicating whether the Array has a fixed size. |
IsReadOnly | This property gets a value indicating whether the Array is read-only. |
IsSynchronized | This property gets a value indicating whether access to the Array is synchronized (thread safe). |
Length | This property gets the total number of elements in all the dimensions of the Array. |
LongLength | This property gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array. |
Rank | This property gets the rank (number of dimensions) of the Array. For example, a one-dimensional array returns 1, a two-dimensional array returns 2, and so on. |
SyncRoot | This property gets an object that can be used to synchronize access to the Array. |
Source: MSDN
The different methods and their description is given as follows:
Methods | Description |
|---|---|
AsReadOnly<T>(T[]) | This method returns a read-only wrapper for the specified array. |
BinarySearch(Array, Int32, Int32, Object) | This method searches a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value. |
Clear(Array, Int32, Int32) | This method sets a range of elements in an array to the default value of each element type. |
Clone() | This method creates a shallow copy of the Array. |
ConstrainedCopy(Array, Int32, Array, Int32, Int32) | This method copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely. |
Copy(Array, Array, Int32) | This method copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer. |
CreateInstance(Type, Int32) | This method creates a one-dimensional Array of the specified Type and length, with zero-based indexing. |
Empty<T>() | This method returns an empty array. |
Equals(Object) | This method determines whether the specified object is equal to the current object. |
Exists<T>(T[], Predicate<T>) | This method determines whether the specified array contains elements that match the conditions defined by the specified predicate. |
GetEnumerator() | This method returns an IEnumerator for the Array. |
GetHashCode() | This method serves as the default hash function. |
GetLength(Int32) | This method gets a 32-bit integer that represents the number of elements in the specified dimension of the Array. |
GetType() | This method gets the Type of the current instance. |
GetUpperBound(Int32) | This method gets the index of the last element of the specified dimension in the array. |
GetValue(Int32) | This method gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer. |
IndexOf(Array, Object) | This method searches for the specified object and returns the index of its first occurrence in a one-dimensional array. |
Initialize() | This method initializes every element of the value-type Array by calling the default constructor of the value type. |
Reverse(Array) | This method reverses the sequence of the elements in the entire one-dimensional Array. |
SetValue(Object, Int32) | This method sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer. |
Sort(Array) | This method sorts the elements in an entire one-dimensional Array using the IComparableimplementation of each element of the Array. |
ToString() | This method returns a string that represents the current object. |
Source: MSDN
We will cover some ArrayClass operations here.
A program that gets the upperbound and lowerbound of a three-dimensional array is given as follows:
using System;
class Example
{
static void Main()
{
int[,,] array = new int[3,4,5];
Console.WriteLine("Upper Bound : {0}",array.GetUpperBound(0).ToString());
Console.WriteLine("Lower Bound : {0}",array.GetLowerBound(0).ToString());
Console.WriteLine("Upper Bound : {0}",array.GetUpperBound(1).ToString());
Console.WriteLine("Lower Bound : {0}",array.GetLowerBound(1).ToString());
Console.WriteLine("Upper Bound : {0}",array.GetUpperBound(2).ToString());
Console.WriteLine("Lower Bound : {0}",array.GetLowerBound(2).ToString());
}
}
Source Code: Program that gets the upperbound and lowerbound of a three dimensional array in C#
The output of the above program is as follows:
Upper Bound : 2
Lower Bound : 0
Upper Bound : 3
Lower Bound : 0
Upper Bound : 4
Lower Bound : 0
The program that sorts one dimensional array in descending order using Array Class method is given as follows:
using System;
namespace Demo
{
public class MyApplication
{
public static void Main(string[] args)
{
int[] arr = {34, 76, 1, 99, 68};
Console.Write("The original unsorted array is: ");
foreach (int i in arr)
{
Console.Write(i + " ");
}
Array.Sort(arr);
Array.Reverse(arr);
Console.WriteLine();
Console.Write("The sorted array is: ");
for(int i=0; i<arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
}
}
Source Code: Program that sorts one dimensional array in descending order in C#
The output of the above program is as follows:
The original unsorted array is: 34 76 1 99 68
The sorted array is: 99 76 68 34 1
A program that sorts an array in C# is given as follows. The Array.Sort() method is used to sort an array.
using System;
class Demo
{
static void Main()
{
int[] arr = { 23, 8, 2, 99, 67 };
Array.Sort(arr);
Console.WriteLine("The sorted array is: ");
foreach (int i in arr)
{
Console.Write(i + " ");
}
}
}
Source Code: Program that sorts an array in C#
The output of the above program is as follows:
The sorted array is:
2 8 23 67 99
The program that uses the ?: conditional operator in C# is given as follows:
using System;
namespace Example
{
class Example
{
static void Main(string[] args)
{
int a, b = 5;
a = ( b == 5 ? 10 : 0 ) ;
Console.WriteLine("a = " + a);
Console.WriteLine("b = " + b);
Console.ReadKey();
}
}
}
Source Code: Program that uses the ? conditional operator in C#
The output of the above program is as follows:
a = 10
b = 5
The program that demonstrates the clear() method of array class is given as follows:
using System;
class Example
{
static void Main()
{
int[] arr = new int[] {12, 67, 34, 99, 7};
Console.WriteLine("The original array is given as follows:");
foreach (int i in arr)
{
Console.Write(i + " ");
}
Array.Clear(arr, 0, arr.Length);
Console.WriteLine();
Console.WriteLine("The array after clear() method is given as follows:");
foreach (int i in arr)
{
Console.Write(i + " ");
}
}
}
Source Code: Program that demonstrates the clear() method of array class in C#
The output of the above program is as follows:
The original array is given as follows:
12 67 34 99 7
The array after clear() method is given as follows:
0 0 0 0 0
The program that demonstrates the clone() method of array class is given as follows:
using System;
class Example
{
static void Main()
{
string[] array = { "Clone() method", "in C#"};
Console.WriteLine(string.Join(" ", array));
string[] arrayClone = array.Clone() as string[];
Console.WriteLine(string.Join(" ", arrayClone));
}
}
Source Code: Program that demonstrates the clone() method of array class in C#
The output of the above program is as follows:
Clone () method in C#
Clone () method in C#
The program that demonstrates the copy(, ,) method of array class is given as follows:
using System;
class Example
{
static void Main()
{
int[] arr1 = new int[5] { 45, 12, 9, 77, 34};
int[] arr2 = new int[5];
Array.Copy(arr1, arr2, 5);
Console.WriteLine("arr1: ");
foreach (int i in arr1)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.WriteLine("arr2: ");
foreach (int i in arr2)
{
Console.Write(i + " ");
}
}
}
Source Code: Program that demonstrates the copy(, ,) method of array class in C#
The output of the above program is as follows:
arr1:
45 12 9 77 34
arr2:
45 12 9 77 34
The program that demonstrates the CopyTo(,) method of array class is given as follows:
using System;
class Example
{
static void Main()
{
int[] arr1 = new int[5]{12, 55, 9, 1, 78};
int[] arr2 = new int[5];
Console.WriteLine("arr1: ");
foreach (int i in arr1)
{
Console.Write(i + " ");
}
arr1.CopyTo(arr2,0 );
Console.WriteLine();
Console.WriteLine("arr2: ");
foreach (int i in arr2)
{
Console.Write(i + " ");
}
}
}
Source Code: Program that demonstrates the CopyTo(,) method of array class in C#
The output of the above program is as follows:
arr1:
12 55 9 1 78
arr2:
12 55 9 1 78
The program that demonstrates the GetLength() method of array class is given as follows:
using System;
class Example
{
static void Main()
{
int[,] a = new int[5, 2];
int length1 = a.GetLength(0);
int length2 = a.GetLength(1);
Console.WriteLine(length1);
Console.WriteLine(length2);
}
}
Source Code: Program that demonstrates the GetLength() method of array class in C#
The output of the above program is as follows:
5
2
The program that demonstrates the GetLongLength() method of array class is given as follows:
using System;
class Example
{
static void Main()
{
int[,] arr1 = new int[6, 2];
int len1 = arr1.GetLength(0);
Console.WriteLine(len1);
long[,] arr2= new long[76, 89];
long len2 = arr2.GetLongLength(0);
Console.WriteLine(len2);
}
}
Source Code: Program that demonstrates the GetLongLength() method of array class in C#
The output of the above program is as follows:
6
76
The program that demonstrates the GetLowerBound() method of array class is given as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lowerBound
{
class Example
{
static void Main(string[] args)
{
Array a = Array.CreateInstance(typeof(String), 5);
a.SetValue("James", 0);
a.SetValue("Lily", 1);
a.SetValue("Harry", 2);
a.SetValue("Bill", 3);
a.SetValue("Charlie", 4);
Console.WriteLine("The lower bound is: {0}", a.GetLowerBound(0).ToString());
}
}
}
Source Code: Program that demonstrates the GetLowerBound() method of array class in C#
The output of the above program is as follows:
The lower bound is: 0
The program that demonstrates the GetType() method of array class is given as follows:
using System;
public class Example
{
public static void Main()
{
object[] values = { (int) 67, (long) 84658};
foreach (var val in values)
{
Type t = val.GetType();
if (t.Equals(typeof(int)))
Console.WriteLine("{0} is an integer data type.", val);
else
Console.WriteLine("{0} is not an integer data type.", val);
}
}
}
Source Code: Program that demonstrates the GetType() method of array class in C#
The output of the above program is as follows:
67 is an integer data type.
84658 is not an integer data type.
The program that demonstrates the GetUpperBound() method of array class is given as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo
{
class Example
{
static void Main(string[] args)
{
Array a = Array.CreateInstance(typeof(String), 5);
a.SetValue("James", 0);
a.SetValue("Lily", 1);
a.SetValue("Harry", 2);
a.SetValue("Bill", 3);
a.SetValue("Charlie", 4);
Console.WriteLine("The upper bound is: {0}",a.GetUpperBound(0).ToString());
}
}
}
Source Code: Program that demonstrates the GetUpperBound() method of array class in C#
The output of the above program is as follows:
The upper bound is: 4
A program that demonstrates the GetValue() method of array class is given as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo
{
class Example
{
static void Main(string[] args)
{
Array a = Array.CreateInstance(typeof(String), 3, 6);
a.SetValue("Apple", 0, 0);
a.SetValue("Mango", 0, 1);
a.SetValue("Cherry", 0, 2);
a.SetValue("Grapes", 0, 3);
a.SetValue("Melon", 1, 4);
a.SetValue("Guava", 1, 5);
a.SetValue("Plum", 1, 2);
a.SetValue("Berry", 1, 3);
int x = a.GetLength(0);
int y = a.GetLength(1);
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
Console.WriteLine( a.GetValue(i, j));
}
}
}
Source Code: Program that demonstrates the GetValue() method of array class in C#
The output of the above program is as follows:
Apple
Mango
Cherry
Grapes
Plum
Berry
Melon
Guava
A program that demonstrates the IndexOf(,) method of array class is given as follows:
Source Code: Program that demonstrates the IndexOf(,) method of array class in C#
using System;
class Example
{
static void Main()
{
int[] a = new int[5] {45, 78, 12, 99, 4};
int index = Array.IndexOf(a, 12);
Console.WriteLine("The index of 12 is {0}", index);
}
}
The output of the above program is as follows:
The index of 12 is 2