c# Array Part I

This is my first post, this is intended for beginners, I just hope someone will benefit from this. I'll be posting more tutorials on C#
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ArraysPartOne
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             /*
  13.              * sometimes programmers needs to use
  14.              * multiple objects of the same type(value types or reference types)
  15.              * so we use arrays.
  16.              * Definition of array -> is a data structure that contains a number of
  17.              * elements of the same type. (Professional C# 2008 Wrox)
  18.              */
  19.  
  20.             /*
  21.              * how to declare an array?
  22.              * so there are many ways on how to declare an array
  23.              * just try to look below
  24.              */
  25.  
  26.             int[] _FirstArray = new int[5];
  27.  
  28.             int[] _SecondArray = new int[2] {4,5,6};
  29.  
  30.             int[] _ThirdArray = new int[] { 4, 5, 6 };
  31.  
  32.             int[] _FourthArray = { 6,7,8};
  33.  
  34.             // end of declaring or initilizing an array
  35.  
  36.             //start of our sample array
  37.             try
  38.             {
  39.  
  40.                 Console.WriteLine("Array Sample Part I");
  41.  
  42.                 int intInputNumber =0;
  43.                 int intCounter =0;
  44.  
  45.                 Console.WriteLine("How many number to want to input");
  46.                  
  47.            
  48.                 intInputNumber = int.Parse (Console.ReadLine());
  49.                  
  50.                 int [] _UserArray = new int[intInputNumber];
  51.  
  52.                
  53.                
  54.                 do
  55.                 {
  56.                     Console.WriteLine(
  57.                     string.Format
  58.                    ("Input numbers at index[{0}]",intCounter ));
  59.  
  60.                     _UserArray[intCounter] = int.Parse(Console.ReadLine());
  61.  
  62.                     intCounter++;
  63.                  
  64.                 }while (intCounter < intInputNumber);
  65.  
  66.  
  67.                 //iterate throught the elements in the array ,
  68.                //i'll be using for and foreach loop
  69.  
  70.                 Console.WriteLine("Iterate elements using for loop");
  71.  
  72.                 for (int i = 0;
  73.                          i < _UserArray.Length; i++)
  74.                 {
  75.                     string strMessage =
  76.                     string.Format
  77.                    ("Array Index [{0}] = [{1}]", i, _UserArray[i]);
  78.                      
  79.                     Console.WriteLine(strMessage + Environment.NewLine);
  80.                 }
  81.  
  82.                 Console.WriteLine("Iterate elements using foreach loop");
  83.  
  84.                 StringBuilder result = new StringBuilder();
  85.  
  86.                 foreach (int IndexValue in _UserArray)
  87.                 {
  88.                     result.Append(IndexValue);
  89.                     result.Append(",");
  90.                 }
  91.  
  92.                 Console.WriteLine(result);
  93.  
  94.                 Console.WriteLine("Do you want to sort the array?[y/n]");
  95.  
  96.                 string ans = Console.ReadLine();
  97.  
  98.                 if (ans.ToUpper().Equals("Y"))
  99.                 {
  100.                     Array.Sort(_UserArray);
  101.                  
  102.                 }
  103.  
  104.                 //iterate throught the elements
  105.                //in the array ,i'll be using for  loop
  106.  
  107.                 Console.WriteLine("Iterate elements using for loop");
  108.  
  109.                 for (int i = 0; i < _UserArray.Length; i++)
  110.                 {
  111.                     string strMessage =
  112.                     string.Format
  113.                     ("Array Index [{0}] = [{1}]",
  114.                                     i, _UserArray[i]);
  115.  
  116.                     Console.WriteLine
  117.                     (strMessage + Environment.NewLine);
  118.                 }
  119.  
  120.                 Console.ReadKey();
  121.  
  122.             }
  123.             // a wrong index value,
  124.             //IndexOutOfRangeException will be thrown
  125.              
  126.             catch (IndexOutOfRangeException ex)
  127.             {
  128.                 Console.WriteLine(ex.Message);
  129.             }
  130.             catch (Exception ex)
  131.             {
  132.                 Console.WriteLine(ex.Message);
  133.              
  134.             }
  135.         }
  136.     }
  137. }
output
Tags

Comments

Just Born today.. intending to learn cyber security. please help.

Add new comment