In this tutorial, we’ ll learn How to passing arrays as arguments in C#.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | internal class Program { static void Result(int[] arr) { // displaying the array elements for (int i = 0; i < arr.Length; i++) { Console.WriteLine("Array Element: " + arr[i]); } } // Main method public static void Main() { // declaring an array // and initializing it int[] arr = { 1, 2, 3, 4, 5 }; // calling the method Result(arr); Console.ReadKey(); } } |
Output:
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | internal class Program { static int[] bubbleSort(int[] arr) { var done = false; while (!done) { done = true; for (var i = 1; i < arr.Length; i += 1) { if (arr[i - 1] > arr[i]) { done = false; var tmp = arr[i - 1]; arr[i - 1] = arr[i]; arr[i] = tmp; } } } return arr; } static void Main(string[] args) { int[] numbers = { 10, 20, 30, 50, 5, 25, 15, 45 }; bubbleSort(numbers); foreach(int i in numbers) { Console.WriteLine(i); } Console.ReadLine(); } } |