# Array

***

**1. Array Declaration and Initialization**

```csharp
// Declare an array of integers
int[] numbers = new int[5];

// Initialize the array with values
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
```

**2. Array Length**

```csharp
// Get the length of the array
int length = numbers.Length;
```

**3. For Loop Iteration**

```csharp
// Iterate over the array using a for loop
for (int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);
}
```

**4. Foreach Loop Iteration**

```csharp
// Iterate over the array using a foreach loop
foreach (int number in numbers)
{
    Console.WriteLine(number);
}
```

**5. Array Sort**

```csharp
// Sort the array in ascending order
Array.Sort(numbers);
```

**6. Array Reverse**

```csharp
// Reverse the array
Array.Reverse(numbers);
```

**7. Array Copy**

```csharp
// Copy the array to a new array
int[] copy = new int[numbers.Length];
Array.Copy(numbers, copy, numbers.Length);
```

**8. Array Clear**

```csharp
// Clear the array
Array.Clear(numbers, 0, numbers.Length);
```

**9. Array Join**

```csharp
// Join the array elements into a string
string joinedString = string.Join(",", numbers);
```

**10. Array Concatenation**

```csharp
// Concatenate two arrays
int[] combinedArray = numbers.Concat(new int[] { 6, 7, 8 }).ToArray();
```

**11. Array Find**

```csharp
// Find the index of the first occurrence of a value in the array
int index = Array.FindIndex(numbers, x => x == 3);
```

**12. Array FindLast**

```csharp
// Find the index of the last occurrence of a value in the array
int lastIndex = Array.FindLastIndex(numbers, x => x == 3);
```

**13. Array Binary Search**

```csharp
// Perform a binary search for a value in the array
int binarySearchIndex = Array.BinarySearch(numbers, 3);
```

**14. Array Min and Max**

```csharp
// Get the minimum and maximum values in the array
int min = numbers.Min();
int max = numbers.Max();
```

**15. Array Sum**

```csharp
// Get the sum of all the values in the array
int sum = numbers.Sum();
```

**16. Array Average**

```csharp
// Get the average of all the values in the array
double average = numbers.Average();
```

**17. Array Variance**

```csharp
// Get the variance of all the values in the array
double variance = numbers.Variance();
```

**18. Array Standard Deviation**

```csharp
// Get the standard deviation of all the values in the array
double standardDeviation = numbers.StandardDeviation();
```

**19. Array Covariance**

```csharp
// Get the covariance of two arrays
double covariance = numbers.Covariance(new int[] { 6, 7, 8 });
```

**20. Array Correlation**

```csharp
// Get the correlation of two arrays
double correlation = numbers.Correlation(new int[] { 6, 7, 8 });
```

**21. Array Reshape**

```csharp
// Reshape the array into a new dimension
int[,] reshapedArray = numbers.Reshape(2, 3);
```

**22. Array Flatten**

```csharp
// Flatten a multidimensional array into a one-dimensional array
int[] flattenedArray = reshapedArray.Flatten();
```

**23. Array Jagged**

```csharp
// Create a jagged array (array of arrays)
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5, 6 };
jaggedArray[2] = new int[] { 7, 8, 9 };
```

**24. Array Rank**

```csharp
// Get the rank of the array
int rank = jaggedArray.Rank;
```

**25. Array Dimensions**

```csharp
// Get the dimensions of the array
int[] dimensions = jaggedArray.GetDimensions();
```

**26. Array GetEnumerator**

```csharp
// Iterate over the array using an enumerator
IEnumerator<int> enumerator = numbers.GetEnumerator();
while (enumerator.MoveNext())
{
    Console.WriteLine(enumerator.Current);
}
```

**27. Array LINQ**

```csharp
// Use LINQ to query the array
var selectedNumbers = numbers.Where(x => x % 2 == 0);
```

**28. Array Parallel**

```csharp
// Perform parallel operations on the array
Parallel.ForEach(numbers, number =>
{
    Console.WriteLine(number);
});
```

