Enumerations

 An enumeration is a collection of constants. That means you can create your own set of named constants by using enumerations.
 Each constant will have a name with an integer value.

 Syntax for Enumeration Declaration:
public enum enumname
{
Constant1 = value1, Constant2 = value2, Constant3 = value3
}
 Syntax for usage
Enumname.constantname

Enumeration using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumerationDemo
{
public enum Months
{
January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, November = 11, December = 12;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Months.March);
Console.WriteLine((int)Months.March);
Console.Read();
}
}
}
enam

Arrays

 An array is the collection of similar type of values.
 Each value in the array is to be called as an element.
 The total no. of array elements is called as “Array size”.

Implementation of Arrays:

 Single Dimensional Arrays:
 Array declaration:
 Without initialization:

datatype[] arrayname = new datatype[size];
 With initialization:
datatype[] arrayname = {val1,val2,val3,…..};
 Accessing the elements:
arrayname[index]

 Double Dimensional Arrays:
 Array declaration:
 Without initialization:
datatype[,] arrayname = new datatype[rows size,columns size];
 With initialization:
datatype[,] arrayname = {{val1,val2,…}, {val1,val2,…},…};
 Accessing the elements:
arrayname[row index,column index]

 Multi Dimensional Arrays:
Same as “Double” dimensional arrays, but increase the no. of dimensions.

Demo on Single Dim Arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayDemo
{
//Demo on Single-Dim Array.
class Program
{
static void Main(string[] args)
{
//read the no. of students
int n;
Console.Write("Enter no. of students: ");
n = Convert.ToInt32(Console.ReadLine());
//check n value whether it is greater than 0 or not.
if (n > 0)
{
//declare the arrays
string[] Names = new string[n];
int[] Marks = new int[n];
string[] Result = new string[n];
//read student names
Console.WriteLine("\nEnter " + n + " students names:");
for (int i = 0; i < n; i++)
{
Console.Write((i + 1) + ": ");
Names[i] = Console.ReadLine();
}
//read student marks
Console.WriteLine("\nEnter " + n + " students marks:");
for (int i = 0; i < n; i++)
{
Console.Write((i + 1) + ": ");
Marks[i] = Convert.ToInt32(Console.ReadLine());
}
//calculate results
for (int i = 0; i < n; i++)
{
if (Marks[i] >= 0 && Marks[i] <= 100)
{
if (Marks[i] >= 80)
Result[i] = "Distinction";
else if (Marks[i] >= 60)
Result[i] = "First Class";
else if (Marks[i] >= 50)
Result[i] = "Second Class";
else if (Marks[i]>= 35)
Result[i] = "Third Class";
else
Result[i] = "Fail";
}
else
Result[i] = "Invalid";
}
//display the student names and marks
Console.WriteLine("\n\nStudent Details:");
for (int i = 0; i < n; i++)
Console.WriteLine((i + 1) + ". " + Names[i] + " - " + Marks[i] + " - " + Result[i]);
}
else
Console.WriteLine("N value can't be zero.");
Console.Read();
}
}
}
enam

Demo on Multi Dim Arrays

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MultiDimArrays
{
//Demo on Multi-Dimensional Arrays
class Program
{
static void Main(string[] args)
{
//Single dimensional arrays
int[] x = { 10, 20, 30, 40};
Console.WriteLine("Single dimensional array:");
for (int i = 0; i < x.Length; i++)
Console.Write(x[i] + ", ");
//Double dimensional arrays
int[,] y = { {10, 20}, {30, 40}, {50, 60} };
Console.WriteLine("\n\nDouble dimensional array:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j <2; j++)
Console.Write(y[i, j] + " ");
Console.WriteLine();
}
//Multi dimensional arrays
int[, ,] z = { { { 5, 10 }, { 15, 20 } }, { { 25, 30 }, { 35, 40 } }, { { 45, 50 }, { 55, 60 } } };
Console.WriteLine("\nMulti dimensional array:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
Console.Write(z[i, j, k] + " ");
Console.WriteLine();
}
Console.WriteLine();
}
Console.Read();
}
}
}
enam

“foreach” Loop

 One of the most common usages of the “for” loop is to iterate through a collection of values (array).
 C# offers a simplified and easier syntax of for loop called “foreach loop”, designed only for such kind of array iterations.
 Syntax:
foreach (datatype variable in arrayname)
{
-----------;
-----------;
}
 In the above syntax, the loop will be executed once for each value in the array. For every iteration, the values of the array will be assigned to the variable.
 For example, you take the following for loop.
int[] nums = { 10, 20, 30};
for (int i = 0;i < nums.Length; i++)
{
Console.WriteLine(nums[i]));
}
 You can re-write the above example with “foreach” syntax as follows:
int[] nums = { 10, 20, 30};
foreach (int n in nums)
{
Console.WriteLine(n);
}
Note: The “arrayname.Length” property gets the size of the array. We discuss about the “Array” class in future.