Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to declare and use a single-dimensional array in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = new int[] { 1, 2, 3, 4, 5 };
            foreach (int n1 in arr1)
                Console.Write("{0, 5}", n1);
            Console.WriteLine();
            arr1[0] = -1;
            arr1[4] = -5;
            foreach (int n1 in arr1)
                Console.Write("{0, 5}", n1);

            Console.WriteLine(); Console.WriteLine();
            int[] arr2 = new int[5] { 10, 20, 30, 40, 50 };
            foreach (int n2 in arr2)
                Console.Write("{0, 5}", n2);
            arr2[0] = -10;
            arr2[4] = -50;
            Console.WriteLine();
            foreach (int n2 in arr2)
                Console.Write("{0, 5}", n2);

            Console.WriteLine(); Console.WriteLine();
            int[] arr3 = { 100, 200, 300, 400, 500 };
            foreach (int n3 in arr3)
                Console.Write("{0, 5}", n3);
            arr3[0] = -100;
            arr3[4] = -500;
            Console.WriteLine();
            foreach (int n3 in arr3)
                Console.Write("{0, 5}", n3);
            Console.WriteLine();
        }
    }
}

/*
run:
      
    1    2    3    4    5
   -1    2    3    4   -5

   10   20   30   40   50
  -10   20   30   40  -50

  100  200  300  400  500
 -100  200  300  400 -500
     
*/

 



answered Sep 3, 2015 by avibootz
edited Feb 20, 2016 by avibootz

Related questions

...