Intro Week 5
esse quam videri
Contents
Enum
More complex variable types but very useful ways of defining data.
enum
- enumerations allow us to define data that can take a fixed set of results.
enum direction {north, east, south, west};
enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25};
Or we can use months
//another more useful example
// forced sequence to start
// from 1 instead of 0 (default)
enum Months
{
January = 1, February , March, April ,
May , June , July , August , Sept , Oct , Nov , Dec
}
To write out the name of month we can use the enumerator
Console.WriteLine(Months.February);
Console.WriteLine( (Months)3 );
String Methods
Array
Groups of that same type of variable
A group of strings
string[] names; //delcare an array of strings
names = new string[4]; //initialize the array
//set the values in the array
names[0] = "Cheryl";
names[1] = "Joe";
names[2] = "Matt";
names[3] = "Robert";
for each
A for each loop requires and iterator.. more on this iterator thing later...
string[] names = {"Cheryl", "Joe", "Matt", "Robert"};
foreach (string person in names)
{
Console.Write (" " + person);
}
produces
Cheryl Joe Matt Robert
Homework
Write a program that averages 10 numbers