Tutorial

  • This tutorial is divided into the following sections:

    Arrays in General
    Declaring Arrays
    Initializing Arrays
    Accessing Array Members
    Arrays are Objects
    Using foreach with Arrays

Arrays in General

  • C# arrays are zero indexed; that is, the array indexes start at zero.
  • Declare array after the type, but not the identified
    • Correct declaration of an array :
      • single dimension  int [] n ;
      • multi dimentsion :
    • incorrect int n[];
  • Size of an array
    • any size : int [] n;
    • fixed size int [] n;
      • n = new int [10];

Declaring an array doest not create an array, it has to be initialized.

Initializing an array:
  •  An array initializer is a sequence of expressions enclosed by curly braces and separated by commas.
  •  Array initializers are executed from left to right and can include method calls and complex expressions, as in the following example:
	int[ ] data = new int[4]{1, 2, 3 4};
Table 2. The System.Array Class Methods.
BinarySearch:   :This method searches a one-dimensional sorted Array for a value,
                 using a binary search algorithm.
Clear           : clears an array and sets an array to 0.
Clone           : copy of the Array.
Copy            : This method copies a section of one Array to another Array and performs 
                  type casting and boxing as required.
CopyTo          : This method copies all the elements of the current one-dimensional Array to the specified
                  one-dimensional Array starting at the specified destination Array index.
CreateInstance  : This method initializes a new instance of the Array class.
GetEnumerator   : This method returns an IEnumerator for the Array.
GetLength       : This method returns the number of items in an Array.
GetLowerBound   : This method returns the lower bound of an Array.
GetUpperBound   : This method returns the upper bound of an Array.
GetValue        : This method returns the value of the specified item in an Array.
IndexOf         : This method returns the index of the first occurrence of a value in a 
                  one-dimensional Array or in a portion of the Array.
Initialize      : This method initializes every item of the value-type Array by calling
                  the default constructor of the value type.
LastIndexOf     : This method returns the index of the last occurrence of a value in a 
                  one-dimensional Array or in a portion of the Array.
Reverse         : This method reverses the order of the items in a one-dimensional 
                  Array or in a portion of the Array.
SetValue        : This method sets the specified items in the current Array to the specified value.
Sort            : This method sorts the items in one-dimensional Array objects.
 

          Array Class Properties
    
    IsFixedSize    Return a value indicating if an array has a fixed size or not.
    IsReadOnly     Returns a value indicating if an array is read-only or not.
    IsSynchronized Returns a value indicating if access to an array is thread-safe or not.
    Length         Returns the total number of items in all the dimensions of an array.
    Rank           Returns the number of dimensions of an array.
    SyncRoot       Returns an object that can be used to synchronize access to the array.
    
Array Length::

using System;
//csc test_array_1.cs

class test
{
static void Main()
{
int[] data = new int[]{4,6,3,8,9,3};
Console.WriteLine(("Length of Array ; " + data.Length));
}
}

 

Example 2

    using System;
//csc test_array_3.cs
namespace same
{
 public class ucap
 {
  public static string Ucase(string str)
  {
  str = str.ToLower();
  string strempty = "";
  char[] chEmpty = new char[]{' '};
  foreach (string search in str.Split(chEmpty))
  {
  strempty += char.ToUpper(search[0]);
  strempty += (search.Substring(1, search.Length - 1) + ' ');
  }
 return strempty;
 }
}


class test
{
  static void Main()
  {
   Console.Write("Write a sentence :");
   string str = Console.ReadLine();
   string con_str = ucap.Ucase(str);
  Console.WriteLine("Line Entered :" + str);
  Console.WriteLine("Line Changed :" + con_str);
  }
}
}
    

 

Example: new line as is

using System;
//csc test_array_4.cs
namespace same
{
public class ucap
{
public static string Ucase(string str)
{
str = str.ToLower();
string strempty = "";
char[] chEmpty = new char[]{' '};
foreach (string search in str.Split(chEmpty))
{
//strempty += char.ToUpper(search[0]);

strempty = (search.Substring(0, search.Length ) + ' ');
Console.WriteLine(strempty);
}
return strempty;
}
}


class test
{
static void Main()
{

Console.Write("Write a sentence :");
string str = Console.ReadLine();
string con_str = ucap.Ucase(str);
Console.WriteLine("Line Entered :" + str);

}
}
}

sorting data

    using System;
//csc test_array_5.cs
namespace arraysort
{
class sortarray
{
public static void  sorting(string[] word)
{
Console.WriteLine("Sorted Array:");
Array.Sort(word);
foreach (string str in word)
{
      Console.WriteLine(str);
}
//return word;
}

}

class test
{
static void Main(string[] str)
{
Console.Write("Now hit enter :");
// to hold the curosr for enter key press
Console.ReadLine();

}
}
//
}
   
sorting data, my confusion is here, I did not call any function or anything in test_array_5.cs, still does the job

Note the difference

using System;
//csc test_array_5b.cs
namespace arraysort
{
class sortarray
{
public sortarray(){ Console.WriteLine("Constructor evoked");}
~sortarray(){ Console.WriteLine("Destructor evoked"); }
public static void sorting(string[] word)
{
Console.WriteLine("Sorted Array:");
Array.Sort(word);
foreach (string str in word)
{
Console.WriteLine(str);
}
//return word;
}

}

class test
{
static void Main(string[] str)
{
Console.Write("Now hit enter :");
// to hold the curosr for enter key press
Console.ReadLine();
sortarray.sorting(str);
}
}
//
}
 

 
Now note in this revision, constructor was summoned with the object.

using System;
//csc test_array_5d.cs
namespace arraysort
{
class sortarray
{
public sortarray(){ Console.WriteLine("Constructor evoked");}
~sortarray(){ Console.WriteLine("Destructor evoked"); }
public static void sorting(string[] word)
{
Console.WriteLine("Sorted Array:");
Array.Sort(word);
foreach (string str in word)
{
Console.WriteLine(str);
}
//return word;
}

}

class test
{
static void Main(string[] str)
{
Console.Write("Now hit enter :");
// to hold the curosr for enter key press
sortarray sa = new sortarray();
Console.ReadLine();
sortarray.sorting(str);
}
}
//
}