Difference between revisions of "Intro Week 5"
esse quam videri
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >") |
m (Text replacement - "</csharp>" to "</syntaxhighlight>") |
||
Line 7: | Line 7: | ||
<syntaxhighlight lang="csharp" line="1" > | <syntaxhighlight lang="csharp" line="1" > | ||
enum direction {north, east, south, west}; | enum direction {north, east, south, west}; | ||
− | </ | + | </syntaxhighlight> |
<syntaxhighlight lang="csharp" line="1" > | <syntaxhighlight lang="csharp" line="1" > | ||
enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25}; | enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25}; | ||
− | </ | + | </syntaxhighlight> |
Or we can use months | Or we can use months | ||
Line 21: | Line 21: | ||
January = 1, February , March, April , | January = 1, February , March, April , | ||
May , June , July , August , Sept , Oct , Nov , Dec | May , June , July , August , Sept , Oct , Nov , Dec | ||
− | }</ | + | }</syntaxhighlight> |
Line 28: | Line 28: | ||
Console.WriteLine(Months.February); | Console.WriteLine(Months.February); | ||
Console.WriteLine( (Months)3 ); | Console.WriteLine( (Months)3 ); | ||
− | </ | + | </syntaxhighlight> |
==String Methods== | ==String Methods== | ||
Line 47: | Line 47: | ||
names[3] = "Robert"; | names[3] = "Robert"; | ||
− | </ | + | </syntaxhighlight> |
==for each== | ==for each== | ||
Line 56: | Line 56: | ||
{ | { | ||
Console.Write (" " + person); | Console.Write (" " + person); | ||
− | }</ | + | }</syntaxhighlight> |
produces<br> | produces<br> |
Revision as of 18:29, 25 January 2016
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.
1 enum direction {north, east, south, west};
1 enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25};
Or we can use months
1 //another more useful example
2 // forced sequence to start
3 // from 1 instead of 0 (default)
4 enum Months
5 {
6 January = 1, February , March, April ,
7 May , June , July , August , Sept , Oct , Nov , Dec
8 }
To write out the name of month we can use the enumerator
1 Console.WriteLine(Months.February);
2 Console.WriteLine( (Months)3 );
String Methods
Array
Groups of that same type of variable
A group of strings
1 string[] names; //delcare an array of strings
2 names = new string[4]; //initialize the array
3
4 //set the values in the array
5 names[0] = "Cheryl";
6 names[1] = "Joe";
7 names[2] = "Matt";
8 names[3] = "Robert";
for each
A for each loop requires and iterator.. more on this iterator thing later...
1 string[] names = {"Cheryl", "Joe", "Matt", "Robert"};
2 foreach (string person in names)
3 {
4 Console.Write (" " + person);
5 }
produces
Cheryl Joe Matt Robert
Homework
Write a program that averages 10 numbers