Difference between revisions of "Intro Week 5"
(→Struct) |
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >") |
||
Line 5: | Line 5: | ||
enum | enum | ||
:enumerations allow us to define data that can take a fixed set of results. | :enumerations allow us to define data that can take a fixed set of results. | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
enum direction {north, east, south, west}; | enum direction {north, east, south, west}; | ||
</csharp> | </csharp> | ||
− | <csharp> | + | <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}; | ||
</csharp> | </csharp> | ||
Line 14: | Line 14: | ||
Or we can use months | Or we can use months | ||
− | <csharp>//another more useful example | + | <syntaxhighlight lang="csharp" line="1" >//another more useful example |
// forced sequence to start | // forced sequence to start | ||
// from 1 instead of 0 (default) | // from 1 instead of 0 (default) | ||
Line 25: | Line 25: | ||
To write out the name of month we can use the enumerator | To write out the name of month we can use the enumerator | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
Console.WriteLine(Months.February); | Console.WriteLine(Months.February); | ||
Console.WriteLine( (Months)3 ); | Console.WriteLine( (Months)3 ); | ||
Line 37: | Line 37: | ||
A group of strings | A group of strings | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
string[] names; //delcare an array of strings | string[] names; //delcare an array of strings | ||
names = new string[4]; //initialize the array | names = new string[4]; //initialize the array | ||
Line 52: | Line 52: | ||
A for each loop requires and iterator.. more on this iterator thing later... | A for each loop requires and iterator.. more on this iterator thing later... | ||
− | <csharp>string[] names = {"Cheryl", "Joe", "Matt", "Robert"}; | + | <syntaxhighlight lang="csharp" line="1" >string[] names = {"Cheryl", "Joe", "Matt", "Robert"}; |
foreach (string person in names) | foreach (string person in names) | ||
{ | { |
Revision as of 18:28, 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.
<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