Intro Week 5
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.
<syntaxhighlight lang="csharp" line="1" > enum direction {north, east, south, west}; </csharp> <syntaxhighlight lang="csharp" line="1" > enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25}; </csharp>
Or we can use months
<syntaxhighlight lang="csharp" line="1" >//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 }</csharp>
To write out the name of month we can use the enumerator
<syntaxhighlight lang="csharp" line="1" >
Console.WriteLine(Months.February);
Console.WriteLine( (Months)3 );
</csharp>
String Methods
Array
Groups of that same type of variable
A group of strings
<syntaxhighlight lang="csharp" line="1" > 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";
</csharp>
for each
A for each loop requires and iterator.. more on this iterator thing later...
<syntaxhighlight lang="csharp" line="1" >string[] names = {"Cheryl", "Joe", "Matt", "Robert"}; foreach (string person in names) {
Console.Write (" " + person);
}</csharp>
produces
Cheryl Joe Matt Robert
Homework
Write a program that averages 10 numbers