Arrays

What is the difference between arrays in C# and arrays in other programming languages?
Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences as listed below1. When declaring an array in C#, the square brackets ([]) must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#.
int[] IntegerArray; // not int IntegerArray[];2. Another difference is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of the array's length.int[] IntegerArray; // declare IntegerArray as an int array of any sizeIntegerArray = new int[10]; // IntegerArray is a 10 element arrayIntegerArray = new int[50]; // now IntegerArray is a 50 element arrayWhat are the 3 different types of arrays that we have in C#?1. Single Dimensional Arrays2. Multi Dimensional Arrays also called as rectangular arrays3. Array Of Arrays also called as jagged arraysAre arrays in C# value types or reference types?Reference types.
What is the base class for all arrays in C#?
System.Array
How do you sort an array in C#?
The Sort static method of the Array class can be used to sort array items.
Give an example to print the numbers in the array in descending order?
using System;
namespace ConsoleApplication
{class Program{static void Main()
{int[] Numbers = { 2, 5, 3, 1, 4 };//Print the numbers in the array without sortingConsole.WriteLine("Printing the numbers in the array without sorting");
foreach (int i in Numbers){Console.WriteLine(i);}//Sort and then print the numbers in the arrayConsole.WriteLine("Printing the numbers in the array after sorting");Array.Sort(Numbers);
foreach (int i in Numbers){Console.WriteLine(i);}//Print the numbers in the array in desceding orderConsole.WriteLine("Printing the numbers in the array in desceding order");Array.Reverse(Numbers);
foreach (int i in Numbers){Console.WriteLine(i);}}}}
What property of an array object can be used to get the total number of elements in an array?
Length property of array object gives you the total number of elements in an array. An example is shown below.
using System;
namespace ConsoleApplication
{
class Program
{
static void Main()
{int[] Numbers = { 2, 5, 3, 1, 4 };
Console.WriteLine("Total number of elements = " +Numbers.Length);
}}}
Give an example to show how to copy one array into another array?
We can use CopyTo() method to copy one array into another array. An example is shown below.
using System
;namespace ConsoleApplication
{
class Program
{
static void Main(){int[] Numbers = { 2, 5, 3, 1, 4 };
int[] CopyOfNumbers=new int[5];
Numbers.CopyTo(CopyOfNumbers,0);
foreach (int i in CopyOfNumbers){Console.WriteLine(i);
}
}
}
}
3) Which of these statements correctly declares a two-dimensional array in C#?
int[,] myArray;
int[][] myArray;
int[2] myArray;
System.Array[2] myArray;
Can you store multiple data types in System.Array?No.
What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object.
How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.