Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Methods
3.
Example
4.
Frequently Asked Questions
4.1.
What is C#?
4.2.
What is an array?
4.3.
What is the syntax to create an array in C#?
4.4.
What is the Array Class in C#?
4.5.
What are the applications of C#?
5.
Conclusion
Last Updated: Mar 27, 2024

C# Array Class

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C# is a multi-paradigm, general programming language. C# was designed by Anders Hejlsberg in 2000. Later it was introduced by Microsoft along with the .NET framework and Visual Studio.

The array is nothing but a collection of elements of a similar data type. Each element stored in an array can be accessed easily using only the index of the element. The Array Class in C# is used to manipulate data present in an array. Developers can reverse, sort, clone, empty, and perform many more functions on an array using the Array Class.

Methods

There are various methods present in the Array Class that can be used to work on arrays. Let us look at some of these methods,

  • AsReadOnly(): This method is used to return a read-only wrapper for a given array.
  • Clear(): The Clear method is used to a set of elements in an array.
  • BinarySearch(): The BinarySearch method is used to perform a binary search on a one-dimensional array.
  • Clone(): This method can be used to create a copy of a given array.
  • ConvertAll(): This method is used to convert an array from one data type to an array with a different data type.
  • Copy(): The Copy method is used to copy all the values from one array to another.
  • CreateInstance(): This method is used to create an instance of the Array Class.
  • Empty(): This method returns an empty array.
  • Find(): This method returns the first occurrence of the specified element in an array.
  • FindAll(): This method returns all the occurrences of the specified element in an array.
  • FindIndex(): This method returns the index of the first occurrence of the specified element in an array.
  • FindLastIndex(): This method returns the index of the last occurrence of the specified element in an array.
  • ForEach(): This method is used to iterate through all the elements present in an array.
  • GetHashCode(): This method is used as a default hash function for arrays.
  • Reverse(): This method is used to reverse the value of a one-dimensional array.
  • Sort(): The sort method is used to sort all the values in an array.
  • TrueForAll(): This method determines if all the elements present in the array satisfy the specified condition.

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example

using System;
class Array_Rank {
public static void Main()
{
int[] arr;
arr = new int[] {50, 10, 40, 90, 80, 20};
Console.WriteLine("Length of the array: {0}", arr.Length);
foreach(int i in arr)
Console.WriteLine(i + " ");
}
}

In the above example, we are using the length property of the Array Class to print out the size of the array.

 

using System;
class Array_Rank {
public static void Main()
{
int[] arr;
arr = new int[] {50, 10, 40, 90, 80, 20};
Console.WriteLine("Rank of the array: {0}", arr.Rank);
foreach(int i in arr)
Console.WriteLine(i + " ");
}
}

In the above example, we have used the rank method to print the rank of the Array. The rank for a one-dimensional array is one and for a two-dimensional array is two.

 

using System;
class Array_Rank {
public static void Main()
{
int[] arr;
arr = new int[] {50, 10, 40, 90, 80, 20};
Console.WriteLine("Array before reverse");
foreach(int i in arr)
Console.WriteLine(i + " ");
Console.WriteLine();
Array.Reverse(arr);
Console.WriteLine("Array after reverse");
foreach(int i in arr)
Console.WriteLine(i + " ");
}
}

In the above example, we are using the Array.Reverse method to reverse all the elements in the array.

 

using System;
class Array_Rank {
public static void Main()
{
int[] arr;
arr = new int[] {50, 10, 40, 90, 80, 20};
Console.WriteLine("Array before sorting");
foreach(int i in arr)
Console.WriteLine(i + " ");
Console.WriteLine();
Array.Sort(arr);
Console.WriteLine("Array after sorting");
foreach(int i in arr)
Console.WriteLine(i + " ");
}
}

In the above example, we are using the Array.Sort method to sort all the elements in the array in ascending order.


Also check out this article - singleton design pattern in c#

Frequently Asked Questions

What is C#?

C# is a multi-paradigm, general programming language. C# was designed by Anders Hejlsberg in 2000. Later it was introduced by Microsoft along with the .NET framework and Visual Studio.

What is an array?

The array is nothing but a collection of elements of a similar data type. Each element stored in an array can be accessed easily using only the index of the element.

What is the syntax to create an array in C#?

Arrays can be created using the following syntax in the C and C++ programming languages,

data_type[][] name_of_array = new data_type[rows][]

What is the Array Class in C#?

The Array Class in C# is used to manipulate data present in an array. Developers can reverse, sort, clone, empty, and perform many more functions on an array using the Array Class.

What are the applications of C#?

C# has a wide range of applications. It is used for Web development, Application Development, and Game Development. 

Conclusion

This blog covered all the necessary points associated with the Array Class in C#. We further looked at various methods present in the Array Class and also looked at a few examples of the arrays in C#.
Recommended problems -

 

Do check out our blogs on object-oriented programming and data structures

Don’t stop here. Check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass