Arrays of Objects
using System;
class Employee
{
public Employee(string name, float billingRate)
{
this.name = name;
this.billingRate = billingRate;
}
public float CalculateCharge(float hours)
{
return(hours * billingRate);
}
public string TypeName()
{
return("Employee");
}
private string name;
protected float billingRate;
}
class Manager: Employee
{
public Manager(string name, float billingRate) :
base(name, billingRate)
{
}
public new float CalculateCharge(float hours)
{
if (hours < 1.0F)
hours = 1.0F; // minimum charge.
return(hours * billingRate);
}
public new string TypeName()
{
return("Civil Employee");
}
}
class MainClass
{
public static void Main()
{
// create an array of Employees
Employee[] earray = new Employee[2];
earray[0] = new Employee("A", 15.50F);
earray[1] = new Manager("B", 40F);
Console.WriteLine("{0} charge = {1}",
earray[0].TypeName(),
earray[0].CalculateCharge(2F));
Console.WriteLine("{0} charge = {1}",
earray[1].TypeName(),
earray[1].CalculateCharge(0.75F));
}
}
Employee charge = 31
Employee charge = 30
A two dimensional array of objects
using System;
public class Employee
{
public string name;
public int no;
public Employee(string name,int no)
{
this.name = name;
this.no = no;
}
}
class MainClass
{
public static void Main()
{
Employee[,,] empArray = new Employee[10, 5, 3];
empArray[1, 3, 2] = new Employee("S", 3);
empArray[4, 1, 2] = new Employee("A", 9);
Console.WriteLine("empArray.Rank (number of dimensions) = " + empArray.Rank);
Console.WriteLine("empArray.Length (number of elements) = " + empArray.Length);
for (int x = 0; x < empArray.GetLength(0); x++)
{
for (int y = 0; y < empArray.GetLength(1); y++)
{
for (int z = 0; z < empArray.GetLength(2); z++)
{
if (empArray[x, y, z] != null)
{
Console.WriteLine("empArray[" + x + ", " + y + ", " + z +"].name = " + empArray[x, y, z].name);
Console.WriteLine("empArray[" + x + ", " + y + ", " + z +"].no = " + empArray[x, y, z].no);
}
}
}
}
}
}
empArray.Rank (number of dimensions) = 3
empArray.Length (number of elements) = 150
empArray[1, 3, 2].name = S
empArray[1, 3, 2].no = 3
empArray[4, 1, 2].name = A
empArray[4, 1, 2].no = 9
string arrays
using System;
class MainClass
{
public static void Main()
{
string[] stringArray = new string[2];
Console.WriteLine("stringArray[0] = " + stringArray[0]);
stringArray[0] = "Hello";
stringArray[1] = "World";
foreach (string myString in stringArray)
{
Console.WriteLine("myString = " + myString);
}
}
}
stringArray[0] =
myString = Hello
myString = World
Use object to create a generic array.
using System;
class MainClass {
public static void Main() {
object[] ga = new object[10];
// store ints
for(int i=0; i < 3; i++)
ga[i] = i;
// store doubles
for(int i=3; i < 6; i++)
ga[i] = (double) i / 2;
// store two strings, a bool, and a char
ga[6] = "String";
ga[7] = true;
ga[8] = "X";
ga[9] = "asdf";
for(int i = 0; i < ga.Length; i++)
Console.WriteLine("ga[" + i + "]: " + ga[i] + " ");
}
}
ga[0]: 0
ga[1]: 1
ga[2]: 2
ga[3]: 1.5
ga[4]: 2
ga[5]: 2.5
ga[6]: String
ga[7]: True
ga[8]: X
ga[9]: asdf
Use the Sort() method to sort the elements in a char array
using System;
class MainClass
{
public static void Main()
{
char[] charArray = {"w", "e", "l", "c", "o", "m", "e"};
Array.Sort(charArray); // sort the elements
Console.WriteLine("Sorted charArray:");
for (int i = 0; i < charArray.Length; i++)
{
Console.WriteLine("charArray[" + i + "] = " +
charArray[i]);
}
}
}
Sorted charArray:
charArray[0] = c
charArray[1] = e
charArray[2] = e
charArray[3] = l
charArray[4] = m
charArray[5] = o
charArray[6] = w