Home > Backend Development > C#.Net Tutorial > Reverse an array using C#

Reverse an array using C#

WBOY
Release: 2023-09-17 08:05:02
forward
866 people have browsed it

使用 C# 反转数组

First, set the original array-

int[] arr = { 1, 2,3 };

// Original Array
Console.WriteLine("Original Array= ");
fo            reach (int i in arr) {
   Console.WriteLine(i);
}
Copy after login

Now, use the Array.reverse() method to reverse the array-

Array.Reverse(arr);
Copy after login

The following is in C# Complete code for reversing an array in -

Example

using System;

class Demo {

   static void Main() {
      int[] arr = { 9, 32, 87, 45, 77, 56 };

      // Original Array
      Console.WriteLine("Original Array= ");
      foreach (int i in arr) {
         Console.WriteLine(i);
      }

      // Reverse Array
      Array.Reverse(arr);

      Console.WriteLine("Reversed Array= ");
      foreach (int j in arr) {
         Console.WriteLine(j);
      }
      Console.ReadLine();
   }
}
Copy after login

The above is the detailed content of Reverse an array using C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template