Join Alexander Zanfir for an in-depth discussion in this video Working with enumerators, part of Learning C#.
- [Instructor] We just received the feature request from the school board to support multiple schools. To begin with, they want to support only three schools for a trial period. So let's create a property in the student that will allow us to set the school. So let's go down to our Student class, and we'll add the field there. So we'll add a public string, and I'll call it School, and let's go back up to our program, and we'll ask the user what school the student goes to.
We'll do that right after the grade and we'll simply ask for the school name, and that should be it. Let's give this a try, and it seems to be working with no issues. Now, after submitting this update to the school board, several reports came in from the database administrator, that it is taking far too long to export the students to a database, and it often fails.
They examined the application and have determined that the school property is the culprit. Oftentimes, users might mistype the school, and when they try to export a student from a specific school, that student with the mistyped school fails. Also, since the property is a string, and the database program on the administrator's end is comparing school names letter by letter, and often the school names are long. The process is expensive. So that's one thing to keep in mind is that if we're comparing hundreds or thousands of strings against each other, and they each have several dozen characters, that's a very expensive process.
So your team comes up with the idea to create an index of schools. School A will have the index of one, school B will be two, and school C will be three, and so on, and instead of a string, we just store an integer for the school index. This way there is no way to misspell the school, and comparing a single integer is much faster. So first, let's modify the question, and then we'll modify the field after. So instead of Console.Ask on line 24, we'll change that to AskInt, and now let's go down to our Student class, and we'll change line 82 from a string to an int.
But we also need to supply the index inside the actual question. So let's go back up to line 24, and we'll give them several options. So I'll use parentheses inside the string, and specify, type corresponding number, and then I'd like to specify, let's start with zero for, and we'll come up with a school name. I'll make it Hogwarts High, and the next school, with index one, Harvard, and finally two, with MIT, and I'd like each of these to be on a new line, so I'll use a set of characters that will give us a new line that we can put anywhere in our string.
So after we ask the question, I'll create a new line. Then, after the first option, another new line, and so on, and we'll end with a new line as well, so that they can answer on a new line, and actually, I'll run this in debug mode so that we can see what the value is once it's set. So let's first get rid of the breakpoint we have on line 18, and then add a new one below our school question, so on line 26, and now let's run with F5, and then let's hover over school, and we can see the value is set to two, so it's working.
This looks and works great. We also received another feature update to export the students database for each school. Each school database is different, and so we will need to create a separate export function for each call that our database programmer can plug into. So to keep this clean, let's go down to the bottom of our Program class, and let's create a new function called Exports, and it won't return anything since it's just exporting something, and inside here, let's begin by writing a foreach loop, and that will go through all of our students inside our students collection.
Now, our students collection is just a local list, so let's move it out of our Main function and into the class. That way we can access it anywhere in the class, and I'll get rid of the var keyword, and then I'll specify the type explicitly, which will be a list of students, and I'll change that to static as well, since we need to access that inside static functions. All right, now let's go back down to our export function, and in here we'll have to execute a different export function, depending on what school the student belongs to.
So let's use a switch, and we'll switch on student.School, and we'll have the first case, zero, and we'll simply do a Console.WriteLine("Exporting to Hogwarts"); and we'll add the break, and then we'll add case one, and this time we'll export to Harvard.
Finally, case two, we'll export to MIT. Okay, now the next thing we need to do before we try it is simply to call Exports inside our Main function. So after the first foreach that displays the student name and grade, let's call Exports. Now let's give it a try. I'll select Harvard, and again, so here we're getting the Exporting to Harvard message, so it's working.
So this seems to be working fine, but it's a bit hard to read since we are dealing with just numbers and the index, and inside the switch. Instead of using an int, we can use something called an enumerator, or an enum for short. Let's take a look at that. So I'll go to the top of our file, above our class Program, and create an enum. I'll call it School, and I'll set Hogwarts to zero, Harvard to one, and MIT to two, and now let's go ahead and use our School enum, and we'll go down to our Student class, and instead of specifying an int for the type, we'll use a School enum type.
Now we're getting a few errors, so of course we'll have to modify the cases in our switch statement on line 75. So we'll start with case zero, and change that to School.Hogwarts, and then case one, change that to School.Harvard. Case two, School.MIT. So that reads a lot better since it's less ambiguous than the school index we had before.
Now, if you can see, we still have one more error we need to deal with, so let's scroll up, and on line 31, we're getting an error that we're trying to implicitly convert an int to our School enum, and even though the enum has an underlying number, it can't directly convert it unless we specify to do so explicitly with a cast. So let's add that in front. So to use a cast, we'll just append parentheses, and then the type we want to cast to, which is School, and so that will take the integer from our AskInt, and cast it over, or convert it into a School enum, and let's try running our program one more time, and we can see everything's working.
We selected two for the school, which is MIT, and we're getting Exporting to MIT. One last thing we can look at in regards to enums is that they come with an index, by default, that starts at zero, so we don't actually have to give them the corresponding numbers, as I did here. By default, the first will be zero, the second one, and so on. So this should behave exactly the same with less code, and that's always good, and it continues to work.
Explore variables and data types; controlling program flow with conditions and loops; and building functions. Learn how to implement object-oriented programming such as encapsulation and inheritance in C#, and find out how to debug your code. Alexander then explores advanced concepts such as enumerators, ref parameters, interfaces, events, and abstract classes. In the last two chapters, he covers the "top" features introduced in the last several versions of C#, including lambda expressions and string interpolation.
- C# variables and data types
- Switch statements
- Object-oriented programming: encapsulation, properties, and inheritance
- Debugging C# code
- Advanced C# concepts
- Top new features