Lesson # 6. Theory
- An enumeration type, or enum, is a structure that enables you to create a variable with a fixed set of possible values. The most common example is to use an enum to define the day of the week. There are only seven possible values for days of the week, and you can be reasonably certain that these values will never change.
- A best practice would be to define your enum directly within a namespace so that all classes in that namespace will have access to it, if needed. You can also nest your enums within classes or structs.
- By default enum values start at
0
and each successive member is increased by a value of 1. - To create an enum, you declare it in your code file with the following syntax, which demonstrates creating an enum called
Day
, that contains the days of the week: - By default enum values start at 0 and each successive member is increased by a value of 1. As a result, the previous enum ‘Day’ would contain the values:
Creating and Using Enums
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; |
Sunday = 0 Monday = 1 Tuesday = 2 etc.
enum Day { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; |
Using an Enum
// Set an enum variable by name. Day favoriteDay = Day.Friday; // Set an enum variable by value. Day favoriteDay = (Day)4; |
enum DayOfTheWeek_v3 : short { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } |
Console.WriteLine((int)DayOfTheWeek.Tuesday);
Sequences
To have sequences in your code two directives must be incuded:
using System.Collections.Generic; using System.Linq; |
Example: Generates a sequence of integral numbers within a specified range:
Range (int start, int count); |
start
— The value of the first integer in the sequence.count
— The number of sequential integers to generate.
IEnumerable<int> seq; // definition seq = System.Linq.Enumerable.Range(3, 10); foreach (var x in seq) Console.Write($"{x} "); // 3 4 5 6 7 8 9 10 11 12 |
Example: Generates a sequence of integers from 1 to 10 and then select their squares:
IEnumerable<int> squares; squares=Enumerable.Range(1, 10).Select(x => x * x); foreach (int num in squares) { Console.WriteLine(num); } |
Labs and tasks
To do: Request user to input a number — a mark (1
, 2
, 3
, 4
, 5
). Check the inputted number to print out the characteristic of the mark (very_bad — 1, bad — 2, satisfactory — 3, good — 4, excellent — 5). And also output the mark for the characteristic of satisfactory.
Note: Create a function CheckMark
with o switch
statement in it to check the mark.
[Solution and Project name: Lesson_6Lab1
, file name L6Lab1.cs
]
The resulting examples:
Please enter a number 2 The characteristic for 2 is: bad And also the characteristic satisfactory is for: 3 mark
✍ Algorithm:
- Create Console Application with the name
Lesson_6Lab1
. - In the Solution Explorer window find a file
Program.cs
and rename it intoL6Lab1.cs
. - In order not to print always the class
Console
, we can include it right in the beginning of the editor window: - After the opening brace for the class
Program
and just before theMain()
method, enter the following code to create an enum calledMarks
, that represents characteristics of the marks: - Inside
Main()
add the following code. - This code shows the use of enumerations in two aspects. First, you are using Intellisense when you type in
Marks.satisfactory
. Second, the internal representation of these enumeration values are numeric so you can convert them toint
data types and display them as such. - Run the program by pressing
CTRL+F5
to see the output. - Under this code request the user to input the number (
1
—5
): - Create a function with the name
CheckMark
and two parameters:int
parametermark
to accept the mark andref
parameter with a namecharacteristic
of the type of the enumMarks
. - In the function create the switch statement to check an inputted number and to assign the characteristic result to the
characteristic
parameter: - In the
main
function initialize the variables to use them as the parameters for the function: - After, call the function and output the results:
- Run the program by pressing
CTRL+F5
to see the output.
... Console; ...System.
...
Marks { very_bad, bad, satisfactory, good, excellent, noSuchMark };
...
... satisfactory; Console.WriteLine($"And also the characteristic satisfactory is for: {x} mark"); ...x = ( )Marks.
... WriteLine("Please enter a number"); mark = .Parse(ReadLine()); ...
static CheckMark( mark, ref Marks characteristic) { ... }
switch (mark) { case 1: characteristic = Marks.very_bad; break; case 2: characteristic = Marks.bad; break; case 3: characteristic = Marks.satisfactory; break; case 4: characteristic = Marks.good; break; case 5: characteristic = Marks.excellent; break; default: WriteLine("Invalid selection. Please select 1, 2, 3, 4 or 5."); characteristic = Marks.noSuchMark; break; }
... WriteLine("Please enter a number"); // it was already here mark = .Parse(ReadLine()); // it was already here Marks characteristic = Marks.very_bad; // it must be assigned some value, no matter which one
... CheckMark(mark, ref characteristic); WriteLine($"The characteristic for {mark} is: {characteristic}");
🎦
To do: Create Console Application with the name Lesson_6Task1
. Remove the main
function from the project. Copy the code from the text-file, and insert this code to the main class (class Program
) of the created project. Follow all the tags TODO
and make the tasks written there. Make sure the project works properly.
The resulting example:
Please enter the day of the week: 2 Tuesday
Please enter the day of the week: 9 Необработанное исключение: System.ArgumentOutOfRangeException: day must be > 0 and <8; day=9
[Solution and Project name: Lesson_6Task1
, file name L6Task1.cs
]
Sequences
To do: Create a sequence of N
integral numbers starting with a
. a
and N
are entered natural numbers.
The resulting example:
Please, enter two natural numbers N and a: 2 5 The result: 2 3 4 5 6
[Solution and Project name: Lesson_6Task2
, file name L6Task2.cs
]
To do: Create a sequence of 9
integral numbers starting with 1
with a step=2
.
Note: to have the step you can use lambda expression ... .Select((a,b)=> ...);
. Think of what it can be there, instead of ...
.
The resulting example:
The result: 1 3 5 7 9 11 13 15 17
[Solution and Project name: Lesson_6Task3
, file name L6Task3.cs
]