**29. Array Lambda**

```csharp
// Use lambda expressions to perform operations on the array
int[] squaredNumbers = numbers.Select(x => x * x).ToArray();
```

**30. Array Extension Methods**

```csharp
// Define an extension method to calculate the mean of an array
public static double Mean(this int[] array)
{
    return array.Sum() / (double)array.Length;
}
```

**31. Array Custom Comparer**

```csharp
// Define a custom comparer for sorting the array
public class CustomComparer : IComparer<int>
{
    public int Compare(int x, int y)
    {
        return x.CompareTo(y) * -1;
    }
}

// Sort the array using the custom comparer
Array.Sort(numbers, new CustomComparer());
```

**32. Array Equality**

```csharp
// Check if two arrays are equal
bool areEqual = numbers.SequenceEqual(new int[] { 1, 2, 3, 4, 5 });
```

**33. Array Deep Copy**

```csharp
// Perform a deep copy of the array
int[] deepCopy = numbers.Clone() as int[];
```

**34. Array Shallow Copy**

```csharp
// Perform a shallow copy of the array
int[] shallowCopy = numbers;
```

**35. Array Slice**

```csharp
// Slice the array into a new array
int[] slicedArray = numbers.Skip(2).Take(3).ToArray();
```

**36. Array Diff**

```csharp
// Get the difference between two arrays
int[] diffArray = numbers.Except(new int[] { 2, 4 }).ToArray();
```

**37. Array Intersect**

```csharp
// Get the intersection of two arrays
int[] intersectArray = numbers.Intersect(new int[] { 2, 4, 6 }).ToArray();
```

**38. Array Union**

```csharp
// Get the union of two arrays
int[] unionArray = numbers.Union(new int[] { 2, 4, 6 }).ToArray();
```

**39. Array Reverse For Each**

```csharp
// Iterate over the array in reverse order using a for each loop
foreach (int number in numbers.Reverse())
{
    Console.WriteLine(number);
}
```

**40. Array Sum By Key**

```csharp
// Group the array elements by a key and sum their values
var groupedNumbers = numbers.GroupBy(x => x % 2).ToDictionary(x => x.Key, x => x.Sum());
```

**41. Array Max By Key**

```csharp
// Group the array elements by a key and get the maximum value for each group
var maxNumbersByKey = numbers.GroupBy(x => x % 2).ToDictionary(x => x.Key, x => x.Max());
```

**42. Array Min By Key**

```csharp
// Group the array elements by a key and get the minimum value for each group
var minNumbersByKey = numbers.GroupBy(x => x % 2).ToDictionary(x => x.Key, x => x.Min());
```

**43. Array Count By Key**

```csharp
// Group the array elements by a key and count their occurrences
var countNumbersByKey = numbers.GroupBy(x => x % 2).ToDictionary(x => x.Key, x => x.Count());
```

**44. Array Aggregate**

```csharp
// Perform an aggregation on the array using an aggregate function
int totalSum = numbers.Aggregate((a, b) => a + b);
```

**45. Array Partition**

```csharp
// Partition the array into two subarrays based on a predicate
var partitionedNumbers = numbers.Partition(x => x % 2 == 0);
```

**46. Array ToObjectArray**

```csharp
// Convert the array to an object array
object[] objectArray = numbers.ToObjectArray();
```

**47. Array ToDictionary**

```csharp
// Convert the array to a dictionary
Dictionary<int, int> numberDictionary = numbers.ToDictionary(x => x, x => x * x);
```

**48. Array ToHashSet**

```csharp
// Convert the array to a hash set
HashSet<int> numberSet = numbers.ToHashSet();
```

**49. Array ToList**

```csharp
// Convert the array to a list
List<int> numberList = numbers.ToList();
```

**50. Array ToDataTable**

```csharp
// Convert the array to a data table
DataTable numberTable = numbers.ToDataTable();
```
