Learning How To Program - Console TODO List in C#
February 27, 2023
• 2,136 views
Learninglearn to codeeducationprogramminghow tohow to codehow to programfree webinarprogramming livelive programmingcoding livecoding live streambeginner codingcoding tutorialprogramming for beginnerscoding for beginnerssoftware engineeringsoftware engineerhow to build an appcoding project ideascomputer sciencecoding project ideas for beginnersasmr programmingasmr codinglive codingcodingcoding livestreamfintechleetcode
I'm Nick Cosentino, and I'm a Principal Engineering Manager at Microsoft. I believe that programming is one of the most important skills you can have access to, and I'm here to help you get started. We'll be using C# but you should be able to apply these concepts to any language.
In our last live streaming session (https://youtube.com/live/cuyn7Vl5bjY), we built a simple calculator together! In this session, we'll be building another simple console application that will let us add, remove, and...
View Transcript
going to send a message here in the chat for people that join after or if you're watching the recording. So, what that message says is really just that we're going to be building a to-do list in C. Today, we're going to be looking at if statements, type conversions, collections, and loops. And I think the other thing to mention as well is that I'm going to be using net fiddle. So, if you want, you don't have to code anything. You can just watch along. But if you do want to code, you can go right to net fiddle and code right in your browser. You don't need to download anything fancy and then that way you can actually jump right into coding and not have to worry about it. So with that said, um let me go ahead and get my screen switched over to net fiddle
and that way we can start coding. Awesome stuff. Okay, so int net fiddle right now I just have a very basic template program for C and what we're going to be doing is coding inside of this public static void main. So everything inside of here that we have this is where we're going to start to add our code. So for our to-do list, what I wanted to add in here is just a few things that I'm looking to accomplish today. I'm hoping we can get this done within an hour. um I don't foresee it taking the full hour and that way we can cover all of these like uh functional criteria that we should have in a really simple to-do list that would run in the console. Okay, so when we think about a to-do list, some really simple functionality that we want to have
is going to be to add things, remove things, and essentially being able to view the contents of that list. And then the other thing that I added in here just because this is a console application, we're going to want to make sure that we have a way to exit gracefully. So if we were building out a fullyfledged app or something or a website, we wouldn't have to worry about this. But for the time constraints that we have today, this is just going to be something that runs right in a console window. And for those of you that aren't familiar, when I say console window, I'm just referring to, you can actually see it in net fiddle. this space here at the bottom where it says hello world. This is essentially a console window. All right, so let's get started. And the first thing that I
want us to be able to do is really we're just going to write to the console saying that we are in our to-do list. And that way when we go to run this or if you're trying it out, you also know that when you press run and it's going that it's actually at least started. So, I'm just going to put console right line. And then I'm going to add a string in here as denoted by the double quotation marks. Um, welcome welcome to the to-do list. And you'll see if I just press run now, we have this printed in our console window. So, so far so good with this is very very basic. And we want to start looking at the different things that we need to implement here. So, what I was considering when putting this together is that I want us to be
able to basically have a loop that will allow us to input um at least a number to pick what functionality we want to do. And that could be one of the things that we have listed here and then based on the the selection that we make the to-do list program will perform the operation for us. So for example, if we made this option zero, if someone entered zero as the option, then the to-do list program would just exit. If instead this was say this is option one that I have highlighted here, and then someone said, let me go execute option one, then of course our to-do list program would let us input something to add to our to-do list. Okay, so let's go ahead and start doing that. In order to make that work, what we're going to want is a um is a while
loop that will structure all of our program inside of. So I'm just going to put while true here. And really what this code does is that indefinitely because this part here while this expression whatever you put inside the parenthesis while that's true the loop will continue to execute. And because we have the word true in here, this expression is always true. So this is basically an infinite loop. So inside of our infinite loop, I think it would be really valuable to be able to print out what options that people are able to do in our to-do list. So that's going to be more console writing. [laughter] So it's a little bit boring to start with. I'm just copying and pasting this line right here. Um, here are the options you can select. And then what I'm going to do is basically print a line to
the console that will allow us to um see which basically selections we can make. And then what we're going to do after that is take the user's input. We're going to convert that from a string because when you type into the console that's going to be a string. We're going to convert that string into a number, match it up with one of these and then perform that operation for the user. So, as I said, a little bit more console writing, but let's structure it like this. So, option zero is going to be exit. And I'm just going to copy and paste this a few more times. So, if I were to go run this right now, basically this would just blast the console with a ton of text for forever. So, please, if you're following along and you're coding this right now, please don't press
run right now because it won't be a great time. It will literally just keep running for forever. So, we want to change this up a little bit. Let's make it so that um we'll do option one is going to be to view items. We'll make it. So, option two is going to be add to to-do list. And then we'll have option three is going to be remove from. Okay, cool. So, again, don't go run this right now just because we have this infinite loop around the whole thing. But really, what this code is trying to do so far is that we're going to print out the options that the user is allowed to select. So now we actually have to get that input from the user. So instead of console right line, we can actually use the inverse of that which is console read line.
So let's go ahead and do that. And we need to store whatever the user is entering. We need to store that into a string variable so we can use that. So we'll call it string user input. I can't type properly apparently. Um and then we will do console readline. Oops, too many of those. So when we do this, what will happen is this will print to the console. We'll have all the options available. Then it will stop and it will wait for us to put something in and we can type that right down here in the bottom part. And then what we want to do is take that user input and then convert it to a number. And that's because we want to match that number with one of these numbers that's up here printed in our options. So in C and other strongly typeyped
languages, um you can see that I have a string type here for this variable that's called user input. And because the user input variable is typed with the string type, it can only take sequences of characters inside of it. That means that when we have numbers, so if you were to type the numbers 1 2 3 for example into the console, 1 2 3 will show up as a string of numbers together. And if you're unfamiliar with what that means, it's just that the type of the variable will not actually be the same as a number. It looks like a number, but it is still a string. So, we have to be able to convert from string to an integer number for us to be able to use it the way we'd like. Now, there is a way like you don't necessarily have to do
that. We could compare the string input for literally um a string um value of zero, a string value of one. But I want to demonstrate the type conversion here. So let's go ahead and we can have um we're going to use something called triparse and I'll explain how that works. But we want to take an integer and we can call try pararse on it just like this. And the first parameter that we put into that is going to be the string. So we want to pass in user input and then we want to output the result of the parsing. So we would say out int and then um it would be option let's call it um option id. So this is actually a variable declaration. Um if I recall correctly I actually think that net fiddle does not like us doing that. So let me
put it on the line above. Sorry for the confusion there. Um, I think for some reason net fiddle doesn't like you defining the variable inside here. It technically is totally valid C code. I'm just going to define the variable on the line above. And then we're going to output the result of the parsing into this. And then we need a semicolon at the end of that. Okay. So once we have the string being converted into an integer, technically what we want to do is double check that we actually were able to parse it because if I were to type the word zebra in here, the word zebra is not convertible to an integer. Of course, right? So when that happens, um, we want to make sure that we can handle that properly. And I'm just double- checking the chat because I wanted to be able
to answer questions along the way. So it says to clarify the string 1 2 3 is made of characters 1 2 and 3 as opposed to the numerical value of the integer 1 2 3. That's exactly right. So if I quickly were to say like int x= 1 and then I did string xx equals these two things are not the same [laughter] to the human eye and if you were to print it to the console it would look the same but they are not the same types. So thank you for pointing that out. Okay. So when we try to parse, we want to actually make sure that it succeeded. If not, then what we want to do is actually tell the user, hey, that's the wrong input. Let's try again. So this is where we can use an if statement. And actually to make it
a little bit more clear, I'm going to assign the result of the try parse into a boolean. So that's denoted by bool. And we'll call it um option option parse result and then I will write the if statement here. So if option parts result and then we'll do some action here but I did mention that I want to print something when the user has not put in a number. It's sort of the error case that we want to handle. So in order to get the opposite of option parse result, we can actually put an exclamation mark here and that would be just like saying this. Okay, so I just wanted to clarify that for shorthand we can put an exclamation mark here and it will invert this condition for us. So the exclamation mark used like this is called a not operator. So it would
be like saying if not option parse result then we'll do this. So option parse result will be false for us if parsing did not work. And that's great because now we can handle that error. So we can do a continue. The word continue. What this will do inside of a loop is actually bring us back to the top here. So we can say continue and then it will print out the options again. So before we do that, let's actually tell the user that they messed up. So more console printing. I know so far this has been mostly console printing. Um input was not an integer. Okay. So so far again just to quickly highlight we're printing out the options. We are asking the user for some input, parsing that string input into a number and making sure that we could successfully parse it. If not,
we go right back to the top and repeat the process. Okay. So, what I'm going to do just so that we can actually try this, I'm just putting a break statement at the end here. And that way when I press run, we're not ever going to be stuck in this infinite loop. We have a way out. We'll get rid of that after, but I'm going to go ahead and run it. So, welcome to the to-do list. Here are the options. Now, again, to try this out, I've said I'm going to try the word zebra here to show that we're not actually handling, sorry, that we're able to properly handle when we can't parse things. So, if I type in zebra, it says input was not an integer. And you can see that basically it restarted the whole loop. And that's because of this continue here.
Okay. And to prove that we can actually get a valid integer, I'm going to type in 1 2 3. Even though it's not a valid input, we're going to have to check for that case. But if we have a valid integer, it should just stop. And it does. And that's because we were able to hit this brake line. Okay, cool. So now we're able to actually get the input from the user and figure out what they would like to do. So we should actually try implementing each one of these so that we can take that user input and map it to the right action. So we can start with the most simple one because we basically have it right in front of us and that is when the input is zero. When the input is zero, we actually just want to stop. So let's go
check for that. If what did I call it? Option ID. So if option ID is zero, let's go ahead and exit. Um, in previous videos I have shown a couple of different ways to do this, but just to highlight, we have two options we can do. One of them is to break like I just had printed out here before. So you can break out of the loop. And what that will do is I'm just trying to show the whole loop on the screen at the same time. I can't really make that much smaller. Sorry. So if there was code just outside of the loop, this would keep running if we put break here. Okay. So if we put break, it will just leave the loop and keep running the rest of the program. If instead we put return here, it will actually leave the entire
function that we're inside of. So in our use case right now, it's not really going to matter. I'm just going to leave it as break. And then that way if we wanted, we could say at the end, oops, we could say, hey, thanks for using the cal or the to-do list. Right? So, we have a little exit message here. And that way, when we hit this break, you'll notice that we do actually go get to this. Let's go ahead and run that just to double check that it works as expected. So, if I press run and then I type in zero, now what we should hope to see is that we hit this condition and then we see this line printed. Awesome. That one's pretty easy. [laughter] Now, let's get on to the more interesting options that we have. So, when I have I'm watching
the chat, too. So, thanks for I I saw the message just got deleted. So, go ahead and uh rephrase or ask and I'll try to jump to answer. When we have Let's go look at um option one is going to be a little bit boring to start with. Let's make sure we can add stuff to the to-do list. I should also note here that there are if statements and something else called switch statements. And we'll just say that that's homework. So if you wanted, you could actually form what we're about to type here in a different way, but I'm just going to show you with if statements to keep it simple to start with. So let's go ahead and implement option number two, which is adding to our to-do list. Well, so far we don't even have a to-do list. We don't have anything that's
going to hold the items in our list. So, we actually have to go ahead and create a collection. And a collection, there's many different types of collections that we can use in C. And other programming languages have similar collections as well. For our use case, we actually want to have a collection that can change size. And that's going to be an important property when we're considering what type of collection to use. There's a really basic type of collection that's called an array. And traditionally, arrays are a fixed size. So for our use case, we're actually not going to use an array because we do plan to change the size. C has something that's called a list. Um, and conveniently it's [laughter] very simply named and actually serves our purpose pretty well here because we are able to add things to it and remove things from
it dynamically changing the size. So what I would like to do is before we get into our loop that takes the input from the user, I'm going to go ahead and declare for us a list of to-do list items. So that will look like this and we're taking a really simple approach here. We're just going to be using um strings for the type of the collection. So in C there's different ways that you can use collections. And when we think about lists, the way that the list functions, it doesn't care what types of things you have inside of it. So if you wanted a list of numbers or a list of strings, the list still functions the same way. But when we declare our variable like this where we have these angled brackets, we're saying that we want a list that contains specifically strings in
this case. Now it's also saying that I have to Will this autocomplete for me? No, I have to go get I have to go add something up here. So the collections that we're interested in, we need to actually get them from somewhere. And we'll get them from system.colctions.generic. So that's something that will take a little bit of practice to know where things are located. So if you didn't know that off the top of your head, that's totally okay. Generally, if you're programming inside of something like Visual Studio, which is where most C developers will write code, this kind of thing will be um almost automatically available to you. So, you don't really have to think about it. Let's scroll back. Oh, sorry, too far. But you can see now on line 19 that I was able to make a list of to-do items and I
actually assigned a new list over to this variable. So, at this point, we have a list, but we're not doing anything with it yet. So, I mentioned I wanted to go with option number two, right? Adding items to the to-do list. So, let's go ahead and do that. So, if option is two, this microphone's way too big, so as I'm typing, I can't see where my hands are. So, if the option ID is two, what we want to be able to do is take some user input to add the entry into our to-do list. Now, what's interesting this time versus when we wanted the other input is that our to-do list items are going to be strings. That means that we can take the user input and we actually don't have to do any fancy type conversion with it. So, let's go ahead and I'm
going to copy just so I don't make a typo here. I'm going to copy line 29. I'm going to put it inside here. And we're going to have it called um user in or let's say like um to-do. Naming stuff is hard. Um new [laughter] to-do list item. Okay. So, what we'll do is ask the user to input some text and we will assign that to a new string variable. From here, what we're going to do is add that string variable into our list. And we can just simply call add on our list. We can take this variable and pass it in right here. And at this point, that's really all we need to do to be able to add stuff to the list. It's really simple. And just to repeat it again, we're asking for user input. We're going to store that user input
into a string. Unlike before where we needed to actually do some integer conversion, we don't have to do that here. And then that string is just going to get added into our list. Awesome. So, while that works, the next thing you might say is, "Well, prove to me that it works." And the tricky part is going to be that I actually can't show you until we have the ability to print the items in the to-do list. So, we're going to have to scroll back up and we need to be able to do option number one here to be able to see the things that are inside of the list. So, we'll implement that part next. And I realize probably what we should do is print something right before this that says please enter please enter your to-do list item. So that would prompt the user
to like to actually suggest they need to type something and then and then press enter to submit it. Okay. So now we have to go do option number one which is going to be viewing that to-do list. So just a quick note, the reason I wrote switch statements here is that you'll notice as we go to populate this if statement chain, it's going to look a little bit messy. So um switch statements and there's other things that could help us clean this up a little bit. Um, yeah, great question in the chat. Um, is there any merit to validating the user didn't just hit enter without typing anything to uh to stop empty items being added to the list? Absolutely. Um, so let's make a note here. I just want to do a time check because if we have time at the end, we can
come back and do this. So todo. So just to to repeat what that point was. Basically, if we read the user input and someone just pressed enter on the console, do we want to actually add an empty to-do list item or do we want to handle that? Maybe tell them, hey, it can't be empty. Do we want to just ignore it and kind of sit there for them to add another one? Do we want to treat that as a way where we need to handle, hey, maybe the user accidentally said that they wanted to add something and we need to back out. So, there's different ways that we could handle and validate that. But that's a really good point. So, thank you for asking that in the chat. And if we have time, we'll come back and and pick how we want to approach that.
Okay. So, option number one to actually view things is going to look similar to what we have going on so far. We can do if I'll copy that if option is number one. And you'll notice that what I've done now is we have an if and now we have an else if. So what will happen is we're as the code is flowing through and checking it will say if option is number one do this code that's in here. But if it's not number one so that's the else. So else, it's not number one. If it's option two, then do the stuff in here. And that means we can actually have an else condition on the whole thing. And what we should be checking here is todo check if or I guess not check um tell user invalid option. So, if we don't have support for
whatever number the user entered, we should tell them so that they know and they can't just enter 1 2 3 like I did earlier and hope that something's going to happen. So for option number one to actually view what we have in the list, what we want to do is probably print out a little message that says something like your to todo list. Now once we print this out, we probably need to consider that we need to actually print the items in the to-do list. So, if I scroll back up a little bit, we talked about loops and the fact that I'm using this while true loop. There's another type of loop that we can go use on the to-do list items to print them out one at a time. And for that, we're going to use something that's called a for each loop. So
if I go here, I can actually type for each string in todo list items. And what will happen is that this is now a loop that will go through each item one at a time inside of to-do list items. And oops, I didn't name it properly. Sorry. for each string and we'll call it item in to-do list items. Let's go print it out. So everything that's inside of here is going to run for each and every item that's inside of this list. And all that we want to do is print them out. So more console writing of course and it will look something like that. So this is actually something I think we can go run and prove that if we do option number two, then we should be able to actually do option number one after and see that we have something inside of
our to-do list. So I'll go ahead and press one. I said one instead of run. Um, and then if I enter it one now, if I do that, we should see that it prints this, but there's going to be nothing inside of it because we haven't added anything. So, it says your to-do list and then nothing. And then it goes back to the top of the loop and prints the options again. So, that's a little bit hard to read, right? We might want to come back. Maybe we want to put a thing here that says todo um print better output when the list is empty, right? Because it says your to-do list and there was nothing inside of it. So, it's not really obvious. It just kind of prints out the instructions again. So, we can improve that if we have time. What we can
try now is option number two, which should let us enter something. So option number two, please enter your to-do list item and then press enter. So my to-do list item that I'm going to enter is implement option number three. Okay. So again, maybe we could have better user feedback there, right? Because do we know that it got added? It's not really obvious, but that means we can probably try option number one now and see if it's inside of the list. So your to-do, it says your to-do list items implement option number three. So that's cool. And then now it actually says here are the options you can select. So it printed out the one option inside of our list. That's thanks to this for each loop. And there's only one thing inside of it. And then it printed the instructions back again. So this is
actually working as expected so far. And what we're noticing, or at least in my opinion, what I'm noticing is that there's definitely some cleanup we could do to make sure that the output that we're seeing in the console is a little bit more clear as to what's happening. So I'm going to go ahead and press zero to exit. And tells us thanks for using it. Awesome. Okay. So option number three is going to be removing stuff. Now how do we want to do that? Let's go ahead and get this. I'm going to copy and paste it. But let's figure out how we actually want to remove things. Please enter the to-do list item to remove and then press enter. So, this is an interesting situation. When we were adding things, all that we had to do was type the text for the new to-do list
item that we wanted to add. Well, when we go to remove things, how should we do that? Should someone be forced to type in the exact same text from their to-do list item and make sure that it matches just so that we could remove it? Is there a better way? Maybe we could look at actually removing things by the index that they are in the to-do list. Perhaps that might be a little bit more clear. In this case, I don't actually think there's like a right and a wrong answer. And I think that we could make a decision about what we can try here. So, because I'm the one talking, I think I will probably opt for getting people to input the index of the to-do list item and then we can try to remove that index. An index in this case is going to
be a numeric representation. So, that will mean that we have to do the same thing as earlier or we get a string input and then we convert that to an integer and then we can try removing things. Okay, that should be pretty straightforward because we've seen some of these pieces of code already. However, something that I think we're going to want to improve is that we actually don't know the indices of each item in the to-do list. If you're familiar with C, we would know that whatever order we see the items printed in, they basically start at a zero index. And then we could just assume that. But maybe we could make that a little bit more obvious for our users. So what I might do is go back up to option number one first that actually does the printing. So again, there's no right
and wrong way to do this, but I would like to pretty it up just a little bit so that it's more clear as to what's happening. So, I'm going to take this console right line that prints out each item individually and I'm going to try to make it perhaps a little bit more clear as to what's happening. And I wanted to get the index. Well, the tricky thing about trying to get the index with a for each loop is that it's not really included. We'd have to basically count as we're going along using this for each loop. But there's probably a better way that we could do that. And that's instead of using this for each loop, we can actually just use a normal for loop. Now, when we're learning how to program, usually a for loop is one of the first types of loops
that we see. So, this is something that will apply into different languages, not just C. But the syntax that I'm about to write might look different in other languages. So when we're using a for loop, it's basically just defining how a number is going to count and then the conditions for when it should stop counting. So we can say for int i equals z. I is going to be less than. So this is telling us how long we want to loop for. And because it starts at zero, we have to keep that in mind. We want to make sure that we're just looping for as many items as we have here. So, we can loop until we're just at the count. And then we want to say I oops I ++ and I ++ what that syntax tells us is that it will increment. So,
it will add one to I. And what's happening here again just to quickly read it out is we're saying we want to have a loop that counts from zero. It will count until we are right at the um so one less than the count. And that's because if we had um if we had one thing in the list, the zero index will be for the first item one. If we have two things in the list, when we get to the end, we will be at index one. So, it's always off by one. That's a tricky part that you're going to want to pay attention to. So, now we actually have something that will count from zero up until the number of items we have. We can do this to make it maybe a little bit more clear. We can get the item out and we
can index into our collection like this. So, this line here on line 52 is actually going to get us the list item at index i. I'm seeing that it's putting some red on my screen and I think that might just be leftover, but I'm not totally sure. Um, okay. So, what I want to do when we're writing this line out is actually format this in a prettier way. So, instead of just writing the item, I'm going to actually do something a little bit different. Now, this is going to be um maybe I'll keep it pretty simple here. I'm going to use some string concatenation. And concatenation just means that I'm putting some strings together. So what I would like to do is I would like to put a dash in the beginning and then a space and then I want to put the item after
it. But that might not be enough. I think that what I probably want to do because I still want to get that index included there. I think maybe if we were to use a tab. So back slasht inside of quotes here. That's going to be a tab that we can use. Um, then we could maybe do something like I here to tell us the index and another space. So, how this will actually look if I were to go write it out, it would look like a um a tab character. I can't maybe I can if I press tab uh yeah, if I press tab on the comment, it's not really showing. So, it'll be a tab character. Then we would have like zero dash and then the next item would look something like this our oops second item. So that's the format that we should
be able to get in the console by using this. Right? So this is a string. It's just a tab character. Yes, this is an integer. And when you're doing a string plus an integer in C sharp, what it will do is convert the integer to a string. Then we add on this space dash space and then we add on the item text. Okay, so we still haven't implemented the removing, but let's go try this again and see if it prints out a little bit more obvious for us. I will press run. we're going to want to add something, right? So, let's go to option number two and we'll say this is our this is our first item. Let's actually add a second thing so we can see how this prints out with more than one item in it. This is our this is our second
item. Okay. So, at this point, we should have two items in our to-do list. So, if we press one and go to view, we should hopefully see this new, more improved formatting that has the index. And we do. So, it does say your to-do list. We get this nice tabbing so that we have some visual cue that these are the items. We see the index here. So, it starts at zero and says your first item. Then we see one. This is your second item. And then it stops at this point because there's no more items. Okay. So to go implement removing, let's try to get the user to input the index of what they would like to remove. And as I mentioned earlier, we're going to have a string input that we need to convert to an integer. So where else have we done that
already? If I scroll back up, you can see that we have it up here right where we got the input, we have our integer, and we have the parsing. So, what I'm going to do at this point is I'm just going to copy this. We're going to modify it, but I want to copy it just so I have all the pieces. So, in option three, which is removing, I'm just leaving a comment there. So, it's a little bit more readable as people are joining or watching. And it says, "Please enter your to-do list item to remove." We actually want to say, "Please enter the index of the to-do list item to remove." And then press enter. And on my clipboard, I will paste in what we had earlier. That didn't format nicely at all. That's [laughter] okay. Um, and we will we have to change
some variable names because these were already used. So we can say user input for um remove index and then we can call it remove index. Again I'm just reusing the code that was above because we saw that this was working for us. And then we will put these new variable names inside of the parsing. And just a heads up that a really common mistake or a really common source of error for not just C programmers but every programmer is copying and pasting stuff and then forgetting to update what you need to update. So I got to make sure as I'm recording this live I don't mess that up. But I'll paste in user input for remove index because that's our new variable here. We want to output that into remove index. And then we want to say remove index parse result. So we have a
new variable to look at that has the result of the parsing. And of course we have to put that here. Okay. So this gave us already a little bit of error handling that we had for free above. Now what we can try doing is now that we have an index, we can try removing the item at that index. If you're thinking through how this might work, something that might be crossing your mind is, well, okay, if we have two items in the list, we know if we want to remove the first item, we would remove at index zero because C is a zero index language. If we want to remove the second item, then we would use index one. Well, what's going to happen if we had two items in the list and someone said remove at index six. Well, we can actually see what
happens. So, before we add in any error handling to try it or any error handling to see if we need to avoid it, let's actually try it. So we can say what did I call the list new to-do list or sorry where is it to-do list items this right here what I want to do is say remove at so that's a a proper or I guess it's a function that we have available to us on the list type and we can remove at the index which we said is called remove index and it compiles like it's not complaining. So let's go try it. If I say run and let's actually start with nothing in the to-do list and try and remove something. So I'm going to use option three. So it says please enter the index of the to-do list item to remove and then
press enter. So if I try to remove index 2 and there's nothing there, we get an exception. So for those that aren't familiar, an exception is when your program does something that was unexpected. It's an exceptional case or an error and it's actually telling us the index was out of range. It must be a non- negative. So two is non- negative and less than the size of the collection. So in this particular case, it was not less than the size of the collection. [snorts] So that's going to mean that we probably do want to put in some error handling. So let's go ahead and do that. We can add another if statement right above this remove that says if and what are the conditions we have to look for? I'm just going to put it into a comment so we have it right in front
of us. So must be non- negative and less than the size of the collection. Okay. So that means that remove index right must be non- negative. So greater than or equal to zero is non- negative and it has to be less than the size of the collection. So that means remove index has to be less than to-do list items count. That's not the right word. [laughter] Right? So if we go read this, let me put these curly braces in. If we read what this says, remove index is greater than or equal to zero. So that is non- negative. These double amperands just mean and. So like literally it means and. Um so this condition has to be met and this other condition has to be met. So remove index has to be less than the count of the items in this list. So this statement
should match what this wants. So we should tell the user now that that's not going to work. So we can take this and we can say input um must be well let me just copy this. [laughter] So input must be non- negative and less than the size of the collection. Let's go ahead and try it again and make sure that our error handling works. So we should see a similar message but it should not say anything about an exception. So we'll start by removing and it's empty. So if we pick uh I don't know let's pick literally negative one because that is negative. Uhoh that didn't work. Why didn't that work? Runtime index was out of range. just give us the line number 84. So, anyone know why that happened? It's because my condition is actually backwards. [laughter] So, what does that mean? Well, what
I was doing before was I was trying to handle the error case. This is not the error case. This is actually the good case. So we need the exact opposite of this. So this was totally unplanned. I feel a little bit stupid. But at the same time, this is a really good opportunity to show you how you can inverse really simple logic. So it's a good exercise. Let's quickly try it out. Now, there's lots of resources online, um, and there's like a lot of computer science stuff that can show you like the math behind how this works, but a couple of simple rules are that you're going to want to switch to inverse something. You're going to want to switch your ands with ors, and then we need to get these conditions inverse as well. A really simple simple way for us to do this
and I don't want to just cheat but we could put the knot operator in front of the whole thing and this will solve our problem because that nod operator will just invert everything for us. But I don't want to do that because I want to show you how we can actually go ahead and reverse this ourselves. So if we're looking for the error case, there's two conditions that will give us an error. One is that it is negative, right? So I just inverted that small expression. So if it is negative or right, so here I am inverting the and it's now an or the remove index is greater than or equal to the count of items. So you can see that all that I did was I flipped this condition. I flipped this condition and then I changed the and to an or. Now we're
going to be looking for the error case. And then when we have the error case, we can tell the user. So let's go prove it and make sure that my boolean logic checks out. Again, we will try to remove it's an empty to-do list. I'm going to remove at indexative -1 and it says input must be non- negative and less than the size of the collection. So far so good. Let's try removing again and we will give it one instead. Input must be non- negative and less than the size of the collection. Okay, one more to try out. I think what happens when we put in zero? input must be non- negative and less than the size of the collection. Awesome. Yes, in the chat, Deorggan's law. De Morgan's laws if you're interested. Yeah, go read up if you're brand new to programming and want
to understand better how this works and how I converted that. Lots of resources online. And again, if you're really into the math side of things, personally, I never was, but there's a lot of really interesting things about the properties of some of these conditions and how you can swap them around. So, really interesting stuff. Okay, so we seem to have error handling, and that's nice, but we never actually prove that this works when we don't have an error. So, let's go add something. We'll use option number two to add an item. our first item. Let's add another one. Our second item. If we were to view what this looks like now, we can use option number one. Your to-do list has two things in it, right? We have our first item and our second item. And we have, of course, zero indexing here. So, let's
go do the final thing, which is to remove it and see if it works. use option three. And now we have to pick either zero or one. Well, let's try to remove our first one at at index zero. Okay. Well, I probably didn't do very good at outputting to the user if it actually worked. And we can improve that. to-do tell the user it worked. So, we can always follow up on that. But let's see if it did. Let's use option one to view that to-do list. And it seemed to work, right? You'll notice that our second item is now at index zero. Previously, if I scroll up a little bit more, you'll see that it used to be at index one, but we removed the first thing in the list. So, that ended up taking that whole entry out and shifted up the second
item to the first index at index zero. And honestly, I think that's about it for the basic functionality. If I scroll back up, the basic functionality that we set out to go achieve. And what's pretty cool is that through this exercise, we actually had a whole bunch of to-do list items and some suggestions in the chat. Thank you, by the way, about some things we could make uh do to make this a little bit better. So, we could do a little bit better to print when the to-do list is empty. It wasn't very obvious when that was happening. We could validate the user input here when you're trying to add something. And then I made another to-do list entry, sorry, to-do list item here in the code that was telling the user it worked because when we removed something, we had no idea unless we
viewed it. And I just noticed we have this one final to-do. And we should probably just add this one in super quick. And we can say console right line that option is not valid. Oh, where did that go? Oh, sorry. My cursor jumped around there. And if I run this and I say that I want to do option one, two, three, we see that option is not valid. Awesome. Okay. So I think that's it for today. Again, just to recap on the things that we were able to touch on, we got to see some if statements. We got to see all three variations of loops that are pretty common. So we saw a while loop. We had a for each loop. And then we converted it to a for loop. We got to work with some basic collections here. Right? So we defined our list.
We added things to it, removed things from it, and then iterated over it. And the other thing that we got to do was type conversions. So, we were able to take some string input and convert that to an integer. It's probably worth mentioning, it's not specific to C, but we got to actually play around with some usability. And yes, of course, this is a console application. So, if you were setting out to actually go build the next cool to-do list application, odds are you're probably not going to go write this in a console application. But that's okay. It's a really good way to learn. But we got to practice some usability like error handling. We discussed a few things like how we might format some of the output. And I think that those things are really good to consider as you're kind of exploring how
to program because those are types of things that are really valuable in programming, but they're not language specific either. And I think that's going to be it for today. So, thanks so much. Um, thanks for the comments in the chat. I appreciate it. And, um, yeah, we'll do another one like this in the future. And I think to be honest, I'm going to schedule another one that is going to be uh not beginner probably in about 2 weeks. I want to test it out in .NET Fiddle and we'll probably focus on uh unit testing, autofac dependency injection, all sorts of fancy stuff. Um so we'll look at that I think and in another few weeks from now we'll do another intro one. So thanks again and we'll see you next time. Thank you.
Frequently Asked Questions
What programming concepts will be covered in the to-do list project?
In this project, we're going to cover if statements, type conversions, collections, and loops as we build our console to-do list.
Do I need to download any software to follow along with the coding?
No, you don't need to download anything fancy! You can code right in your browser using Net Fiddle, which makes it easy to jump right into coding.
How can I handle invalid input when the user enters something that isn't a number?
These FAQs were generated by AI from the video transcript.I demonstrate this by using a try parse method to convert the string input into an integer. If the conversion fails, I handle the error by informing the user that their input was not an integer and asking them to try again.
