 Let's take a look at how to install Visual Studio using the custom option. For this example, we'll use the Community Edition of Visual Studio 2015. In order to get it, simply visit visualstudio.com and click on the Download Community 2015 button. Once we've clicked on the download, it'll download to our computer and it's a web installer. So we click on the Run button and it will initiate the installation routine for Visual Studio Community 2015. Once we have the option screen available, it's time to start looking at customizing the installation of Visual Studio. For the most part, the default allows you to create web and desktop type applications. But if you want to create different styles of applications or include more languages, then the custom option is what you should be choosing. I always recommend selecting the custom option for the installation of Visual Studio 2015 to ensure that you're getting the packages and libraries that you need to create the applications you may wish to use. So by selecting Custom and clicking on the Next button, we are now brought to the screen where we can select the different features. The first option is Programming Languages. And if we click the arrow to expand it, we can see that we have Visual C++, Visual F-sharp, and the Python tools for Visual Studio that are additional programming languages that will get installed if you select this option. Remember, by default, Visual Studio Community Edition will only install C-sharp in Visual Basic Templates. Also notice under Visual C++, we have options for the common tools, the Microsoft Foundation classes, and then Windows XP support for C++. For my purposes, I like to have all of my programming languages available to me because I create projects using the different languages all the time. So I'm going to select the checkbox next to Programming Languages to install all of those programming types. Also, under Windows and Web Development, we can choose various options here for things such as Quick Ones Publishing tools, SQL Server Data tools, PowerShell tools for Visual Studio, Silverlight Development, etc. Here's a very important component. If you want to develop universal Windows applications, we need to ensure that we have the tools, the emulators, and the SDK. Now, you can choose the default install of Visual Studio, and then come back and install the Windows 10 SDK at a later time, and that will include the tools, the SDKs, and the emulators for you. But it's so much easier to install these during the installation of Visual Studio. Please note that it will increase the install size of the application, so the tool set will be much larger. So again, depending on what it is that you want to do, you may want to select Universal Windows App Development Toolkit, PowerShell tool for Windows, or for Visual Studio, rather, if you want to be using PowerShell tools within your applications. If you need backward compatibility for Windows 8.1 and Windows Phone 8.0 and 8.1, you can select this option. Also, there are some common tools or cross-platform mobile development tools. These are important if you want to develop applications using the Xamarin platform. Xamarin is a cross-platform tool that allows you to create applications for Windows Phone, for iOS devices, and for Android devices by using the C-Sharp Language in Visual Studio. All of these tools are available for the cross-platform mobile development using Xamarin platform. It includes all of the emulators as well. So again, remember, it will increase the size of the install base for Visual Studio. You might also notice that because I selected the cross-platform mobile development tools, we now have a little box inside the Windows 8.1 and Windows Phone tools. If we expand that, we'll see that it has included tools and Windows SDKs. And the reason it does that is because there's a potential that you may want to target Windows Phone 8.0 or 8.1 applications. So the tools and SDKs will also get installed. At the same time, the common tools check box includes a little square box indicating that we have also added another component here, and that is the git for Windows. So we can install git, which is your source control, git hub extension to Visual Studio so that you can integrate with git hub source control projects, and then of course, an extensibility tool to update three for Visual Studio as well. You'll notice that by selecting all of these options, setup can require up to 48 gigabytes across all of the drives that we'll install it on. So again, review each of the items that you have selected to ensure you have all the necessary components, tools, and SDKs. For your development, tools of choice or platforms of choice, and then select the next button. Once you do, you basically see a quick little selected features screen that will tell you all of the different items that you have selected. And by clicking install, you'll agree to the license terms of all the software components. If you're not sure what those are, each one of the items that has license terms allows you to click on it to view those. Once you're satisfied with it, click the install button and Visual Studio starts installing all of the components that you have selected. So this is a quick overview of how to perform a custom installation of Visual Studio 2015. Hi, I'm Bob Taber with Developer University. For more of my training videos for beginners, please visit me at devview.com. In this lesson, I want to build a super simple C sharp application I want you to follow along. It's a Hello World application, meaning that we're merely going to print out the words Hello World to a console window. And the point of this exercise is just to show you the basic workflow. So I'm not going to attempt to even explain why we're doing what we're doing. The focus will be on what I'm going to do next and how I'm doing it. In other words, I want you to focus on the basic workflow. That'll be the same for all the applications we'll build in this course and pretty much every application you'll ever build using C sharp. Things like how to create a new project. Where do you type in your C sharp code? How do you test your application to make sure that it's running correctly and what do you do whenever you have an error in your code? How do you save your project? Things of that nature. So for now, just try to follow along. Don't worry if something doesn't make a lot of sense at this point. That's really what the rest of this course is for. The next few lessons after this one, we're going to dissect this tiny little application that we built. And I'm going to explain at that point why we did what we did and then what does the code mean and why it's doing what it's doing. And just a quick reminder, like I said in the previous video, the introduction of this course, I'm going to assume that you have some version and some addition to Visual Studio already installed. Even if your Visual Studio looks a little bit different than mine does on camera here, don't be overly concerned about that. The basics are the same no matter what, I promise. All right, so let's go ahead and get started here. And to begin, we're going to create a new project. There are a number of different ways to do this, but I'm going to keep it simple and go to File, New, Project. And selecting that menu option will open up the new project dialog. Now, chances are the number of items that you see here in this center part will be dramatically different than the items that I see based on which version and addition of Visual Studio that you have installed. However, you should be able to select templates and then select C sharp. And one of the options should be a console application. So I want you to select that and then we're going to rename this project to Hello World. Now, notice, I used a little naming convention where I use a capital H and Hello and a capital W in World, and I don't use a space in between the two words. Now, that's just a naming convention that I came up with to help me identify projects a little bit easier, something I recommend that you follow. We shouldn't have to make any other changes in this dialog. I'm going to go ahead and click the OK button. And Visual Studio will go off and now create the starting point of a console window application for us. All right. And so now you should see in this main area, in this text area, a file open called program.cs. And there's some code here that is already generated for us, boilerplate code. We're going to ignore most of that, except we're going to find this innermost set of curly braces. Now, one of the first things you're going to need to do when you're learning how to develop software is tell the difference between parentheses, curly braces, square brackets, angle brackets, and I don't know that I left out any, OK? But here we want the curly braces look like little mustaches turned on their side. These are important. I want to go inside of those two, that opening and closing curly brace and make some room for ourselves. This is where we're going to type our code. So it's approximately line 13 and 14, at least in my copy of Visual Studio. And then I'm going to type in the following. Type in console and you may notice now this little window pops up below what I'm typing. You can safely ignore that for now. Eventually, this becomes our best friend. But for now, it might kind of be distracting and give the way to try to ignore it and type in everything by hand to the best of your ability. Console and then I want to use the period on the keyboard. I'm going to call it the dot. So console dot and then capital W write capital L line. Next, I'm going to use an opening and closing parentheses. So that's not a curly brace. These are just the characters you would use for a smiley face in an emoticon. And then inside of there, I'm going to use the arrow keys on my keyboard to kind of navigate around here. I'm going to go inside of the opening and closing parentheses. And I'm going to use two double quotes. So it should look like that. All right, make sure you don't use single quotation marks like that. That's not what we want. We want double quotation marks like that. Okay. And inside of there, we're going to type in the words hello in world. All right. So make sure that you have an open parentheses, a double quote, the words hello world, then another double quote, then another parentheses, a closing parentheses. And then at the very end of this line, I'm going to use a semicolon and it looks like that. All right. And it's not a colon. And it's not a comma. All right. It looks like that. All right. And then I'm going to use the inner key on my keyboard to go to the next line. I'm going to type in console dot capital read capital L in line, opening and closing parentheses. Now you may have noticed that as you, as you type in the opening parentheses, that visual studio will automatically type in a closing word for you. Don't let that throw you can continue just to type through. That but make sure that you have exactly what I've typed into my code window here for these two lines of code. Make sure that the capitalization is correct. Make sure that you're using a period not a comma for for the for the little mark that comes after the word console. Make sure you're using parentheses and not some other type of bracket or brace and then make sure that both lines of code end with a semicolon. All right. So the next thing that I want to do is save my project. So I'm going to go and there are a number of different ways to do this in visual studio. Again, I keep it simple and go to file, save all. All right. And then the next thing I want to do is now see my application actually running. And so to do that, I can either find this little green arrow, this little green triangle that has the word start next to it. Or if I don't see that by default in my little toolbar here at the top, I can go to debug and select start debugging. Either way should work. I'm going to go ahead and click that. And you'll notice that some windows pop up and visual studio changes its appearance a little bit. Now off to the side of my screen, the console window popped up and we see the words hello world with a blinking cursor below it. I'm just going to hit the enter key on my keyboard and then the console window disappears and I'm back individual studio kind of resets itself and we're successful, right? However, maybe your experience wasn't successful. Maybe you saw an error message. So what I want to do is take a moment and look at some common errors that people that are new to C sharp might run into and how to remedy them. And this is a good opportunity to learn some of the syntax rules of C sharp as we make mistakes. Okay, so I'm going to pause the video, make a mistake and then we'll talk about it, pause it and so on. Alright, so when you attempted to start the application, you may have seen a little dialogue pop up from visual studio that says there were build errors. Would you like to continue and run the last successful build? Always select no for that. Okay, and what you'll see next is a list of errors. Now, in some cases, the error messages will be obvious to you and they'll make a lot of sense. Sometimes they won't like the the verbiage might be something for just not familiar with yet invalid token and class structure interface. What does that mean? Okay, so typically what you can just do is double click on these and it'll put your mouse cursor on the line where the problem is. Notice that visual studio also gives you another visual way to tell that there's a problem with your code gives you this little red squiggly line. Sometimes you'll see blue squiggly line. They're a little bit different, but essentially this is an area of the code that probably deserves your attention. Something you need to fix. Now, in this particular case, the problem is that we didn't type our code in between the innermost opening and closing curly braces. And so this is an issue with regards to defining a code block in C sharp or a block of code. So different C sharp commands belong in different kinds of code blocks. And I'm going to spend a lot of time in this course talking about the different types of code blocks and what belongs in each type of code block. But to remedy this issue, what you need to do is use your your mouse and just kind of drag and highlight these two lines. Or you can use the shift key on your keyboard and the arrow keys to kind of highlight that area. Hit control X, then move up in between the opening and closing curly brace and paste control V that code in there. And then it should run correctly at that point. All right. So that teaches us the first thing about C sharp. It matters where we type our code. All right. Or when you try to run the application, you may have seen the same build error dialogue. Except you see the message semi colon expected. Hopefully this is an obvious remedy for you. If you double click on that error in the error list, it should take you to the end of the line of code where you forgot to add a semi colon. And so that's the second thing about C sharp that we're going to learn is that just like a properly formed English sentence has to end with a period or a question mark or an exclamation mark. A properly formed instruction in C sharp has to end with a semi colon. All right. Or maybe the error that you saw was something like a syntax error, something expected. The name hello doesn't exist in the current context. The name world doesn't exist in the current context. If you were to double click these, you'll get to the vicinity of the problem. And you'll also see that there's red squiggly lines beneath the words hello and world in between of our parentheses. Now remember, we needed to use double quotation marks around that string of characters hello world. And so alphanumeric characters that we want to literally write to screen or present in some way. We need to surround them with characters that indicate that we want to use this literal string, the string of literal characters. Okay. So to do that, we use double quotes, or perhaps you see the error, something like the name console does not exist in the current context. You're like, doesn't exist in the current context. You look at the word and you say, well, looks spelled correctly. Remember that I told you you had to type exactly what I was typing. And so C sharp is case sensitive, meaning that a lower case C and an uppercase C mean that you're typing two completely different things into C sharp. Okay. And that that does that is tricky because many of us are not used to that degree of precision whenever we're communicating. But when communicating with a computer, you have to be precise. So in this case, all we needed to do was change the capitalization of the word console. And we're back in business. Or perhaps you see something like console does not contain a definition for either right line or read line. And again, you're looking at it and you're thinking it's spelled correctly. Well, what could the problem be? Here again, capitalization is important. Lower case R read line is different than uppercase R read line. And lower case L read line is different than lower case uppercase L read line. Okay. And so again, things have to be spelled spelled correctly and have the correct capitalization in order to be processed correctly by the C sharp compiler. We'll talk about compilation in the next lesson. Now, fortunately, if you're not good at spelling and you're not good at typing and capitalization and you're just not as precise in the way that you would type a letter or an email message or even a text message. Fortunately, Visual Studio can help you out. There are tools that will help you not only write your code more quickly but also more accurately. And chances are if you utilize those tools, the chances that you will miss some of these really simple syntax things like capitalization will almost be completely eliminated. We'll talk about some of those tools in an upcoming lesson. All right. But assuming that you got all of this to work correctly, you're really well on your way to building applications. You've already crossed over one of the big first steps. And as you undoubtedly learned in this lesson, writing C sharp code is an exercise in being precise in precision. And again, fortunately, the Visual Studio IDE will help you out a lot when it comes to that. It will give you clues and maybe some of the phrases and the words that they use to explain the issue might not be familiar to you yet with experience. It will be. But generally, it'll point you into the right direction and with the red squiggly lines in the message, you can typically figure out what the issue is. Now throughout this course, if you run into a wall and you simply can't figure out what the problem is, do this. Compare character by character. Take your time until you kind of develop a vision for the problems where your eye will jump to the problem and code. Compare what you wrote versus what I wrote. I'll supply the source code to you. And so open it up in a second copy of Visual Studio and then just look line character by character. What did I do different than what Bob did? All right. And that will usually help you figure things out if you can't do it on your own. All right. So in the following lessons, we're going to focus on two things. First of all, we're going to talk about why we did what we did and what was going on behind the scenes that turned our code. Turned our code into a working application, albeit a small application. What happens whenever we create a new project? What happens whenever we choose to save our project? What happens whenever we choose to start or run our application? And then secondly, we're going to talk about the syntax of the C sharp code that we wrote. And we'll learn more syntax rules and more keywords as we go along. So if precision is so very important in C sharp, then you're going to need to have some explanation as to what all those little words and symbols actually mean and some rules to guide you as you're writing your own code. It's really easy once you get a few of the basics under your belt. Being completely honest, many, many people learn how to code, how to write code and C sharp is a fairly easy language to learn. You can do this. Just you got to put a little bit of time, a little bit of effort to figure it out. All right, so we'll begin that process in the very next lesson. We'll see you there. Thanks. Hi, I'm Bob Tabor with Developer University. For more of my training videos for beginners, please visit me at devu.com. Now in this lesson, we want to start the process of dissecting that little application that we wrote in the previous lesson. Now previously, I wanted you to focus on the workflow, what we did and how we did it. But now what I want to do is focus on why we did what we did. It's really crucial at this point that we cement some really important ideas in your mind because they're going to provide the basis, the foundation for everything that comes next. So what I want to do is start on the inside and kind of work our way out. And I'll start by talking about the nature of writing code. When you learn how to write applications with C sharp, really any programming language, learning the syntax of C sharp, or in other words, learning the nouns and the verbs and the punctuation of the programming language is really just half the battle. The other half of the battle is learning about related prebuilt functionality that's available to the code that you write. Now in our case, Microsoft has created something called the dot net framework, which sounds kind of spooky and mysterious, but it's really not that bad. It's actually pretty large, but we're only going to focus on two specific portions of it for our purposes. The first part that I want to focus on is something called the class library, which is simply just a library of code that Microsoft wrote to take care of difficult tasks so that we as software developers, we don't have to worry about them. So there's library code to help with many common sorts of things that many applications will need, things like working with math or working with strings and text and working with dates, manipulating dates and times, maybe displaying things to the computer screen or transmitting information across a network. So a lot of that kind of foundational stuff that would be difficult for us to write and is utilized by many different applications. So that's really the first part, taking advantage and understanding the class library of the dot net framework. The second part of the dot net framework is called the runtime. It's also known as the common language runtime. You'll see it called as the CLR as well. And really, it's just this protective bubble that wraps around your application. Your application lives inside of it. It runs inside of that protective bubble. And it essentially takes care of a lot of the low level details so that you, the software developer, you can focus on what your application is supposed to do, not worry so much about how it's actually accomplishing it under the hood. You don't have to worry about the computer's operating system interacting with it and interacting with memory and interacting with the hardware, the computer itself. Many of those things are kind of abstracted away from you. You don't have to worry about them. Furthermore, the CLR, that runtime also provides a layer of protection for the end user so that you, the malicious evil software developer, you can't do something really bad to somebody's computer without them at least giving you permission to do it in the first place. So without their knowledge and their approval, you're not going to be able to wipe out their entire hard drive, for example. For right now, it's the dot net framework class library that I really want to focus on because it's what we used, whether you realize it or not, whenever we were writing our first application. So for example, in lines 13 and 14 where we did our work, you see console dot write line and then we used open parentheses, close parentheses and so on. We were using code in the framework class library that knows how to display text into a console window. All we got to do is say, hey, use this text, stick it in a window. And we don't really care how it does its job. We just care that it did it. And so the next line of code, this console dot read line, it was also really important. We're telling the application to wait for input from the end user before continuing its execution. So again, here we're calling code in the dot net framework class library that knows how to accept user input. We call that I use the enter key on the keyboard and then the application continued on, it exited and we were back in the visual studio. So in both of those lines of code, we were utilizing methods that were defined, that were created by somebody in Microsoft to handle that interaction with displaying and retrieving data from the end user. So what were to happen if we were to comment out that line of code? And here to comment out a line of code, I use two forward slashes on my computer. It's over the question mark. And commenting out code simply means that I want those instructions to be ignored. Now, I could have just deleted that line of code completely, but I might want it later. So maybe I don't want to remove it completely. I just don't want to, I just want to ignore it for now. I also might use code comments to write myself some notes to remind myself of something about the application in the future. We'll talk about code comments a little bit later. But if we were to run the application now, watch what happens. Okay, it ran and it's already done. What happened? Well, you might have seen a flicker on screen for like a fraction of a second. The reason was because, hey, it executed this one line of code. And it said, well, looks like I'm done here and it exits out of the application. By adding the read line, we're now stopping execution waiting for the end user to do something before exiting out. Okay, so hopefully that makes sense. All right, so next let's talk about the position of the code that we wrote. I made sure to emphasize that you have to write the code in the correct place. And the correct place was in the in between the opening and closing curly braces of that innermost set of curly braces as defined kind of by the by the level of indentation that we saw in the boilerplate code. So if you don't add the code there, we saw what the ramification of that was, right? The application, you'll try to run it. It'll give you a runtime error. The correct place for that code was where we have it right now in between that opening and closing curly brace that you see on screen. Now, as you can see, there's several sets of curly braces. And so it's important that we talk about what these do. And I need to oversimplify things here. We will come back and fill in some of the details later. But essentially, you have an opening and closing set of curly braces and those define a code block. And code blocks typically have names and they have purposes. So in this particular case, we have a first code block in this code block has the name main. And this particular code block is known as a method. And this particular method by convention is the very first method that's called whenever your application is executed. And so I don't want you to worry about these other words static and void and even the string in the args for right now. We'll talk about those later on. But this entire code block here as well as the line above it, they define something called a method and a method is simply a block of code that has a name. Now later on, you're going to come to realize that a method is so much more than that. But I want to use that as a working definition as we're getting started here. The method has a name and when you have a name, you can call a name and say I want you to execute. And so we'll talk about methods again a little bit more in a little while. This main method lives inside of another set of curly braces and that set of curly braces also have a name. The name is program. It's a class called program. And so you can think of a class as simply a container for all of the methods of your application. You can kind of keep the methods that are related to each other in separate classes. Now, what do I mean by related to each other? Well, that's really for you the developer to decide as you get deeper into programming, you're going to come to understand the thought process behind organizing your code. But that's a little ways off for now. Just trust me on that. Now, I said that a class was merely a way to organize your methods. It is so much more than that. And again, I'm way over simplifying this as we're getting started here. But the main takeaway for right now is that code is organized in curly brace containers and you have some blocks of code that reside inside of other blocks of code. And to kind of emphasize that, again, here we have another set of curly braces and this set of curly braces has a name as well. In fact, it's a namespace called Hello World, which happens to be the name of the application that we gave it. Again, keep things extremely simple here. A namespace is just another way of organizing code. Again, at some point, it becomes so much more than that, but let's keep it simple for now. So let's take a look at these lines of code and kind of illustrate these ideas about namespaces that contain classes that contain methods. Here, what we're doing whenever we're calling console.writeline is we're actually making a call into the .NET Framework class library. Remember, it's that library of code supplied by Microsoft. And we're saying in that entire library, there's a book and there's a chapter inside of that book that I want to reference. So in this case, we're saying that book is the console book, the class. And I want you to look at the chapter named writeline that has the definition for this method. All right, hopefully that analogy works for you. But we're looking inside of a library to find a class and we're going to call a particular method inside of that class. And by using its name, we can execute all the code that was written inside of that method. Same with the method that we're calling below it as well. Notice that there is a period that we use between the name of the class and the method name. And we use that, it's called a member accessor. It allows us to access a member of the class. Or in other words, now that we know what the book is, we can find out what chapter we want to reference. Hopefully that analogy works for you. Now, notice also that both, whenever we call the writeline method and the readline method, that they both have parentheses following them. Now, in the case of the writeline method, we're actually sticking something in between the opening and closing parentheses, whereas in the readline method we're not. But essentially, those parentheses are saying not only do we want to reference that particular class method name, but the parentheses mean I want you to actually invoke it, execute it, do it now. Okay, so that's the purpose for those parentheses. Now, we can say do it now and pass in information. Do it now with this stuff, with this argument. So we're passing in an argument to the writeline method and saying, we want you to do it right to write this to screen. And here's what we want you to write. So it's an input parameter to the method named writeline. Okay. Now, don't worry, we're going to come back to the notion of methods in the future, as well as passing values into a method like we did as we passed in the literal string hello world into our method here. Just know that whenever you see parentheses after a given word in your code, you should be thinking that that code is being called right now as we step through the execution of the code. Next up, let's talk about the semicolon. We've already kind of explained it in the previous video, but just to kind of emphasize it, notice that almost everything, even these statements at the very top, have semicolons with the exception whenever we're defining a namespace a class or a method. And we said at the time that the semicolons are actually similar to the period or exclamation mark or question mark at the end of an English sentence. It completes a thought in C sharp. Now, some programming languages like visual basic, for example, they don't really have this idea. They only allow one complete thought per line of code. However, with C sharp, you could you could do this what I'm about to do. Watch. Okay. Now I have both of those lines of code on a single line. All right. And if we run the application will work just as it did before. So the way that you separate or indicate that you have two different complete thoughts is through the use of a semicolon. Furthermore, we could put lines of code on separate lines like this. Now it wouldn't make sense in this case because the line of code is so short it actually makes it difficult to read. But sometimes when you have a very, very, very, very long line of code, you'll see me split that line of code into multiple lines. And still the application will execute. Now, in other programming languages, you wouldn't have that behavior. Because really white space and line feeds and things of that nature, they don't matter to C sharp. The only thing that really matters is to indicate a complete thought is a semicolon at the end of the line. Let me go ahead and get rid of all that. And the other thing that I want to mention here that you may have noticed is the level of indentation that you get automatically from Visual Studio. Now that's completely optional. And Visual Studio nudges you in the right direction. But essentially, even if you were to kind of come out here and we'll use the tab key several times and write the word console dot right line, something like that. And notice that that Visual Studio reinvented it for us. Why do you suppose it did that? Well, many people believe that indentation helps the readability of the code so that you can see what code container where code resides inside of the other curly braces inside of your application. All right. And so kind of along those same lines for readability sake, notice that there are many different colors that are used as text inside of this text editor window. You have these royal blue colors and these are my default colors. Yours might look a little bit different, but by default, I think you have some royal blue, some black. You have aqua color here. This is a dark red. You have some light gray and light blue. All right. And all of those are used to help you identify the parts of speech. I guess you can say inside of the code that you write. All right. We'll talk more about that as we talk more about the syntax of C sharp in an upcoming lesson. Okay. All right. So now that we've talked about the code that we wrote and its position and formatting in white space and tabs and all that sort of stuff, what I want to do is stop right now for this video. And in the next one, I want to talk about the files themselves, the file that we typed our code into, how that relates to projects and even solutions. What happened when we saved our project? What happened when we actually ran our project? And so we'll do that in the very next lesson. We'll see you there. Thanks. Hi. I'm Bob Tabor with Developer University. For more of my training videos for beginners, please visit me at devu.com. Next, we're going to talk about how code files are organized into projects and solutions and then where you can actually find these projects and solutions on your hard drive. So whenever we created a new console project, the program.cs file was opened for us automatically in the main area in Visual Studio. And that's one of the things that project templates do for us. Whenever we choose file new project and we see the new project dialogue and we choose a project template, they provide a great starting point for the type of application that we want to build. It includes files with boilerplate code, important settings and other resources that we might need whenever we're building that type of application. So as you can see, we're working inside of this file in the main area and there's a series of tabs. Again, this isn't intended to be an overview of Visual Studio, but it's important to note that the names of the code files that you're working on are contained inside of those tabs. And if you take a look over to the right hand side, here is the solution explorer window. And it has a tree like structure of all the items that are kind of contained inside of our project. Now, again, as I said at the outset of this course, this isn't intended to provide a tour of Visual Studio per se. And there are other resources on Microsoft Virtual Academy that can really help orient you to using Visual Studio and the various windows and functionality that it contains. But the solution explorer is probably the most important part of Visual Studio next to this main area where you'll usually see the text editor and other designer windows. So simply put, the solution explorer is our main navigational device to the other files and settings that comprise our program. So you can see here that there is a program.cs file. Now, if I were to close the program.cs tab in the main area, I can always get back to it and open it up again by double clicking it inside the solution explorer. Alright, you can see it's open once again. Now, files and important settings are organized into a concept called projects. So you can see here this word hello world is actually a project. You can see there's a little C sharp icon next to it, letting us know that this is a C sharp project specifically. And projects get compiled into a single dotnet assembly, which we'll talk about later. Furthermore, one or more projects are organized into solutions and you can see in the solution explorer, we have one solution here at the very top solution also named hello world that contains one project. Now, in many cases as you're getting started, you're only going to have one project inside of one solution. But as you come to build more complex applications over time, it's highly likely that you're going to need to manage multiple projects that are somehow related. Now again, the reason might not be obvious at this point, but as you continue to learn C sharp and how to build more complex applications for large companies or for yourself, this becomes a crucial code management strategy. But just for now, except the fact that there's this extra layer of a solution and one solution can contain one or more projects. And the projects will contain then all of the code files and the settings and the like that will be used to create an actual executable program. So trust me, these concepts will become more important after we get past the basics. Now the big question at this point should be, where are all these projects and solutions and files actually stored on your hard drive? I mean, can we see them? We can see them in the solution Explorer. Where are they actually on your hard drive? Well, when we created this Hello World project, we merely provided the name of the project you'll recall. And then I said, go ahead and accept the other defaults by default, Visual Studio will put your projects into your documents folder. So if you take a look here and we navigate into the documents folder, it will put your projects into whatever version of Visual Studio you're currently using. So you can say I have side by side Visual Studio 2013 and 2015. We're using 2015 for this series, but it could be a future version of Visual Studio. You'll look in that particular folder for your version of Visual Studio. And as we drill in, there will be a projects folder. And as you drill in, you can see that by default when we created a new project, it put it here in our document slash Visual Studio, whatever version slash projects folder. Okay, so as I add more projects, this obviously will be filled up with folder names for those projects. And it's important to note that whenever you create a new project, you don't have to create it and put it right here. You can put it anywhere. And so to keep things organized, you're typically going to put them, keep them in the same place. Now, furthermore, you can actually open up a project that saved anywhere on your computer as well. So for example, I have this, in this course, I'll supply the projects after I record the video, I'll zip them up and you'll be able to download them and then open them up on your own hard drive and then walk through them and to better understand them. And so just to kind of demonstrate how you do this, I have this project called, is zipped up into a file called example.zip. And so what I'm going to do is actually right click this and select extract all. And then in the extract compressed zip folders, I'm just going to put this on my root. So c colon slash example, and then click extract. And so now you can see that on my local hard drive, I have an example folder. And inside of that folder, there is a another folder with a file called example dot SLN. I'll talk about that in just a moment. But I can either double click this dot SLN file to open up the solution inside of Visual Studio, like so. All right, or I could go to file open product solution and then navigate to that directory using the open project dialogue, if it'll let me. Okay. All right, and unfortunately, it's a little bit too large for the recording area, but then I would just simply select the solution that I wanted to open from this dialogue, and then and then click the open button. All right, let's go ahead and close that. And let's shut down this copy of Visual Studio. And I want to get back to where we were just a moment ago in our documents. Visual Studio in my case Visual Studio 2015 projects folder. And here are a list of all the solutions in our project folder. And so just kind of want to walk our way through this. This first folder here will contain our solution files. So there's this dot SLN file, which is a solution file. It contains information about all the projects that are under this umbrella solution. Okay. And we could actually open this up and look at it inside of notepad. And it's simply just a configuration file. There's nothing all that special about it. You certainly don't want to make any changes to it. But it's going to have information about all of the locations for the various projects that are associated with this solution. Any global settings and some of these things won't really be useful to us until we get deeper into our understanding of compilation and things of that nature. But inside of the solution folder is a second folder, which is actually going to contain the project files. And so here we have a hello world.csproj, which is the project file, the C sharp project file. And this file will contain, let's open that up as well with notepad. It'll contain references to things like all the files that are associated with this project and any of the settings and any other metadata. Again, information in here that you certainly don't want to edit and you don't want to accidentally make any changes to it whatsoever. But I just wanted to make you aware of it, that there's really nothing magical going on. There's just these configuration files that contain information about your project. And as you make settings on the project level, those will be saved inside of that CS project. And then finally there's this bin folder here and the word bin typically is short for binary, which denotes that this is where a binary version of your application will be stored. So the process of compilation, it takes your source code and which is human readable, and it's going to convert it into a format that is machine readable or understood by a machine, your computer. And so if we were to take a look inside of this folder, we would see that there is a debug folder. And so this folder is created for us whenever we started debugging our application. It creates a temporary version of our application for debugging purposes, which we'll talk about later. If we drill into that, you'll see that there is actually an executable file and several other helper files for the purpose of debugging. We'll talk about these later. If I were to double click the hello world.exe, it actually executes our application. And so compiling your code into a working application is the end goal. But I don't want to talk about compilation just too much yet or about creating a debug version versus a release version of your .NET assembly of your compile code. I think you're going to get a better appreciation for those ideas after we get past some more of the basics. So what I want to do is just stop our conversation about the VIN directory right now. We'll come back to that a little bit later. But you're doing great. Let's continue on. We'll start learning more C-Sharp now that we have some of these tangential topics out of the way. I'll see you in the next lesson. Thanks. Now in this lesson, I want to get back into talking about C-Sharp, the syntax. And we're going to talk about declaring variables, how to choose the right data type for your new variable, and then also how to initialize variables with values. So to begin, let's take a look on screen. If you've ever taken an algebra course, hopefully you've seen something like this where you're asked to solve for the value of X. And hopefully without a lot of thought, you're able to see that X equals 7, right? So using that same thought process, take a look at this little snippet of code on screen. X equals 7, Y equals X plus 3, and then we're going to do a console.writeLine with the value of Y. And hopefully you look at it for a moment. Using your existing knowledge of algebra and you think to yourself, then it's going to output the value of 10 to a console window, right? Exactly. And so my point is that C-Sharp, first of all, is human readable. It's got a few things that might be a little foreign to you like the semicolon at the end of the line. However, for the most part, I'm willing to bet that as we go through this series of lessons, you'll be able to understand what the code is doing for the most part, even before I explain it to you. So it's really not that hard. And then secondly, it's probably very similar to things that you've done in the past like working with math and algebra and things of that nature. And so if we're looking at the C-Sharp code, the X and the Y in this context are referred to as variables. And a variable is simply under the hood, a bucket, I guess you could call it, in the computer's memory. And you put things in buckets and you dump things out of buckets, right? And so we can put values into a given bucket in the computer's memory, and we can retrieve the value out of that bucket. We can even replace what's in the bucket with something different, right? And so that is what you use a variable for. And so this particular situation that we see on screen, these buckets are just holding numeric values. However, we could create buckets that are just the right size for almost any type of information, whether it be individual alphanumeric characters or strings of characters, strings of alphanumeric characters, like even entire sentences and paragraphs and even books. We could also create buckets that are just the right size for dates and times, buckets that are just the right size for really, really, really massive numbers, or even create buckets that are just the right size for numbers that have a lot of values after a decimal point. Now, in this case, what we would expect to see here is that these two buckets, the bucket that's labeled X and the bucket that's labeled Y, would hold numeric values because we want to add numeric values together, right? And so we know that, but how do we express that intent in C-sharp? The instructions that we write in C-sharp were ultimately, after a compilation step, they will ultimately be executed by the .NET runtime that we learned about in a previous lesson, and part of its responsibilities are to make sure to allocate memory for our variables in memory to hold the right kind of data. So here we have two data items, an X and a Y, and we have to tell the runtime that we want to allocate some space in memory that's sufficiently large enough to hold numeric data, like the type of data that we want to work with here and our application. But how do we do that? Well, that's the topic of this lesson. So to get started, what we want to do is create a new project. And here again, I'm going to go to File, New Project. We will go to the New Project dialog and make sure we select the Console Application Project Template. And here we're going to rename this and call this project Variables, and then click the OK button. And Visual Studio goes to Work, uses that template and creates a new solution with a project. And as you can see on screen, here we are back in our familiar program.cs. Obviously, we want to work inside of our static void main in between the opening and the closing curly brace, just like we learned about in our previous lesson. So before we get started, there's one big takeaway from this lesson, and that is that a variable is simply a bucket in memory that you can put data into and retrieve data out of. But we have to tell the compiler, we have to tell the .NET runtime what size of buckets that we want to create. So we have to declare our variables. We have to create those buckets and then give them some label that we can refer to them with from that point on. Now, before we get started here, typing some code, all the same rules apply in this video that apply previously. So you have to type the code exactly the way that I type it. Take the time, develop the skill of identifying differences between even small differences, a different in capitalization or in spacing or the various special punctuation marks that we use while we're writing code. Develop that skill to identify the differences between what I write on screen and what you're writing in your copy of Visual Studio. And if you see a little red squiggly line, you already know that there's going to be a problem there, right? So that gives you the clue necessary to go and focus either on that exact character or in that vicinity and use your detective skills to figure out what it is that went wrong. Okay, so now let's go ahead and we're going to create two buckets, two variables, and define them in such a way that they're going to hold numeric values. So we'll start with int x and int y, and it's as simple as that. So here to borrow the explanation that we used earlier, we are asking the .NET runtime to allocate space in our computer's memory sufficiently large enough to hold numeric values. Now we're asking it to create these two buckets, and eventually we're going to put values into them and read values out of them. But at this point, we're just declaring their existence and saying, here's what we need to work with. And then after we've declared that, after we've created them in this manner, then we can begin to work with them and assign values, retrieve values from them. But most importantly here, I'm telling the computer that I want to assign integer values into those variables. And an integer is really just a mathematical term that refers to a whole number that's within a certain range. So no values after the decimal point, and as far as C sharp is concerned, the values have to be between a negative 2 billion, 147 million, and a positive 2 billion, 147 million. That's the size of the bucket that we have to work with. If you need to work with much larger numbers, then the int data type is not the correct data type for you. There are other data types to choose from, and we'll learn about some of those a little bit later. If we needed to work with like money values where you have dollars and cents or pounds and pence, then the integer is not the right data type to work with. Let's continue in our application, and this is basically just to continue what we did in Notepad a few moments ago. So x equals 7, y equals x plus 3, and then we want to do a console.writeLine with the value of y. And then remember we want to do a console.readLine so we can actually see it on screen without it just flashing and going away immediately. So let's run the application and make sure we get the value that we're expecting. And hopefully you got the value 10 in your copy of Visual Studio just like I got in mine. If not, again, make sure you double check your work against mine. All right, so after we declared the variables in lines 13 and 14, then in lines 16 and 17, I'm doing assignment using the equal sign. Now in this case, we don't really call it the equal sign, we call it the assignment operator. We'll learn about operators in the next couple of lessons. This particular operator, the equal sign means take whatever is on the right hand side and assign that into whatever's on the left hand side. So we're going to say give me the value of 7 and assign that to our variable, our bucket named x. And the same thing would be true here with y. We're assigning a value into the bucket named y, but we have to do something interesting here. We have to actually retrieve the value of x from memory. So what was x again? Where's that bucket again? Oh, there's the bucket. Dump the value of the bucket out. You're holding the value 7. Add that to 3. And then we assign that to the value of y. Now here, we're retrieving the value of y saying, OK, give me the bucket with y in it. And you dump it out into the console.rightline, which we know, then we'll print that to stream. And that's essentially assignment and retrieval of variables. OK, so this is a very simple case. What I want to do now is comment out this code. If I were to begin commenting out the code like we learned about in a previous lesson, I could use the two forward slashes. I'm going to show you a different method in just a moment. But notice when I do that, something interesting happens. I commented out the declaration for the variable named x. When I do that, notice I get these little red squiggly lines underneath x's. And if I hold my mouse cursor there, it says the name x doesn't exist in the current context. And we might say, well, there it is right there. It's in our code comment. But remember, we're telling the C sharp compiler and ultimately the .NET runtime to ignore that instruction. So the compiler is looking at our code and saying, I have no idea what you're talking about x. I've never heard of x before. I don't know what you want me to do with x. And so it raises the red flag and say, I can't continue on under these conditions. You have to give me more information. Obviously, we can fix it by removing the code comments. All right. Now, what I want to do is comment out several lines of code. And instead of just doing two forward slashes in front of every line, which can be laborious, I'm going to comment out multiple lines at the same time using a forward slash and a star character over the number eight on your keyboard to begin a lengthy comment. And then right here before that read line, actually, let's go ahead and keep it all together. After that, I'm going to do a star forward slash. And so now we're going to type another code example beneath that. All right. This will be a little bit more interesting. Follow along. Pause the video if you need to catch up with me. I'm going to try and type fast just to save time. All right. And before I forget, let's go file, save all. Great. Now, let's begin here at the top. You can see that this is a different style application with some different commands or different uses of commands that we're familiar with. We're just going to play a little name game and we're going to ask what is your name and we output type your first name. Now notice in the first case, I'm using a right line, which will print what is your name to screen and then use a line feed character to go to the next line. However, I'm using yet a third method from the console object, the console class, which we'll talk about classes and methods later. But this method is different than right line. This will just write out the statement type your first name, whatever's in between our double quotes there. And it won't go to the next line. It'll just wait there on that line. And then what we're going to do is create a new variable using a different data type, a string data type. So we're not interested in individual alphanumeric characters. So A through Z, one through zero and the special characters. We're interested in a string of them or a collection of those characters. So not just the individual character B, the individual character O, and the individual character B, we're interested them as a string or a collection as Bob, B, O, B. So that's what we're declaring a bucket in the computer's memory sufficiently large enough to hold a string of characters, however long that it is. And then what I'm doing is calling our console.reline method that we're already familiar with, but there's a twist on this. Up to this point we said we're using the redline method in order to stop the execution of the application to wait for the user to hit the enter key on their keyboard then to resume. However, now we're using it for its real intent, which is to retrieve data from the end user. So in this case we're asking the question, what's your name? The user types it in and hits enter. And then whatever they typed in is assigned using the Simon operator to the variable we created called first name. Hopefully that makes sense. Now we're going to create a second variable of type string called my last name. We're going to do the same thing here, console.write, and then we're going to allow the user to type in their last name. And then whatever they type in when they hit enter on their, the enter key on their keyboard will be saved or assigned to the variable called my last name. Now that we've done assignment to my first name and my last name, I'm merely going to concatenate the values together. So let me point something out, there's several things we need to talk about here. Notice here we were doing actual math where we were adding values together. This is the arithmetic or arithmetic operator. We're basically adding things together, right? Here we're kind of adding things together, but that connotation is different. We're not adding hello and Bob and Tabor with some spaces in there from a mathematical perspective where concatenating strings of characters together to make one really long string. So it's the same operator, but it's used in two slightly different contexts. Kind of does the same thing, but we need to understand that there's a fundamental difference in how operators interact with different data types. You'll see why this is important as we continue on through this course. But at the very end here, we're expecting to see hello comma space Bob space. Notice that I have in here a additional double quote with a space in between to give some spacing between the first name and the last name. And then obviously my last name here, one more line of code to write because we need to do another read line so we can see the value on screen. Let's run the application and we have some things we want to talk about here. Okay, so what is your name type first name Bob enter type your last name Tabor enter hello Bob Tabor awesome. Okay, so very simple application, but hopefully now we're pushing the envelope a little bit more learning a little bit more about additional data types that we can use for our variables and learning about assignment that it works with all kinds of variables and then also learning about operators that work differently with different data types. Now before we get too far in the previous example we used merely X and Y which we might expect to see in some mathematical context because we're used to seeing those characters used in algebra problems. But whenever we start writing business applications or even games, we need to give our variables names that are meaningful inside of the program that we're writing. So I could have just used, you know, called this acts and then done something like this acts and then done something like this acts, and you look at this and you say I have no idea what X is supposed to do. All right. It's because we used a very vague description of the bucket in the computer's memory. Instead, you don't have to worry about keystrokes make it human readable. Write your code in such a way that somebody can read through it and understand exactly what the variables are doing and what the logic of the application is doing, and then also notice as I go and change some of these things back here to my first name. I used a little feature of Visual Studio that allowed me to say now that I have changed the name of X to first name, let me rename it everywhere that I've used the word X. Did you notice I did that? I hit the control on the period on the keyboard here. Let's do that one more time. I'm going to change this back to X. Notice that I get the little light bulb here off to the left hand side, which is quick actions, and then I hit control period on my keyboard. And now it gives me the option to rename my first name to X. And it even off to the right hand side shows me all of these changes. This is called a refactoring. I'm changing the code just ever so slightly by renaming things to give them more meaning. In this case, I'm doing the exact opposite, but we'll come back to that. So do I want to rename every time I use the variable name my first name to X? Yes. So let's rename everything. Bam. Just like that. All right. Let's rename it one more time. So my first name, control period, and now I want to rename everything from X to my first name. And you might look at that phrase, my first name, and then again, I used it down here, my last name, and you're thinking yourself, that's a crazy naming convention. Well, it's a naming convention that's called camel casing where you start with the first letter in a list of words that you're kind of munging together to describe a variable or something along those lines. Use a lower case for the first letter of the first word and then an uppercase letter for the second and subsequent words in that variable name. So ideally it makes it human readable. I can read it fairly easily that way. All right. All right. And at this point, I think it's important also to do something like this. I'm going to rename this to my first name, all lower case, and remember what we said in the previous video that C sharp is a case sensitive language. So if you were to use the wrong capitalization, then you're going to get a red squiggly line and it says my first name does not exist in the current context. Do you remember seeing that just a moment ago when we removed the declaration for for X up here when we commented it out? The same thing is true here. My first name all lower case is different from the bucket, the variable we define called my capital F capital name first name. All right. Capitalization matters. Make sure that you remember that. So in this case, let's just go ahead and change everything back correctly. And we should be good to go again. Okay, great. Now you might be saying yourself, well, this degree of precision seems pretty, pretty difficult. I mean, how am I going to remember exactly what I named things in the past? There are a couple of different tricks for them keeping your code methods small and we'll talk about that later. That's one way to do it. But then the other thing is to rely on IntelliSense, which is that little code window that I told you to ignore before. It's actually pretty important. So as I start typing my notice that it pops up beneath what I'm typing. The correct capitalization correct spelling for any of the variables that I've defined up to this point that start with the letters and why now at this point what I can do is simply hit the equal sign of my keyboard and it will type everything else out for me. So I don't have to worry about spelling. I don't have to worry about capitalization. And you may have noticed while I was typing, I was I was typing and then using arrow keys on my keyboard. And then you couldn't really see my fingers moving, but I wasn't typing every single keystroke. This is what allows software developers to write code very quickly. Once you get used to relying on IntelliSense, one of the tools that Visual Studio gives you in the text editor to make your typing more accurate and allow you to type much faster than maybe you normally could. Okay, we'll come back to IntelliSense later. All right. The other thing that I wanted to tell you about here or talk about is that we cannot define the same variable two times. So let's try this. I'm going to go and do my first name and say I want another bucket in the computer's memory with the same name my first name and the compiler says you can't do that. We've already got a bucket. We're going to confuse buckets in memory if we give two buckets the same name. Okay, so it says a local variable name my first name is already defined in this scope. You can't do that. Now we could do this, but I highly recommend you don't do that. Because again, my first name all lowercase is different than my first name with camel case. But this would cause a high degree of confusion. So never do that. Be descriptive with with your variable names. Don't repeat variable names. Always stick to a naming convention and never break that rule. And if you follow those little rules, I think you'll find some of these initial issues. They'll they'll just kind of dissipate. You won't have to worry about them. All right. Okay, so what I want to do now is take a look at this second set of code. And then not only are we in line number 2029 declaring the variable, but then in line number 31, we're actually giving it a value. What if we were to rewrite this little passage of code? So I'll go ahead and comment all of this out. And I'm going to make this smaller. In fact, here's what I'm going to do. String my last name equals console dot read line. And then I will say above that console dot write line type here. Okay, so you can see that I took these two lines of code 29 and 31 and I combined them together. And so what I'm doing here is not only declaring the variable, but then I'm initializing its value to whatever we retrieve when we call read line. So this is called initialization. And initialization is important because you want to give your variables of value as quickly as possible. This puts your variable into what's called a valid state, which will be an important idea as we learn to write applications, real applications. But also experienced developers like to write less code and they're always looking for a convenient way to reduce the number of keystrokes that they have to type and reduce the amount of code that they have to read. And so usually you want to declare your variables as you're using them and not declare them like some people used to do a long time ago, put them all at the very top of a given method or or section of code. And so you should get into the practice of two things. Declaring your variables as you need them in the body of your code. And then secondly, if you can give them and initialize value immediately after you declare them like we've done here in line number 34. Okay, so tell you what, let's stop right there. I think we've covered a lot of ground for one lesson, right? Let's do a quick recap and just talk about over a dozen things that we discussed. We talked about what a variable is. We talked about how to declare a variable, how to choose the correct data type. We talked about the int data type and the string data type. We talked about assigning values into variables and then retrieving values out of variables. We talked about the assignment operator. We looked at the arithmetic operator and also the string concatenation operator, which is both just the plus sign. We looked at console dot write versus console dot write line. We looked at the other life of the console dot read line method that we can actually retrieve the values that the user types in. We talked about camel casing and naming conventions for our variables. We looked at IntelliSense. We talked about how to rename things, how to refactor our code using the little quick action. Remember the little light bulb that we could make changes to by hitting control and period on our keyboard and then using our arrow keys to make selections and to rename all uses of our variable name throughout our entire code base. We probably talked about a lot more than that as well, but let's go ahead and wrap it up here and we'll start again in the next lesson. We'll see you there. Thanks. Hi, I'm Bob Tabor with Developer University. For more of my training videos for beginners, please visit me at devu.com. In this lesson, things are going to get a little bit more interesting. Based on user input, we're going to write logic to execute either one block of code or another block of code. When I use the term logic, I mean that we're going to make a decision to execute some code based on some condition that could be the user's input from the keyboard, maybe the state of the computer system itself, maybe some other data that we have access to or available to us, but somehow we're going to make a decision on whether to branch out and execute this code or execute this other code. So let's begin the way that we normally do by creating a new project and I'm going to go File, New, Project, make sure to choose a console application, C-sharp console application, and we'll call this project Decisions and click OK. And what I want to do is we're going to create a little game and we're going to do it right here in static void main and I'm going to go ahead and start typing. You can pause to catch up with me. Alrighty, so hopefully most of this will make sense. Let's go ahead and run the application. And here we're going to play Bob's Big Giveaway and we can choose a door. What's behind door one, two or three? I choose what's behind door number one and it said, hey, you want a new car? Awesome. Let's play again. I'd like to win something else. And so now we can type in the number two and, well, nothing really happens at all. I'm going to hit Enter again on my keyboard and the application just ends. We can try the same thing for three, but I suspect the same thing will happen with number two and I can just type something randomly and again, nothing really happens here. But let's start at the basics and talk about this if statement that we've created here, which is really the purpose of this lesson in the first place. The if statement is called the decision statement because we will decide whether to execute any of the code inside of this inner code block based on this evaluation that we're going to do after the if keyword. So in this case, what we're doing is evaluating whatever the user typed in and we're gathering that from the console.readline like we learned in the previous lesson. So the user typed something in the hit Enter. We got that now in the user value variable and we want to perform an evaluation to see if what the user typed in is equal to this literal string number one. All right. And so here's where hopefully I want to call your attention to this. You can see that I'm using two equal signs next to each other. So we already learned previously that a single equal sign is actually an assignment operator. We're assigning the value of whatever the user typed in in this case in the console.readline to the variable user value using this assignment operator. But whenever you use two equal signs next to each other, you're going to do an evaluation for true or false. So whatever's inside of this opening and closing parentheses, we're going to we're going to perform an evaluation is user value in fact equal to the number one or the rather the string character one, or is it not equal to the string character only be true or false. All right. And so based on that, if whatever the evaluation of that expression is, if it turns out to be true, then and only then will we perform the code defining the code block immediately after that if statement. If that's not true, if this turns out to be a false statement, then we'll just ignore whatever is inside of this code block and will continue the execution of our application to line 23 and then beyond. All right. So that's how it works. But I tell you what, this isn't a very interesting example, because obviously here we have we have no no prize for door two or door three. And then what happens if somebody is types in four, five, six or ASDF or whatever on the keyboard, we need to account for all of those scenarios in our application. So let me continue typing in some code here and you can again pause the video if you need to follow along, but we'll start by using else if statement right below our our code block for the if statement. Okay. So here we go. All right. Let's stop right there for the moment. And you can see that in order to evaluate additional conditions, I can use the else if statements. In fact, I have two of them here. So if this first evaluation is not true, then we'll continue on and do a second and a third and maybe a fourth and fifth. If this is true, then we'll no longer run any of these additional checks. We'll just continue on to line number 33. The same is true here. If this is not true, then we're going to evaluate this next expression. If this is true and we execute the code inside of the code block immediately following it, then we'll just go ahead and skip over this last else if statement and continue on to line number 33. All right. So if we were to run the application, let's go ahead and just quickly run through scenario number two. Okay. We want to vote and scenario number three, which obviously would allow us to win a cat, but then we still haven't accounted for the situation where we type in for or anything else like the word or just random letters on the keyboard. Nothing really happens in those situations. What we really need is what I would call a catch all case. And so to do that, we'll just create an else statement at the very end of our if else if construct. And so here what we'll do is just that string message equals. Sorry, we didn't understand and then console dot right line message. All right. So now we're catching every other case possible, no matter what the user types in. So let's go ahead and run the application. Again, I'll just type in some junk from the keyboard hit enter and says, sorry, we didn't understand. And we continue on. Okay. So that's how and if decision statement work. It also has these optional parts of the else if and the else statements for either additional, additional evaluations or what I call the catch all just in case none of the conditions are true. Okay. Now there's a couple of things about this and we're going to continue on to talk about one other type of decision operator that we can use a conditional operator. But before we do that, here's an opportunity to clean up our code. Let's let's look for areas where we've essentially got the same code repeated over and over and over again. And I can see a couple of instances where this is true. The first would be where we have this console dot right line message. You see we've repeated that in line 20, 25, 30 and 35 wouldn't be great to keep our code a little smaller. And only use that once at the very end of our evaluation, like put it right there outside of the if else, if else decision statements. All right. So let's go ahead and just remove those from there completely. All right. But when we do that, notice that I'm getting a red squiggly line under the word message. The name message doesn't exist in the current context. All right. So we're going to talk about scope and declaring variables inside of certain scopes and a little bit, but just to kind of lead up to that conversation. Essentially, when we defined a variable inside of an inner scope, it's not available outside of that scope. In other words, if we define a variable inside of a code block inside of the curly braces, it's only available inside of those curly braces, not available outside of those curly braces. So what we'll have to do is define that message variable outside of our if statements so that it's accessible to all of the inner code blocks as well as our console dot right line message here in line number 35. So it's a very simple fix. We'll just do this string message equals and then we'll just set it to an empty string to begin with. All right. So now you'll see a different message, a red squiggly line. But we've seen this before. You can't define the same variable twice, even if it's in an inner scope. So what we'll do is just say instead of defining the variable message there, we'll just set our existing string variable called message to the value like so. Okay. Great. So now our application should run. We've eliminated a lot of code and admittedly I created a straw man here. I wrote more code than I needed to, but I wanted to illustrate this point. So again, if we were to run the application, it works correctly, but there's one other change that we can make. Code blocks inside of as we're using them inside of if else if and else statements. If there's not more than one line of code to execute, then we don't even need to use the curly braces. In other words, since there's just one line of code here underneath my if statement, I can just remove it and make it small like that real compact. Same is true here. And same is true here. Now in this case, maybe just to kind of illustrate why you would need the open and closing curly braces. If I were to do this message equals message plus you lose so and I'll even add a little space here to make it look correct. Let's go ahead and test that that else case just real quick. And I'll type anything in there and hit enter and says, sorry, we didn't understand you lose. Notice that we were able to concatenate two strings together. I did it on two separate lines. Don't really need to. But if I were to attempt to remove the the opening and closing curly braces there, we're going to get a very different result when we run our application. So for example, if I hit three, notice that you want a new cat, you lose. Wait, what? How? Why did that happen? You want a new cat, but you lose? Well, well, there is no such thing as a free cat, but any rate. It's because what it's really seeing is this like that. All right. If we want these two to be evaluated together inside the same code block, we have to include them in the same code block. Otherwise, this being outside of that code block will execute no matter which of these if or else if conditions are true. All right. So let's go ahead and put that back in there to kind of illustrate that idea. In fact, if we were to do something like this, I can even make this a little bit smaller. Let's comment that out. And I'm going to show you a new operator, which is just this. Okay. So now instead of going message equals message plus you lose essentially, I'm saying, give me whatever's in the variable message concatenate on the word you lose and then assign that back into the variable message. That's what I did here. I just do it all in one step right there where I say whatever's in the variable message on the end of that concatenate you lose. So this is the assignment and concatenation operator kind of combined into one. Okay. Just a little shortcut there. All right. So now let's do this. Let's talk about another style of decision statements. It's actually an operator called the conditional operator. And this works well if you have an if or else kind of scenario and you don't have multiple items to evaluate evaluate like we did here. So I'm going to copy some code from lines 14 and 15. Whoops. Yeah. And line 16. Let's just copy all of those and we're going to paste them down below our commented area. All right. And then what I'm going to do is this this will do this a little bit different this time. Okay. There we go. Now I've written a lot of extra code that I need to I'm going to show you how to shorten this up in just a moment. All right. So here the key to this is this little evaluation that I'm doing online number 42. Remember that we're going to evaluate anything in between of the parentheses whenever we see the double equal sign. That means we're doing an evaluation is the user value that they typed in and submitted through the previous line of code line number 40. Is it equal to the literal value one? If that is true, if this equates to true, then what we want to do is is everything after the question mark. We're going to take that value and assign it to our new variable called message. All right. So if the user types in one, we'll find the word boat, the literal string boat and we'll assign it to the variable message. However, if this equates to false, so they type in something different than anything after the colon will be taken and assigned to the variable. Message and we'll use that below. So let's go ahead and run the application. We'll run it twice. We can choose a door and if they choose door number one, they want to boat. However, if we run the application a second time and we choose anything else, then you'll only win a strand of lint. Again, this is only useful in a if else condition, not when you have multiple conditions to evaluate. All right. Now let's address this last little part here because I can shorten this up considerably. Notice in order to get it all to print out on one line, I only use console.write. And so then I typed out the literal string and then I have a second line where I'm actually then printing out the actual message from line 42. And then finally to add a period at the end, I'm having to do yet another console.write statement in order to add a period. Okay, I can shorten all that up in one line of code. Watch how I do this. In fact, let's go ahead and just I'll comment out these lines individually like so. And then I'm going to use a replacement character inside of the console.write console.write line in order to shorten this up a little bit. So we use a write line instead. And for U1A, and then I'm going to use what we what I call a replacement code. So a replacement code of zero. And then after I give it that literal string, I'm going to use a comma and then give it the actual message variable that I want. Whatever is inside of this, I want it replaced in here. Okay, so eliminate that, that the curly braces with the zero and put the message variable value inside of there. So let's run the application again just to see this working. I'm going to type in number one and get the same result. All right. Now what if we wanted to expand on this idea here? Comment that out. And what if I wanted to replace two values inside of that console.write line string? So let's do something like this. You entered and zero. Therefore, U1A. And so in this case, after the comma, I'm going to enter the user value and then the message here. Let's make sure you can see that. Hopefully you can see that all on screen one time. So I need to do one little thing here is change that to a one. All right. So this says take the first item. So in software development, typically you don't start with the number one. You start with the number zero. I don't know. So we're going to see this pop up again and again. So the first item in the list will be at element zero. The second item in the list will always be at element one and so on. All right. So the very first item, this will be matched and replaced by the very first item in that common delimited list of input parameters. After the literal string, that little template that we created for ourselves. Then here we're going to do a second replacement and replace that with whatever's inside of the message variable. So when we run the application and we type in the number one said you entered one. Therefore you want to vote. All right. Okay. So that's enough for one lesson. Hopefully you've learned a couple of important things in this lesson. First of all, we talked about the if decision statement as well as the else if and the else and how to do a comparison and evaluation between two values to determine true or false. If we're using an if statement and we're doing that evaluation, then anything, the code that is in the code block below it will get executed. If that evaluation is true, if it's not, it'll either drop down to a second subsequent evaluation or even do a catch all in the else statement. We talked about using curly braces for our code block versus when you don't need them. We talked about keeping your code nice and tidy and small. We talked about declaring variables inside of scope and inner scope and outer scope as defined by our curly braces for code blocks. We talked about the conditional operators being able to all in one line do a check for true or false. And if it's true, assign one value versus a different value to a new variable. We talked about format codes inside of literal strings for our console dot right line and how to replace those replacement codes with with variable values that we then also pass in in our console dot right line statement. Like you see here at the very bottom in line number 49. So again, cover a lot of ground. Hopefully this all makes sense. If not, rewatch the video. Just watch those portions that didn't make sense. Make sure you're typing in the code yourself so that you can come to some of these epiphanies as you're typing. Okay. All right, we'll pick it up in the next lesson. See there. Thanks. Hi, I'm Bob Taber with Developer University. For more of my training videos for beginners, please visit me at devu.com. In this lesson, I want to spend a bit more time talking about some smaller syntax elements of the C sharp language that you need to master. To understand how a properly formed line of code is constructed in C sharp. And in one of the previous lessons, almost like the first lesson, I said something to the effect that just like you use a period or question mark or exclamation mark at the end of a sentence in English to complete a thought. You also use a semicolon at the end of a line of code in C sharp to denote a complete thought. And kind of to extend that analogy a little bit, I may have briefly referred to C sharp syntax as having nouns and verbs. So I want to elaborate on these sorts of things and clarify what I mean by that in this lesson. So I'm going to talk about the basic building blocks and I guess you could say parsing the parts of speech in C sharp. So let's start off in the beginning. Statements are what you call complete thoughts in C sharp, typically one line of code. Statement is made up of one or more expressions and expressions are made up of one or more operators and operands. So we've seen a number of statements and expressions and operators and operands already, whether you realized it or not. So as we're taking a look at some of the previous work that we've done here, I've got the variables project from a previous lesson opened up. And so for example, you can see that essentially each line of code is a statement. Each of them are made up of one or more expressions. So here, for example, is an example expression. This happens to be a variable declaration statement made up of a operator, which in this case is a keyword int for the for the data type integer and then an operand, in this case, a variable name. We also use another operator, the semicolon for the end of the line of code. Another example would be here where we have an assignment where we're actually calling a method. So here is an operand. It is the name of a class and we're using the open close parentheses. Remember, these are operators. This is the method invocation operator. Then we're using another operator here, the assignment operator to assign this expression, the value of this expression to another operand, the name of a variable that we created. So we were to look through the code, we could continue to kind of parse out and understand what makes up operands, operators, expressions, and then entire code statements. Now, operands are similar to nouns. They are things like objects and classes and variables and even literal values. These are the subject, I guess you could say, of your statement of code. And they're pretty easy to remember because typically you give them names, you define the values yourself and so on. Now, operators are similar to verbs. They are things like the addition operator or the string concatenation operator. These are things that act on the operands in order to perform some action. And typically you're going to use the built-in operators, although you could create your own operators, kind of a little bit of an advanced topic, but there are actually quite a few built-in operators and you're going to need to memorize many of them. You're going to need to know how you come to express yourself in C-sharp. Now fortunately as you start out, you probably only need to know a handful to be productive. And so what I want to do in this lesson is to focus on a few that I think you're going to use probably 90% of the time as you begin learning and you can obviously add to that list as we continue. So I'm going to actually present these in a rapid fire fashion. I've created a very nonsensical application. You can open this up, download it from wherever you're currently watching this video or wherever you originally downloaded it from. I called this project Operator's Expression Statements and the application itself does absolutely nothing meaningful at all. And so all it really does is kind of show you some examples of the various operators and expressions that you'll come across whenever you're working in C-sharp. So at the very outset here you can see that I have a variable declaration. We talked about this already. I did something a little bit different this time where I've declared several variables all upfront as integers. So x, y, and b are all defined as integers. Just wanted to show you something a little bit different there by separating them with commas. It's an easy way to just to declare several variables of the same type all on one line of code. Now I typically don't recommend this, but you might see this around in use in books and on the internet. All right, so next up assignment operator. We've already seen the equal sign at work in that capacity. Note here in line number 22 and following that there are actually many different mathematical operators. We're only looking at the most basic ones, but there are also some advanced ones as well. But here we have addition, subtraction, multiplication, and division. And as I demonstrate here, you can use parentheses to actually change the order of operations. So these are not in this particular situation. The parentheses is not a method invocation operator. It's actually a how you would typically use it in an algebraic or mathematical sense. You would use it in order to specify the order of operations. So perform this expression first, then this expression, and then take the results of those two and multiply them together in that third expression and then assign them to the value of x. And then there are operators that are used to evaluate. We've already talked about the equality operator where we're using two equal sign next to each other to make sure that these two items are in fact equal. Here again, we're using parentheses and yet a different capacity to define the boundaries of our expression that will either equate to true or false. So x equals y is either true or false. We can use greater than. We can use the less than operator. We can use greater than or equal to less than or equal to all of these again should produce a true or false result. There are also two conditional operators that can be used to expand or enhance an evaluation and they can be combined together multiple times as I say here in the comments. So I could ensure that both x is greater than y is true and a is greater than b is true by using the logical and operator. There's also an or operator to say either x has to be greater than y or a has to be greater than b in order for this outermost expression to be true. So here's the logical or two pipe characters next to each other. And then I guess we've already talked about that inline conditional operator where we have some item that's being evaluated and then if it's true then we'll take the first value and if it's false then we'll take the second value and in this case we're assigning either car or boat into this message variables value. And then also wanted to talk about member access and method invocation. We're going to talk about object-oriented programming quite a bit later on in this series of lessons. But we've already said how the console was a class and classes are containers for a lack of a more robust definition for methods and the way that you access a member method of a class or an object is by using the dot or the period. Furthermore we talked about the method invocation operator here. We are invoking a method called write line by using the opening and closing parentheses. And in this particular case we're passing in an input parameter. Again we want to hold off and talk about input parameters and methods a little bit later. But as you can see here's a number of different operators and these are just what I would call a very baseline set. You need to memorize these so that you can express the most basic of C sharp commands and understand exactly what it is that you're trying to do. It's not an exhaustive list by any stretch of the imagination but you will probably need these about 90, 95% of the time. And then you can expand your vocabulary of other operators and keywords over time. So in each of these cases that we just looked at an expression is made up of a combination of operands which are things like the literal strings and variables and objects like the console class itself. And operators. So things like the addition operator, string concatenation operator, equality assignment operators and so on. And you use expressions then to form complete thoughts statements in C sharp which are how the actions or the instructions of an application are expressed to the compiler and ultimately to the dot net run time which executes your application. So why am I telling you all of this? Why go through this little English lesson, parsing out the different parts of speech if you've ever had to take an English class? Well it will help you to understand why this is not a valid statement in C sharp. You can't just type X plus Y and then give it a end of line character and expect it to do anything. The C sharp compiler will look at that and say what are you trying to accomplish here? Have you lost your mind? What do you want me to do with all this? So fortunately in situations like these as you can see Visual Studio can catch these sorts of syntactical mistakes even before you attempt to run the application. And if we were to hover our mouse cursor over the visual guidance here, the red squiggly line, you can see that the fundamental problem with this line of code is that only assignment, call, increment, decrement and new object expressions can be used as a statement. So what's the problem here? Well this is not a properly formed statement. We're not assigning, calling, incrementing, decrementing or creating new object expressions. What? We're not formulating a complete thought a good sentence. I mean I could create an English sentence like this. The red ball, period. And you would say the red ball does what? Who has the red ball? Okay, we can understand that just because you use words doesn't mean you're creating a complete thought or expression inside of the English language. Same thing is true with C sharp. Alright, that's all I'm trying to say here. So for beginners, understand that there's a proper syntax just like there's a proper grammar in the English language. Understanding this is really a big step towards solving your own problems whenever you're phrasing C sharp instructions that the C sharp compiler will understand and accept and ultimately compile into code that will be run by the .NET framework. So here, let's recap what we talked about in this lesson. First of all, statements are complete instructions in C sharp. They consist of expressions. And a statement is like a sentence in the English language and expressions are composed of things like nouns and verbs. In other words, operators and operands. The operands are things like nouns. They're the subject or what we want to do something with. And then there are the operators which are more like the verbs. These will act on the nouns to perform some action. We said that operands are like variables and classes, literal strings. These are things that we get to name ourselves. They give the meaning to our application, whereas operators are for the most part built into the language and we have to memorize them. And so to start off, you might use something like what I've given you here in the form of a project for a cheat sheet. But I think you might just be able to walk your way through and rationalize your way through. Now that you understand that there's a proper way to format a line of code, you might say, okay, what do I need to do here? And you might be able to reason your way through the operands and the operators. I'm going to need a variable to contain some values. And so once I have created that variable in memory, now I'm going to need to assign it to something. Now, how am I going to get to that something? I'm going to need to take another variable and this literal value and I'm going to need to add them together with an operator. And hopefully you get the idea there. Okay, so I hope that this was a useful exercise. I think it's useful for beginners to understand that there are syntax rules and they're not so unlike what you're already familiar with. Maybe they look a little bit different than your typical English sentence, but they still have to make sense and they have to perform an action to do something. And so when you see errors, sometimes it's because you type something incorrectly and then sometimes you may not be using the right forms of speech in a sense in order to express that complete thought at C sharp. Okay, so let's pick it up in the next lesson. We'll see you there. Thanks. Hi, I'm Bob Tabor with Developer University. For more of my training videos for beginners, please visit me at devu.com. In this lesson, we're going to focus on iteration statements, a specific iteration statement called the for iteration statement. Sometimes you're going to need to loop or iterate through a sequence of things to find something that you're looking for to find a successful match. And actually you're going to do this sort of data manipulation more than you realize. So you'll have to trust me that this is a very important tool in your toolbox that you're building. So as you can see, I've already taken the liberty of creating our project. I called it for iteration. Pause the video, go through the steps that you already know how to perform to create a new project, new console window project and catch up with me. I'm going to begin to add some code here on line 13 in just a moment. Now the syntax that we're going to write here in just a moment is possibly the most cryptic of anything that you've seen yet. And I'm going to be completely honest. Sometimes I get things a little bit mixed up myself, but don't worry after we've struggled through it once or twice. I'm going to share the little secret that I use to get it perfect the first time every time. So having warned you about the complexity of the syntax, I'm still betting that you could figure it out and read it even before we actually attempt to run the application. Even before I take the time to explain what each little bit of the application is doing. So let me write it out here and then we'll try that. Okay, very simple, at least very compact section of code here. Actually, you need one more line of code, right? There we go. Now we're ready for action. So what do you think that this code does? Okay, got a theory in mind? Well, let's go ahead and run the application and see if your theory is true. All right, you can see that we have a list of numbers from zero to nine and then we can hit enter to continue on. So we're using C sharp to execute this little block of code right here until a certain condition is true. At which point we will stop executing that line of code and continue on to line number 17. So this four statement says that we should begin by declaring a variable. We're going to call it I, we could call it anything we want to and we're going to initialize its value to zero. Now as long as I is less than 10, we're going to continue to execute the code below it in our code block defined by our curly braces. Each time that we iterate through will increment the value of I by one. So this little bit right here is probably the part that you probably wouldn't completely understand unless I explain it to you. Remember how we use the plus equal sign in order to automatically take the value of message and add something to the end of it and then assign it back to the value of I. Remember that a couple lessons ago? We're essentially doing that here. This is the increment operator. So we're going to increment the value of I by one. So again, we're going to initialize and declare a variable and initialize the value. Then as long as this middle part is true, we'll execute the code below. And once we finish executing, then we will increment the value of I and then do that evaluation one more time. If it's still true, then we'll execute this code again. We'll increment I. If this is still true, we'll increment it again and so on. So that's how it works. Yes, this is cryptic syntax, but if you can just separate the three parts in your mind by remembering that there's semicolons that separate them, that can help. You know you're going to need to start off with some counter of some sort. You're going to need a condition and then you're going to need an incrementer at the end. And again, I'll show you a way to remember this so you never forget it. But before we do that, let me comment out this line of code and give you a variation on this idea. This would be fun. So here we go. So you can see that I simply added inside of our code block for the for iteration statement another if statement with its own code block inside of it. And here we're checking the current value of I. And once we find something we're looking for where I is equal to 7, then what? We'll perform this code. What does this code do? Well, this part's obvious, but this break statement may not be so obvious. You use the break to bust out of or break out of the for iteration. So we're going to make it to the value where I is 7 and then we're going to hit the break statement and then we'll continue on to line number 23. Let's see it in action. It's not going to look all that exciting, but I got an idea. All right, it found 7 and it pretty much finished, right? I have an idea. Why don't we watch this execute line by line? And to do that, we'll use the debugging tools inside of Visual Studio, which we have not even talked about up to this point. And yet is probably one of the most important features of using Visual Studio as opposed to just using a text editor and a command line compiler. So to make this work, what I'm going to do is actually set a break point here on this line of code. Now, how did I do that? I just went to this left most column and I clicked in the column in that gray column and it created a little red dot and off to the right hand side. You see that that whole line of code is outlined in red. Now, there are truth be told a number of different ways to set a break point. Probably the easiest way to do it is what I just showed you, but there's also keyboard shortcuts and there's menu options as well. So for example, with my blinking cursor on line number 16, I'll go to the debug window and I'll select toggle break point. If we look over to the right hand side, you can also see that the F9 key will accomplish the same thing. Great. Alright, so now let's go ahead and run the application and see what happens. Alright, so immediately the application pops up before anything can be printed out to the console window. Notice that we have paused the execution of our code and we're paused right here on this break point. Now at this point, what I can do is I can do a lot of cool things. First of all, I can see what my local variables values are currently. I can also change the value of those variables. I can monitor those values. I can change which line of code will get executed next and a bunch of other things. Now this is not a series on debugging. I can easily spend an hour showing you a lot of cool little features. However, what I do want to do is call your attention to this little window at the bottom. Currently right now, we're in what's called debug time. With the application execution pause on this line of code, the next line of code that's going to execute is that line right there that's highlighted in yellow. I can look at these little windows like this locals window, for example. You can see that the locals window will contain any variables that are currently in scope at the moment. Obviously, args is something that we haven't talked about yet. Let's ignore that one. What I want to focus on is the value of i. Currently, its value is zero. How do I know that? Because I'm looking here in the value column. I can also see what the data type of i is. It's an integer. If I were to hover my mouse cursor over i, you can see I'd be able to see it there as well. If I were to go and pin down that value, I'd be able to monitor it as well in this little helper window. In fact, I can kind of drag it around here. Now watch what happens. Let me readjust some things here. I'm going to step through this line of code. Now there's a couple of different ways I can step through the code. I'm going to recommend that we only talk about step over for right now. When we learn about methods, we can step into and step out of. But for right now, this middle button right here, the step over or the F10 key on your keyboard is what we want. I'm going to click it once and notice that we jumped from line number 16 to line number 22. Why was that? The reason was because i was not equal to 7. We didn't execute the code inside of the code blocks underneath the F statement. And we jumped to the end of the four code block. Now let's continue just to step through this. You're going to see that we'll increment i by one. Now when we do that, notice what happened. The value of i changed from 0 to 1 and that change is indicated by a change in color. Whenever you see the color red, that means something changed in the previous line execution. The value of that variable changed from some of the value now to the value of 1. We can also see this in this little mini window right here as well. How it is now turned to the color red. Now that we've incremented, we're going to do the next check to see is i still less than 10? And here we're going to step one more time into our program. Here we're going to step in the line 14 which opens up our code block and we'll do another assessment. Is i currently the value of 1 equal to 7? No. So we'll jump out of that if block and continue on. And we can just continue through this exercise until we reach where i is equal to 7. Now truth be told, I don't have to keep hitting the step over button. I can just use this continue button. And this will just keep bringing me right back to the break point. This basically says continue running until you hit another break point. And so here at this point now I see that my i is equal to 7 and that's what I'm checking for. So things should get interesting right now. I'm going to go back to start stepping line by line through my code. And here's where I hit the console dot right line. And if I look now on screen it actually did write that to the console window. And now I'm going to step through the next line of code. And notice that it jumped out from the break statement outside to line number 23 outside of the force statement to the console dot read line. We can hit continue from that point on. Our application is still running until we hit the enter key on the keyboard and then we've exited out. All right, very cool. Now you may have found it laborious to step through a number of times or even hit the continue button a number of times until we found just the right condition. So what we can do is make this break point into a conditional break point. So to do that I'm going to hover over the little red stop sign. I guess you could call it in the left most column and I'm going to click this settings. And here this will open up a little break point settings window right in line in my code. It pushed all the other code down. Notice that it goes from line 16 here to line 17 way below it. And I'm going to add a condition. And whenever this expression, a conditional expression is true, then we'll break at that point. So in our case when I is in fact equal to seven, then we'll we'll break on that break point. Okay, you can see that when I hit enter on my keyboard, it's saved. And now I can close this. You can see that the little icon change from just a red stop sign to having a white plus symbol inside of it. So now when we run the application, notice that I is seven and that I is seven our little window here before we even stopped into our break point. And now we can continue stepping line by line through our code and continue on right. And we got our result and we can continue on. Okay, great. So again, I could spend an entire hour just showing you other cool little features that will help you debug your applications. But understanding how to set a break point, how to run your application to a break point, how to step through line by line, and then how to resume at least resume temporarily or continue by using the continue button. Those are the key concepts in debugging. All right. Now let's go ahead and it'll turn off this break point from now on what I want to do is just eliminated. I can do that one of two ways to completely turn it off. I can just click on it'll go away or I can temporarily disable it by using this little icon that was next to the gear that we clicked earlier. And now you can see there's a little outline in the left most column and an outline around the line of code that had the break point. But no longer are we actually going to break on that line. Okay, great. So let's do this underneath the four statement from before. But above the console dot read line. I want to I want to do what I promised you the very outset, which will show you a way a foolproof way that you can get the syntax right for a four iteration statement and truth be told for just about anything else by using a little secret code snippets. Okay, it's not that much of a secret, but you probably didn't know about it. Did you? Okay. All right, so to do this. It's real easy. If you can remember I need a four iteration statement just type in the word for you'll see that it pops up in the IntelliSense. And if you look after the IntelliSense pops up a little message to the right there code snippet for for loop note tab twice to insert the four snippet. All right, let's do it. Let's do it. Tab tab. Bam. And there we go. And notice that it went ahead and pretty much set it all up. Although there are some parts that we're going to have to change, like for example, the length, but I could also change. I don't have to use the value I for my iteration statement as my placeholder is my counter or whatever you want to call it. I could call something like my value notice as I'm typing and then I hit the tab key on my keyboard. It changed everywhere that was using the I variable label to my value. All right, very cool. Hitting tab also took us to the next spot in our code that we're going to need to replace, which was the length. Or in other words, how many times should this for loop iterate? And I'm going to say we'll do it until my value is less than 12. Now here again, we can use a number of different quality or any quality operators here. We don't have to use the less than we could use the equal or we could use the greater than whatever makes sense for our application. But I keep it simple and leave it just like that. Once I'm done making changes, I can sit at the end of my keyboard. There were some highlighted areas that were highlighted in gold color kind of that indicate that these are replacement areas. And now my mouse cursor is right between the opening and closing curly braces. And at this point, then I can continue to create my application right line and then my value like so. And then we can run our application and we would get the following results. Okay, so just to recap, it was a short lesson, but we learned a lot. Not only did we talk about four iteration statements and why you might want them and we'll see them at use later. But we also talked about the debugging tools just briefly and how to step through our code, how to monitor the value of variables, how to use the break statement to bust out to break out of a iteration statement. We looked at code snippets and how to replace values in a code snippet in order to make it our own. So we'll use some of these techniques that we learned here throughout the rest of the series. Very important video. And so let's continue on the next lesson. We'll see you there. Thanks. Hi, I'm Bob Tabor with Developer University for more of my training videos for beginners. Please visit me at devu.com. In this lesson, we're going to talk about arrays and I'm going to start by making a case for why you need arrays in the first place. So often you're going to need to work with several related variable values. But how do you work with multiple variables and kind of treat them all as if they're part of the same group? Well, let me show you how not to do it. You can see an example of that on screen. First of all, I've taken the liberty of creating a project called understanding arrays. So make sure you catch up with me, create a new console window application and you can kind of just follow along. And type this part in. It's wrong anyway. Okay, so you can see what I've done here. I need to keep track of five numbers and I need these numbers to be related to each other. So without any better tools in my toolbox, I might just create something called number one, number two, number three, number four and give them each a value. And now I want to find which variable holds the value of 16. I'd love to be able to loop through them like we learned about previously to find which of the values hold number 16, but I can't really do that. I'm forced to create an if, else if, else if, else if, else if, else if, else if, kind of structure as you can see here below in order to find, ultimately find which variable has the value of 16 inside of it. So this is not the right way to go about working with multiple values that are somehow related and you want to treat them as a group. There's a better way and that as you might assume would be with arrays. So we comment all of this out. And, you know, previously I talked about a variable as being a bucket in the computer's memory that will hold some value. But let's, let's expand our thinking about this for just a moment and talk about an array. Think of an array as like a bucket or maybe even better a tackle box. Have you ever seen one of those if you go fishing, there's a lot of little compartments inside of it and each one of those little compartments can hold something. All right, usually little worm or whatever the case might be. What if we were to use that instead of a bucket? Whatever we were to put values in each of those little tray areas inside of the tackle box and store that up in memory. And then whenever we needed a value out of that tackle box, we just need to take it and look through and find the particular compartment with what we're looking for in order to work with it. That's kind of the idea of an array at least if you want to overextend the bucket analogy. So another way to think of an array is kind of a sequence of data, a collection of data. Although I'm hesitant to use those specific terms because they have very specific meanings in .NET. So think of it in a very general sense. You have a collection of data you want to keep together. How do you do it? Well, one of the ways you can do that is with an array. So let's do this and I'm going to go ahead and create my first array here. I want you to follow along and notice that I'm using square brackets. I'm not using curly braces when I'm working here. So first of all, let's take a look at the declaration of our array called numbers. It is an array of integers. In other words, there are going to be multiple integers all collected under the same umbrella named numbers. And you can see that not only am I creating the declaration for this array, I'm also using an equal new int five. Some of this, like the equal and the new part, we're going to talk about what that really means a little bit later, but for now just accept it as how you go about creating an array. And then notice next to that I have int and then inside of that the square brackets I have the number five. So that's how many elements that I want inside of my new array. I want a new array of integers that can hold five values, five integers inside of them. Next, what I do is I begin to access each element of the array and put a value inside of that element of the array. So here's the first element of the array, the second element of the array. Remember, we're zero based. Here's the fourth element of the array and the fifth element of the array, five elements inside of the array, just like we defined here in line number 31. Now what if we wanted to access the value inside of one of the elements of the array? Well, I would do something like this. So console di right line, obviously. Now what if I wanted to get to and print out the value that's in the second element of the array? Well, then I would use the correct index of the array to access that element. So here, here's numbers and I want the second element, which means I'm going to use the index one. So I'm going to index into that array to get to the correct element. So in this case, the second element is at index number one. All right, and I can print that off the screen and we do a redline here like so. And we can quickly run the application and you can see that we are praying to screen the number eight, which in fact is the second element of our array. Now the other thing that we can do is actually determine how many items are in the array by looking at the length property of the array itself. So console that right line and I'll just go numbers dot length. And so let's see what that will output. In fact, let me go ahead and comment that out and run the application. So you can see that we're able to programmatically determine how many items are in the array by using the length property of the array. All right, there's five elements inside the array. Great. Now what were to happen if we were to attempt to insert data into another item, a sixth element of the array? What do you suppose would happen here? Well, we'll try it and we'll run the application and you'll see that we get an exception and index out of range exception was unhandled. In other words, we are outside of the boundary of the space that we defined inside of our array. We're trying to access compartments that were never created in the computer's memory inside of our little tackle box. So in order to remedy this, we can either redefine our array at the time of declaration that we need actually six items or we can go ahead and we can change at runtime the number of items in our array. It's a little bit of an advanced topic. I don't want to talk about how you would go about doing that, but it is possible to do it programmatically at runtime. All right, so let's move on from there and let's talk about maybe a simpler approach to creating new arrays and that is to not only declare the array, but then also initialize its values at the time of declaration. So let me comment out everything I have here and we'll do this. Now instead of giving it a specific size, we're going to let the compiler figure it out on its own because we're just going to start typing in the values of the elements that should be stored inside of our array. Now in this case, I can create it or just put all the items in there I want to put in there and I can trust that the new array that will be created in memory will be able to hold all six items this time. Let me comment that out and we've been working specifically with integers, but what if we were to work with strings? How would we go about doing that? Well, same sort of idea here. In this case, we want to give it a number of literal strings like so. And so let me move this over a little bit. You can see that we are able to create an array of strings. We don't have to declare upfront how many elements we want in our new array. We'll let the compiler figure that out. It will create four items. Now there's a number of different ways that we can loop through to access each of the items in our array. Let me show you two ways and one of them is going to be what you're already familiar with the for loop, right? So I'm going to go for a tab tab. And so we'll start with an integer i equals zero. And now let's do this instead. Let's go names dot length, right? And then inside of here we'll go let's do this console dot right line and we'll go names. And then what do you suppose we'll put in the middle here? We'll use the value i, right? So what we're going to do is start at zero and continue iterating through until we reach the length of our array. And then we'll stop and jump out of our array. But until then we'll do a console dot realign here. And you can see that this will allow us to print out all four items inside of our array to the console window. Great. Now there's a lot of management of this i here, but there's an easier way to go about this. Let me comment this out real quick. I'm going to show you a second style of iteration statement. So in this case we'll just do this. We'll do for each and I'll go ahead and use the code snippet. I'll just go tab tab. So for each string name in names. Alright, and I've made up the term name as singular in names is actually what we called our our array, right? So now I'm going to hit enter on my keyboard twice. And I'll just do console dot write line name. And let's go console dot read line. What this will do is it'll allow us to essentially loop through every single name in our array of names. And for each item it will copy the current element into this temporary variable called name of type string. And then we can use that to do whatever we want. In this case we're just going to print it off the screen. It's not much easier that is. But we can use either technique in order to iterate through our sequence of data. Pretty neat. Okay. All right, now let me show you one last thing you can do. It'll be pretty powerful stuff. And we can create arrays of different things, right? So what if we wanted to create an array? What if we wanted to take a string and reverse the string? Okay, how would we go about taking, for example, the name Bob Tabor and reversing that to, what, Robatt Bob, I guess. How would I convert? How would I change that? Well, what we can do is take a string and convert it into an array of individual characters. Once we have an array of individual characters, we can then say, go ahead and reverse the order of those items so that the last becomes first and the first becomes last. All right. So let's do this. I'm going to create a string called Zig. And it's going to contain one of my favorite speakers quotes that I have kind of patterned my life after. You can get what you want out of life if you help enough other people get what they want. All right. That's a very, very long line of code. So what I would probably do is I would try to chop this up into multiple lines of code. And we said this before that you can do something like this and see sharp. All I'm doing is going to just break it in half and use this concatenation operator, right, to kind of marry the first string and the second string together. So that's all one really one line of code. So now that I have this, what I want to do is create a an array of characters. So I'm going to use the char keyword, which is the data type char, meaning I want one character. But I'm going to create an array of characters called char array. All right. And then take this Zig string, and I'm going to call a helper method on it called to char array. So every, every data type has some helper methods that are built into it by the dotnet framework. And what this will simply do is take a long string and we'll split it up into individual characters and put those into an array of characters. Now that we have our, our statement here in an array of individual characters, I can do something like this. I'll call array dot reverse. I'm going to pass in the character array. And then finally what I want to do is do a for each tab tab for each char. And I'll just call this a Zig Ziglar char in my char array console dot write not right line, but just write and the Zig char. All right, hopefully all this makes sense. Let's do a console dot read line. And this is just to show you some of the flexibility of working with arrays. Let's run the application. And now we were able to write that whole, that whole string backwards. All right. So that's pretty much it. There's a lot more that you can do with arrays. However, as we move through C sharp, you're going to find that your use of arrays will diminish over time, and you'll start using something a little bit more elegant. Think of it as an array on steroids or maybe like super array. Okay, it's going to be called a collection. There's a bunch of different types of collections and we'll learn about those near the very end of this series of lessons. But at any rate, that's how you work with arrays. Remember that you have to declare an array by giving it at the time of declaration its size. Then you can access individual elements of array by using indexes into the array to access or to set the values in a given element of an array. We can loop through elements of an array using a four or a four each iteration statement. And we can even use some cool utility methods like array dot reverse to swap all of the items in the array, or there's also ways to sort items and so on. Okay, so let's continue on the next lesson. We're doing great. See you there. Methods are going to help us with a number of different things as we write more interesting applications. Methods are going to help us organize our code better. They're going to eliminate duplicate code and the need to copy something we did earlier and paste it later in our code base. They're going to allow us then to take a certain feature or functionality in our application and give it a name and then call it by its name anywhere in our application. And then if we were to ever need to update or fix an issue with our method with that code that's encapsulated in a method, we did to do one place instead of changing it everywhere we copied and pasted our code. So remember what we said at the very outset of this course that a method is merely a block of code as defined by curly braces, and it has a name. And since it has a name, we can call it by its name in order to invoke that code defined in its code block. So methods are actually one of the most important building blocks that we're going to learn in this course, and it will allow us to build more interesting and complex applications. So this is definitely something that we need to understand thoroughly. So to begin, you'll notice that I've already created a project called simple method. Please take a moment, create a new console window project and catch up with me. And what I'm going to do is build the most simple example I can possibly imagine a simple Hello World application again, but this time using a method. And we're going to define our method, our helper method inside of our class program because remember, we're going to keep methods inside of the context of a class. Related methods kind of go together in the same class. We'll expand on that later, but it should be outside of the definition of our previous method, the static void main. So I'm going to go right to the end of the closing curly braces for static void main and I'm going to hit enter a couple times on my keyboard. That should put my mouse cursor after static void main definition, but before the end of our class programs closing curly brace. So somewhere in this area is where we want to work. We have to define things in the right place, just like we learned before. And here, let's create our first very simple helper method. Alright, and that's all it takes. Now I'll explain the word private when we talk about accessibility modifiers and classes. We'll talk about the word static much later in this course. However, just to let you know, it has more to do with building console window applications than typically what you might find yourself using in a different style of application. But we'll talk about it later. The void is something that's important. We'll talk about that in just a few moments here. I'm going to create a block of code, and then I'm going to give it a name. In this case, the name is simple Hello World. Additionally, I'm going to give it an opening and closing parentheses and we'll look at what those are used for here in just a moment. However, then in the body, I'm simply going to just write any of the code that I need my Hello World function to do. Now in this case, one line of code, very simple, but hopefully get the idea. Now, how do I call that method? How do I execute it from my static void name? Well, remember, it has a name, and we can call it by its name in order to invoke it. But remember, there's one other piece of information that that we need to provide here. Not only do we need to give it the name of the method that we want to invoke, but also we need to use the the method invocation operators, which are the opening and closing parentheses in this context. Alright, so now we've called our method and we expect output in the console window. Now I'm going to go ahead and add one more line of code, just so we can see our result. Like we always do. And now when we run our application, we will get the unexciting results Hello World. But the most important part of this was to create the simplest example we possibly could. And now that you see how easy it is to create a method and how easy it is to call the method, let's go ahead and shut down that project. And instead, what I want you to do is open up the project, and you should be able to find this where you're currently watching this video wherever you originally downloaded from there should be source code available. You should be able to find that source code in the before folder for lesson 10 copy that helper methods project folder into your projects directory or somewhere on your hard drive, and then you can open it up from there. So I've already got this opened up here, and you can see that I created a simple name game application. And again, this is simple, but at least there's more code that we can use to demonstrate how useful methods can be for us. So it's going to ask us for our name, and then where we were born. And then we're going to use the little algorithm, I guess you could call from the previous lesson, where we learn how to take a string, how to convert it into an array of characters. How to reverse the order of each of the characters in the array and then display it back out to the console window. And so that's what we have here in our results Oak Park Tabor Bob spelled backwards. Okay. Now in order to accomplish this, I have what third from lines 13 to lines 56 so about 43 lines of code. And admittedly, I made this longer than it probably needs to be but notice the amount of duplicate code that I've introduced into the application. Here's where I am retrieving the first name and the last name and the city. And those are essentially the same, even though what I'm collecting is a little bit different. But it's only two lines of codes that doesn't hurt much right here. Here we are actually taking the first name or the last name or the city and we're going to do the reverse operation on it. And we do that three times. And there's the third one. And then what we're going to do is print out the result into a into a string called result, which will then output in a console that right line. But notice here, we're essentially doing the same thing here and here and then again here. So there's a lot of duplication. Now duplicate code in and of itself is not a huge problem. I mean, there's really no way you can completely eliminate duplicate code in your application. But duplicate code is usually the result of copying and pasting code. So you've you've invented the wheel earlier in your code base. And your first thought is, well, I'll just copy and paste it because I need it here and here and here in my code. Now invariably what happens is your intent is to copy it, but to make a few subtle changes to it. And in your haste, frequently, at least if you're like me, you will forget and you'll make a mistake and forget to change something and you've introduced a bug. And it can steal your soul like even if it's just seconds but what if it's minutes or even hours of your time trying to figure out why you have a weird problem with your application. So copy and paste is dangerous. You should always treat it with great suspicion. But in addition to that, if you have the same code repeated multiple times, then whenever there's a change that's requested in how our application works, we're going to have to change it in multiple places. But what if we were to take some of this functionality like this, for example, and this, and we were to extract it out and put it into its own method and then just call it three times. First of all, it would reduce our need for copy and paste. If we needed to fix a problem with our code, we can do it in one place. And then also, if we were to give that method a meaningful name in our system, it would describe what we're attempting to accomplish. Right now we're just filtering through lines of code and it's a little bit more difficult to ascertain quickly what this application is attempting to do. But if we were to maybe give our methods nice meaningful names, it might read more like a paragraph of English instead of a bunch of disparate lines of C sharp code. So that's the goal. Now the second reason we might want to break this up into methods is to simplify the readability of the code. We already talked about making it more human readable. But also, there's a lot of lines of code here that we have to parse through to kind of understand what's going on. And if we can reduce the amount of code to read, then we can improve the readability of our code. We want to reduce bloat every time we have the opportunity. Alright, so we should strive to make our code readable, clean, clear, and perform well and maintainable so that we need to make a change. We can do it in one place and methods help us accomplish all of those things. So let's do this. Let's let's create a method. We've already learned how to do that. I'm going to go somewhere between the end of our static void main. But before the end of our program class, I'm going to define a private void, private static void, reverse string like so. And what I'll do is copy some of the work that we've done here, for example, lines 24 and 25. And I'll paste those here in our new method. And then I'm going to copy the code that we use to actually print all these out to screen. And I'll paste those here as well in our reverse string method. Now to be to get started here just to make sure that this method is going to work, I'm going to hard code the message. So I'm going to create string message equals hello world. And then I will change first name to just message throughout. All right. And first name array to message array, and we'll hit control period to rename, like we learned about before. And then finally, what I could do is gather up all of the individual items printed out in reverse order using this for each or I could just go here and go console dot right. Each item, like so. And that'll accomplish, at least for now, the same thing. Now that I have this working, I want to comment out everything I've done up to this point. So that I can kind of isolate and then we'll start reintroducing things back in as we as we get them working. So I'm going to call the reverse string method by using the name of the method and then also, again, method invocation operator, and then obviously the end of line character. And I'm going to go ahead and hit start. And not a very exciting example, but now we know that the logic of our method of our reverse string method is working. So what I'd really like to do is make this a reusable method. Currently, right now, it's all not all that useful. How many times do I need to print hello world in an application? But if I were to remove this line of code here and replace it with an input parameter so that the caller can pass in the string that it wants reversed. Now I improve the the usability of this method dramatically to create an input parameter. I need to give it a data type and then a moniker or a name. And so what I'll do is say I'm going to allow the caller to pass in a string and internally I'm going to call that string message. So I'm creating essentially a variable that allows an outside passage of code pass values into the body of my method. I can utilize that value inside of my method and then hopefully as a result of that achieve some more interesting results. Now having done that I've changed the signature of the method. I used to have just a method called reverse string but it accepted no input parameters. But now I have to accept one input parameter and that's not optional. So I get this red squiggly line beneath the reverse string. And if I were to hover my mouse cursor over it's going to say there's no argument given that corresponds to the required formal parameter message of. And you're like what does that mean? Essentially we did not call the method correctly now because we have to give it something like a hard coded string or probably the better thing to do here would be to give it the first name that we collect way up here in lines number 16 and 17. Let me uncomment that out and kind of go down here and comment. So now I'm collecting the first and last name in the city but everything else I'll leave commented out for now. Eventually we'll remove them. And I'm going to call this reverse string method three times each time I'm going to change what I'm passing in like so. Now when I run the application well let's do this as well. Let me copy that and I'm going to so that I can get similar results. Let's go ahead and remove that and let's save the application now. Make sure you have what I have on screen. Pause the video if you need to. Let's run the application and let's see it working and it should work similar to what we had before with fewer lines of code. And it mostly works but you notice there's a subtle problem with this. There's no space in between Oak Park, Tabor and Bob. And so this is a good example of where I can make a change one place in my code and it will fix the problem throughout the code base wherever I'm using and calling my new method. To fix this problem all I need to do is a console write and then add in a blank character that should allow a sufficient spacing in between each call to reverse string. So now when I run the application and I put in my details Tabor. It should work correctly and it does great. Okay, so now this is definitely one way to go about writing this application as I look at this method reverse string. I see a problem typically whenever I create methods I attempt to describe in English what that method is responsible for doing inside of my software system. In this case I would describe the functionality of this method as it reverses a string and it prints it to screen but herein lies the problem. I really only want each method to do one thing in my system and when I use the word and and print it to screen I feel like that's two responsibilities in the system. So typically what I would do is split this out into two separate methods and you might say well that's a little excessive and that's true in this simple case. But following that rule of thumb will help you as you begin to think about how to compose methods what goes into a method how many methods should I write. Should I just create one massive method or lots of tiny methods and typically the answer is more smaller methods with descriptive names are better. Okay, so in this case what we're going to do is change up the functionality of the application a little bit. What I'll do is take out all of this where I'm actually doing the writing to screen and what I want reverse string now to do is accept an input string and then return or report back giving the result to the caller. So in other words right now we're using the void keyword which means I want you to go off and do something but please don't report back to me. I don't care what you have to say. I don't need to know anything from you. You just go, you work, you be quiet when you end and everything is great. However we might want to change this and say instead of being quiet when you finish your job I want you to report back to me what the results were of what you did. So in this case I might want to say return back to me the reverse string. So I'm going to give you a string and then what I want you to return to me is a string that's been reversed. Alright, notice when I added or changed void to string I get a red squiggly line because I have not officially returned anything back to the caller. I need to use the return keyword like so. And I could do a for each and gather up each individual item into a longer string like we've done pretty much previously right here by building that result. However there is an easy way to do this with just one line of code. Just like there is a helper method called reverse on the array class there's also a string class and the string class has helper methods too. One of them is the concat method and it will allow us to pass in an array of individual characters and it will concatenate them all together and return back a full string. So in this case let's just give it the message array like so and that should work just fine. Okay, now notice that I'm able to call reverse string and I'm not really accepting back any values. Now why is that? I thought if we were going to say hey report back to me that I would need to do something with it. In other words I would expect to see something like this right where I'm capturing whatever has been returned from the reverse string method. That's optional. I can listen for it and retrieve it and save it or do something with it or I can ignore it. In this case what I would probably want to do is actually save it. So I would call this reversed first name like so and then string reversed last name equals and then string reversed city equals. We'll shorten this up in a moment but hopefully you'll see where I'm going with this. Alright and then what I can do is console dot write right line or just here let's just do write reversed first name plus space and this seems kind of laborious to do it this way. I've got a better idea. You know the string has another helper method. We looked at the concat method but it also has a format and the format will work a lot like console dot write line in fact they're almost identical. The only difference is console dot write line will print its results to screen whereas string dot format will merely just create a new string as a result of whatever been formatted. But the reason I'm using this is so that I can use the replacement codes like so. So here I go zero one and two and I can pass in reversed first name reversed last name or and reversed city like so. Since that is off to the right hand side of my screen I can't easily see it typically what I'll do is move each of the input parameters to the method. In this case console dot write I'll move in the separate lines to increase the readability. Okay and notice that they're indented a little bit but this is all essentially one line of code even though it's spread on four lines but it improves readability because I don't want to have to scroll off to the right hand side of the screen in order to read my work. So get in the habit of formatting your code for readability and keep things kind of narrow and small and if they do go off to the side of the screen. You know don't be afraid to move things down to different lines to increase the readability. Okay so now let's see what we have this should work. Let's run the application and it works great. What if I want to put this into its own method. I could simply do that like so and I think I can just use a void in this case. And what I could do is go display result like so. And I could just take this and paste it in but now what I need to do is pass in these three values right so how do I go about doing that. Well we know how to add one input parameter how do we add multiple input parameters. So what we'll do is define our first one reversed first name like so. And then to add subsequent input parameters I'll just use a common keyboard. And add the second one like so and then the third one like so. Alright and again since it's off to the right hand side of the screen I might put my mouse cursor right before these s and s. String and move those input parameters below the definition for our display result method again for readability sake. You may not agree you don't have to that's a stylistic choice. And so at this point I should be able then to call display result. So let's call display result passing in the reversed first name. And I just happened to use the same names here but I could have called either the input parameter something different or the the temporary variables here something different. Reverse string last name and you know as I'm doing this I'm beginning to think to myself why am I even going through all this why do I even need these variables. Couldn't I just eliminate those all together and just copy this paste it here I mean it returns a string right so I should be able to do that. And I should be able to do this. And I should be able to do this like so and then I will put them each on the little line. And that should work just fine and here I can eliminate these lines of code completely for my application. See if it works. Okay still works great. Feels like this should probably go into this display result so that should reduce it from being there. Now suppose that I don't want to pass in each of these individual values what if I want to display the result. And only pass in one value what could I do in that case well. I can provide additional ways of calling a method by creating what are called overloaded versions of our methods. And in this case what I'll simply do is I'll start out and copy and paste the exact same method definition twice notice on the second definition I get an error. Let me hover my mouse cursor over so you can see it type program so that's the class program already defines a member called display result with the same parameter types. So you can create additional versions of the same method with the same name but they have to have a different method signature a method signature is the number in the data type of the input parameters in your method definition. In this case I already have a method called display result with three strings I could change these names to just any old gobbledygook text there and I still get an error. It's the same problem because the fundamental fact that we've not changed the signature of the method means that I'm still having the same problem. However I could change this by allowing only a single message or a single string to be input as an input parameter and now I have two completely different versions of the method as far as C sharp is concerned. Now in this case I wouldn't need any of this I probably just do this like so and then I could I could call it by doing this basically what I was trying to avoid last time but we'll go ahead and do it anyway. Okay so this time we're passing in one long string notice the use of the concatenation operator and the use of some some empty spaces defined by two double quotes with just an empty space in between and so we should have two lines that display essentially the same thing. Here let's make sure we do this right. We'll add a right line in between them just to make sure there's a break. And so we get two results that look identical now you might wonder why are we doing this why in the world would you ever want to create two methods with the exact same name that essentially do the same thing but allow the user to pass in different information. And let me give you a good example of why you might want to do that is with the console dot right line. So here we go with console dot right line. Have you ever noticed as you as you type the opening parentheses for the method invocation operator that there's a little message that pops up down there that there's one of 19 and then you look to the to the right of it and as I use the arrow keys on my keyboard and go up and down. Notice that the number goes one two three four five. Okay, these are all the different data types that the right line method will accept so it'll accept an input parameter of type boolean which is true false. It'll accept a single character or an array of characters. It'll accept a decimal value which is usually used for money or a double which is used for longer mathematical calculations or a float which is a massive number in terms of the number of values after the decimal point. It allows you to pass in an integer and other types of integer style values. It allows you to pass in a string and then others as well 19 different versions of console dot right line to make it convenient. For the user of the application to to utilize that method in their app. Okay, for the developer, the application use it in their app. So now when we go to display result will see the same thing in IntelliSense display result and notice that I have two versions. I'm looking at version one up to and notice the emphasis on the input parameter that the first version accepts one input parameter of type mess strain called message. And then the second version accepts three input parameters of type string reverse string reverse last name and reverse city. Okay, so there you go. That is why you create overloaded versions of your methods. All right. Now in this case notice that we could eliminate so much of the code from this in order to essentially get the same working results. So I'll just delete that. And, you know, for the sake of simplicity, I'll go ahead and remove this as well. And so now we've reduced down the amount of code dramatically for our application and improved the flexibility of our application by adding multiple ways to actually display the results. Okay, so at developer university, I issue a decree to students that no method should have more than six lines of code in it. If it has more than six lines of code in it, then it's probably attempting to do too much in the system. You should be able again to express what it's doing in English. And then if you find yourself saying it does this and that, then it's probably an opportunity to split those up into multiple methods. Of course, rules are meant to be broken and as a rule of thumb, six lines of code per method will keep your code tidy and readable. It'll keep everything scoped nice and very tightly and it'll improve the quality of your code dramatically. Okay, so that's all I wanted to say about methods, but we're going to be using them from this point on. So if there's anything about this that doesn't make a whole lot of sense to you, by all means, please make sure that you watch this lesson again or seek out some other resources. Okay, you're doing great. Let's continue on. We'll see you in the next video. Thank you. Hi, I'm Bob Tabor with developer university. For more training videos for beginners, please visit me at devu.com. In this lesson, we're going to look at another iteration statement, the while iteration statement. And let's just recap the iteration statements we've learned about up to this point. We learned about the for loop or the for iteration statement and it allowed us to iterate through a block of code, a number of preset times based on a counter. Then we also learned about the for each iteration statement that allowed us to iterate through a block of code once per item in an array. Now, in both of these cases, you know ahead of time how many iterations or how many times to iterate through the given block of code. But what if you didn't know upfront how many times that you needed to iterate? Maybe you need to keep iterating until some condition is met. In that case, you'll want to use the while iteration statement. Also, we'll take a look at the do while iteration statement, which allows us to always iterate at least one time before breaking out of the iteration statement. So we'll look at both of them in this lesson. And I'm trying to think of use cases where this would be useful. And the most obvious one to me was creating some sort of little menu system for our console window application. You've seen them before, especially if you've worked with DOS in the past. At any rate, what we want to do is begin with a new project. You can see I've already created it. It's called while iteration. Again, another console window application. Please pause the video. Catch up with me when you're ready. Let's go ahead and get started by creating a method that will print out a list of options to our users in the form of a menu. So we'll do something like this. All right. So we have some more work to do here. But what we want to do is display this. So let's just start by displaying the main menu here. Like so. And let's run the application. And here we can choose an option. No matter what we choose at this point, our display will disappear. But suppose that we wanted to actually kick off another feature of our application. So say, for example, let's go private static void print numbers. And then we'll go private static void. This and it will just do a console that right. And so now let's go ahead and call those from here. So numbers and then guessing. So now let's run the application. And we choose the first option and we're able to play the print numbers game. But when I hit enter, we are completely removed from the application. What if I wanted to return back to that main menu? How could I go about that? Well, I could use a while statement to determine whether to show the menu again or to completely exit out of the application. So to make this work, what I'm going to do start off with a new data type called bull. We'll refer to it briefly a moment ago. It's basically true or false. And so we want to create a new Boolean variable called display menu. And we'll set its initial value equal to true. Now what we'll do is create a while statement. Let's type in while tab tab. And what I'll say is while the display menu equals true. All right, and then we will call display menu. Now a couple of things here, what we'll need to do is actually then retrieve back from main menu a Boolean, whether the user clicked exit or not. So what we'll set is display menu equals main menu and then have main menu return a bull itself. Of course, we've completely broken the application at this point. That's okay. So here we are going to continue to display the main menu until main menu returns the value false. So if somebody chooses option number three to exit, then we might choose to completely exit the application in which case will return false. Now if they choose some other option like four or five, six or some other text option, then we might just want to redisplay the menu. So we'll return true again. Furthermore, we might want to return true as well here after we go through each of these options as well. So now let's go ahead and run the application and see how it works this time. All right. First of all, if we choose option number one, it'll display a message and after we hit the enter key in the keyboard, it will display the menu again. And I can select number two and it will display the message and then I can hit enter. And now we can hit the exit and we actually exit out of the application. So what the while statement allowed us to do in this case is check for a condition and when that condition is true, then we can break out of the while loop. Otherwise, we're going to keep executing the code inside of our code block. All right. Now what we can do here is actually shorten this up a little bit. We don't need to say while display menu equals true. Remember that we're just like when we're using the if statement or the else if we want to evaluate an expression and if an expression is true, then we want to either execute that block of code below it or not. In this case, if display menu is already true or if it's false, we don't need to actually do this evaluation that already evaluates to true or false. So we don't have to do any equality there or check for equality. It's either true or it's false. And so we can just write it like that very simply. All right. So moving on. Now what we want to do is kind of maybe fill in the gap on some of these other little games we have here. So let's play the print the numbers game. And in order to do that, let's let's go ahead and say console right type of number and then result equals console dot read line. All right. And that's going to return back a string. But what we really want is an integer. So I'm going to go int dot integer dot parse and this will allow us to take whatever string has been returned and convert it into an integer. Now we should have the actual integer value. And here what we'll do is create a counter for ourselves. So in counter equals one and then we'll go while tab tab. The counter is less than our result. Then we will do a console dot write with the current value counter. We'll go console dot write and we'll do a little delimiter and then we'll do will increase the counter. Now there's a tiny bug with the application. We will come back to that in just a moment here. Let's go ahead and run the application. And let's go ahead and type in a number. Let's type in the number five and it types in and it will print out the numbers one, two, three, four. So we're able to change the number of times on the fly that will iterate through a block of code. Now it just so happens that this isn't exactly what we wanted. And let me exit out of this. What we really wanted was to display from one to five. So I'm going to go ahead and add result plus one. So if I typed in the number five, this would actually make this value six. So as long as we're less than six, go ahead and continue to execute these lines of code. But once this statement becomes false, once the counter is no longer less than six, if it's equal to six, it will break out and we'll hit this line of code here in line number 59, the console dot read line. So that should work. Now the other thing that I noticed when we ran the application is that we keep seeing additional data being written to the window. I might want to clear out everything that's been displayed so far. So here we'll start at the top and do console dot clear and that should clear off the screen for us. And I'll just copy and paste that here to print numbers as well. So when we run the application again, this time I'm going to choose option number one. And notice that it cleared off the screen and we're in the print numbers game. I'm going to type in number, type the number, let's go four. It types out one, two, three, four. I hit enter. It clears that off and it displays the menu again. Awesome. Three. Okay. So the next thing we want to do is play the guessing game. And again here, I'm going to go ahead and clear off everything that's currently on the screen. And what I want to do is choose a random number. And then I'm going to allow the end user who's playing the game to try and guess the number between one and ten. So how do I create a random number in C sharp? We actually use this built in class in the dot net framework class library called the random class. So we'll create a new instance of the random class and we'll talk about what that means. Create an instance of a class in an upcoming lesson. So let me do this. We'll go random, my random equals new random. And again, that should make no sense to you whatsoever. And that's just fine. I'll explain what that actually did in an upcoming lesson when we talk about classes. Okay. And I want to get a random number from my random class. So I'm going to call the next method. And here one of the overloaded versions is that I get to give it a minimum value and a maximum value. So the minimum value will be one, but the maximum value I want it to be 10. So I'm going to say, don't let it be more than 10. In other words, 11 is out of bounds. Okay. Now that I have a random number, I'm going to also keep track of how many guesses the player has guessed up to this point. All right. Then I want to also keep track of whether or not the user was correct or not. So incorrect. And we're going to say it is true that they were incorrect. Now watch this, I'm going to create a do while statement. I want the block of code that I'm going to create to execute at least one time. So that's why I'm going to choose the do as opposed to the do while as opposed to the while. The while will evaluate the very first time. And we may never actually run the code inside of our code block. But this time I wanted to run it at least one time. So we'll say do this. But then at the very end, we'll check for the statement while and if the while condition is true, then we can break out of it. Okay. So while we continue to be incorrect is true. So while we continue to be incorrect, then we're going to keep guessing. All right. So let's start this console dot right line. And we'll say a guess a number between one and 10. All right. And so we want to retrieve that number. So we'll go a string result equals console dot read line. So. All right. Now that we have it, we can do an evaluation. The result is equal to the random number. So what if whatever the user typed in is equal to the random number that we generated, then we want to break out of the while statement. So we're no longer incorrect. So in other words, let's go ahead and set the incorrect equal to false. So at this point, we correct, we guess correctly and we'll break out of the while statement. And here we would want to say console dot right line. Hey, you did it correct. All right. However, if they did not guess correctly, then what we would want to do is write console dot right line and then wrong. And we probably want them to guess again, which will happen because while incorrect incorrect is still true, then we'll come and we'll re execute this this block of code. Looks like I'm missing a end of line character here. I can see that as I hover my mouse cursor over that little red area that I forgot my I forgot a semi colon there. Otherwise this should work. Now there's one other thing that I want to do. I want to keep track of the number of guesses. So each time the user adds a guess, we've already initialized that value there that variable guesses. I'm going to increase guesses or increment guesses by one. So I type in the word guesses plus plus that means I want to add one to the current value of guesses. And then here I want to type out how many times it took. So it took guesses. It took you guesses. And then like so. All right. Let's run the application. And let's choose to guess a number between one and ten. We'll start off at three. And you can see it says it's wrong. So I could continue to guess a number between one and ten. Let's go four, five, six, seven, eight. Okay. The number was eight. It took me six guesses to get to that. All right. So now when I hit enter, it returns me back to my main menu. And here I'll just hit three to exit. And let's go ahead and change our menu at this point. Let's just do print numbers. And then the guessing game. All right. And so we've used the while statement and a couple of capacities. The while statement here is used so that we can continue to display the menu until the user decides to exit. We are using it to merely print out values to a screen. But we get to determine it at runtime or let the user determine it at runtime as opposed to the four or the four each where it's kind of predetermined ahead of time. And then finally we're able to use the do while to continue to ask a series of questions until we get a satisfactory answer at which point then we can break out of the loop. The do variation allows us to run our code block at least one time as opposed to immediately jumping outside of the block if the condition is true. So that's why we would use the while iteration statement. It's pretty useful in certain cases. And so let's continue on. We'll learn about strings in the next lesson. We'll see you there. Thanks. Hi, I'm Bob Taber with Developer University. For more of my training videos for beginners, please visit me at devview.com. Now many of the types of applications that you'll build as a C sharp developer will require you to work with text. Whether you're formatting the text for display to the end user or whether you're manipulating the text in some way. A good example would be whenever you are massaging data. And that's a term that developers use to talk about taking data from a file or a database and it's in some raw form. And you need to manipulate it. You need to remove certain characters. You need to add certain characters in certain positions. You want to get it and prepare it for ingestion to be used by some other software system or to be saved in a different file format, whatever the case might be. So manipulating data is a key skill, whether for display or for the sake of massaging data into the right format. Furthermore, whenever you're working with the string data type, you're working with a data type that can hold a lot of information. So to extend the bucket analogy, you're working with a really big bucket. And when you're working with big buckets, you have the responsibility of working with them in an efficient way because when you're working with data that takes up a lot of memory, it requires a lot of processing power. You are putting a strain on system resources. Now, admittedly, it would take a lot of string manipulation to slow down a computer, especially a modern computer. However, as software developers, we want to do things efficiently and so it's important to understand that there are tools in the .NET Framework class library that will help us work with and manipulate strings in a very efficient way. So that's really the purpose of this lesson, to show you how to perform some simple string manipulations like inserting special characters into your literal strings, formatting strings, especially numbers and dates and things of that nature, manipulating strings, changing things about strings, searching for items and removing them or replacing them with something else in strings, and then also working with strings in a more efficient way. So as you can see, I've already taken the liberty of setting up a new console window project called working with strings. Please take a moment, pause the video and catch it with me. And I've already added three lines of code that we'll use to demonstrate some key manipulations for our strings. And so what I'll wind up doing is just typing in a string and then showing you some manipulation and then moving on to the next line. But at any rate, let's go ahead and start by talking about the special nature of the backslash character, which is that character there. I always used to get my characters confused. That's forward slash, that's backslash. So a backslash character can be used to escape or insert escape sequences into literal strings. This will allow us to do things like put special characters, insert line feeds and things into a literal string. So for a good example of this, what if I wanted to type something ironic like my so called life, right? And I wanted to insert a series of double quotes around the word so called so that it displays the way that I would as the author of this expected to be displayed on screen. Now unfortunately you can see that the Visual Studio on behalf of the C sharp compiler doesn't like this at all. It thinks that you have two literal strings here, the word my in life, and in between something that it can make no sense of whatsoever, the word so or the term so, a minus symbol, and then the word called. These are not variables that have been declared. It doesn't recognize them as keywords. So C sharp does not like this. In order to insert a special character like a double quote to say I don't want this to delineate a literal string, I want to use this inside of my literal string. I'll use the backslash character before each double quote, which escapes out the double quote and makes it available for use inside of the literal string itself. So now when we run the application, we can get double quotes inside of our side of our string. Okay. Now similarly, let's go ahead and my string equals and I tell you what, I'm just going to copy this to my clipboard so I can keep using it. All right, so now what if I needed to add a new line? So what if I need a new line? And I want to split this up into onto two separate lines in my application. What I can do is insert a new line character. So think of a line feed slash N will create a line feed. And let's go ahead and run the application and you can see that it's smart enough to know that even though we didn't separate with spaces around the word and new, it was still able to find that that escape character for the for the line feed and represent it correctly in our literal string. All right. Now you might say, well, that's all well and good. But what if I need to actually use the backslash character? So for example, in an instruction to go to your C colon slash drive, and you'll notice that we get a red squiggly line underneath the backslash because it's expecting us to use the backslash as an escape character as an escape sequence. But we've given it nothing after that to to indicate which escape sequence we want to use. So in this case, we have two options. In fact, in all these cases, we have two options. We can use another backslash character to escape out of it to represent this correctly. So now you can see that you should go to your C colon backslash drive, or what we can do, and I'll just do this again, go to your C drive. So what we can do is add a at symbol in front of the literal string. And that tells C sharp that we want to use our backslash characters as true backslash characters, not as not as escape sequences, all right, or special characters. All right, so let's move on from there. And we've already talked about the use of string dot format. And we showed how we could do something like this, where we are going to insert the words first and second into this template, and the template contains a number of replacement codes. And the number inside the replacement code corresponds to which argument is passed in to the string dot format as input parameters. So let's run the application and we would get what you might expect first equals second. What I didn't tell you at the time was that you can actually reuse the replacement code multiple times, like so, or you can use them in a different order, if you like, whoops, let's go back and change up the order, where the second will be the first item displayed and the first will be the second item displayed, like so, all right. Furthermore, the replacement code has some special powers. And for example, if we want to do a string dot format and say for example that we wanted to display currency to the end user, so I want to display $123 and 45 cents to my end user. Now, in my case, since my computer's culture is set to English US, this will be represented as dollars and cents. But if your culture is, if your country and culture codes are set to, for example, English UK or some other language and some other culture, you would probably see something different whenever you choose to do this. You'll see your native country and cultures symbols for currency. But to create and format values for currency, you use the colon and then see immediately after the numeric replacement code. So in this case, I'm using, say, zero still represents the first item in the list, but the colon C says I want you to format it like currency, all right. So that when we run it, at least on my computer, you'll see dollars and cents, okay, with the dollar symbol. Now, there's all sorts of these little, these little variations on this. So for example, what if I wanted to just display a really long number to an end user, like 123456790? And I want to, I want it to look like a number, not just like how I have it here, where you can't really tell is that what 12 billion or 123 million or what to refuse to remove the confusion, what you can do is use the colon and format character. And this will add in decimal points and commas to give you the appropriate formatting for a large number. So 1 billion 234 million and so on. Okay. All right. Furthermore, continuing that same thought, what if we were to go string format, and we wanted to represent a value as a percentage. So what if I wanted to display this as a, whoops, as a percent? Make sure you get those formatting codes in there. And just to show, there's nothing on my sleeve here. Let's so this is called this percentage like so. And then we will insert a percentage here at this replacement code. Let's go ahead and run the application. And you can see that the percentage is 12.3% in this case. And finally, the last one I'm going to show you, but I'll show you where you can find more information is how to create a custom format. So for example, in the United States, phone numbers have a very specific way that they're presented to presented. So let's go string dot format. And let's go 12345679 and zero. And I want that displayed like a phone number. So I would use zero. And then I'm going to use pound symbols to represent each digit that I want formatted. So in this case, actually, I'm going to use parentheses around the first three numbers because that's usually how an area code in a phone number in the United States is presented. Then a space, then three more numbers, then a dash, then four more numbers. Alright, that's just how phone numbers are presented in the US. So let's go ahead and run the application now. You can see that it in fact formats that number the way that I would expect. Now let me throw one little monkey wrench in this. What if I were to supply too many extra digits? I added another one too at the very end. And yet I don't have that accounted for in my formatting. Where will it be presented? Well, as you can see, it pushes out the area code to five digits instead of just three. So the moral of the story there is that the formatting will go from right to left whenever you're using custom pound symbols to create a custom format for a numeric value. So again, the numbers will push their way out. And once you get to the very end, it will just put as many numbers as it can on that very first character and push that formatting out to the left. So just be aware of that. Okay. So the next thing that we want to do is start manipulating strings in a more meaningful way up to this point, we've just been formatting strings. But what if I want to actually change some things about the strings themselves? Let me start by providing a little something that we can sink our teeth into and work with. And so I'm going to type in a lyric from a song that I like. Alright, and notice that I added or I left in an extra space here at the very beginning of the string and then I left in two spaces at the very end of that string. Alright, and so let's go my string equals my string. And I think the most important thing you want to realize about when you're working with these data types is that they do have built in functionality that were provided to us by Microsoft. So for example, every string has this substring helper method that we can use to just say, hey, I want to start at a specific point and then grab all of the characters within a given range. So I can say started position six and grab me back everything after position six. So when I run the application, you can see it starts with the word summer, which is at the sixth position and grabs everything to the very end of that that line. So here is position one, two, three, four, five and six. So it truncates off the first six characters and starts me there and I pulling everything else, giving me a subset of the strings from that point on. So I can also say, go ahead and give me just the next 14 characters after that. So don't give me everything to the very end of that string, just give me the next 14 characters and so I can isolate just a couple of characters, in this case just three words in that string. I can also do something like my string dot to upper and that will do just what you might think it will. It'll make everything uppercase. Great. What if I wanted to replace one character with a different character, so my string dot replace and say find every blank space and replace it with a double dash, like so. So now when we run the application and see that we get double dashes instead of our spaces, it makes it more obvious that we had some spaces at the beginning of the end. We can also equals my string dot remove and we can remove a number of characters from our string. So instead of just selecting out the substring of characters, we took threes, we can actually remove those entirely from the string. And you can see or I'm sorry summer we took it has been removed from the string completely. And also what if we were to want to actually remove those trailing and and proceeding spaces, we could use the trim method. So let's do my string equals first of all string dot format. And here I'm going to grab the length of the string just to kind of demonstrate this the before length. So and then the after length. And so let's go my string dot length. And then my string dot will call the trim method to strip off all of the all the extra spaces in the beginning and the end, or I could just choose to trim off only the ending spaces or just the beginning spaces. But I'm going to call the trim method to get rid of it all and then determine what the length of the string is at that point. So you recall that we use the length property whenever we were working with the array to find out how many items were in the array. We can also use the length property on a string to tell us how long the string is. So that's ultimately what we're doing here. Tell me how long the string is before we make any changes to it. And then after we trim off those extra spaces, how long is the string itself? So run the application again. And so you can see that the before was 46. The after was 43. We trimmed off three spaces. Great. All right. The last thing I want to do is talk about the working with strings in a more efficient sort of way. And for example, let me just type in a really quick code example here. So we'll just do this. So actually here, my string plus equals. Okay. And hopefully you remember what this operator was for, where we're saying, give me whatever the value of a string is and concatenate everything on the right hand side to it. So here we're concatenating on just double dashes and the current value of I as we loop through 100 times. And we'll merely then just display my string in the console window. Whoops. And let's go ahead and actually get rid of that. And we'll start here with a blank slate. Alright, so let's run the application. And the output isn't all that interesting. It's just a print out of numbers with some dashes in there. But what's going on behind the scenes is the more interesting part of this. What happens when you're working with the string data type is that it's called a immutable data type, meaning that you can't just add more values to it. What happens behind the scenes is there's this little dance at the dot net framework runtime is is performing to make it look like you're still working with the original variable my string. So the original bucket. But what it does is it creates a second bucket and it starts copying things over in this case it copies the previous value of my string. Plus any of the new stuff we want to put in there and it creates this new string in a new bucket and it removes the old bucket and it gives the new bucket the name my string. Then we say let's do it again. In fact, let's do it 100 times and it has to go through that dance 100 times in order to produce the final result that we're printing then in our console window. And as you can you can see that's a very inefficient way and we're requiring a lot of the runtime a lot of memory management that's that might put a drain on the system if we were to do a lot of it. So instead what we can use is a different a different data type. Whenever we're going to manipulate strings in this way where we're going to do a lot of string concatenation or a lot of string manipulation. We can use something called a string builder. So again, just like I said with the random class from the previous video, this may not make a whole lot of sense at first. But hopefully once I talk about what classes are and how to create new instances of classes, this nomenclature that string string builder my string equals new string builder. What is that all doing? We'll talk about that very soon. But just let's create a new string builder class. And then we're going to do something very similar to what we did before where we will iterate through 100 times. But this time instead of just doing a simple concatenation, we're going to use an append method, which is a more efficient way to append additional information to this string builder object rather than going through the previous step of forcing the runtime to create all these temporary versions of string. So my string and I and the result will look identical. But what's going on under the hood is that we're working with strings in a more efficient way. So use the string builder along with the append method of the string builder to work with strings in a very efficient manner. Okay, so we talked about quite a bit in a very short amount of time, how to work with the backslash character for escaping and inserting escape characters, special characters and new are literal strings, how to use string dot format. In fact, let me show you this little page here for standard numeric string formats. If you just search for this on bing.com, you'll be able to find this article and it gives you examples and many other usages for the format that we looked at several examples of here. We looked at several of the built in helper methods to replace or define subsections or completely remove or actually create two upper or two lower to change the case of strings and then finally how to work with strings in a more efficient manner. Okay, so now we're going to give the same treatment to dates and times because you'll again find yourself working with dates and times frequently whenever you're building applications and there's a lot of similar kind of functionality there as well. We'll see you in that next lesson. Thank you. And this lesson will do the same thing except for dates. So we'll start off by talking about formatting dates and times. We'll look at how to add and subtract time to a given date. We'll look at how to create a date time object that represents this moment in time or the past or the future. And then finally we'll look at how to determine the length or the duration of time between two date time objects. So to begin, I've created a new project called dates and times pause the video please and catch up with me. And what we'll do here is actually just create a new date time object by going date time and let's call this my value. And we're going to initialize its value to a valid date time. So the easiest way to do that is to represent this very moment as the application is executing. So we'll go date time dot now. And that represents this instant. All right. And the easiest thing that we can do is just do a console dot right line taking my value and calling the two string method. Now you'll see we have a lot of two something strings and we'll look at a several of these in an effort to format our date time the way that we want. But this default two string method will take our country and our locale and will present dates and times as they are typically presented in our country and in our culture. So here in the United States, we usually represent the month first and then the date. I know in other countries, most other countries this date month year. And then we have the time of afternoon that I'm actually recording the video. Notice that it also has ampm as opposed to military time or 24 hours. So in order to change the way that this is presented, we're given a bunch of other additional helper methods. And so we can do something like this. So my value dot to short date string and this will this will just display the month date year. We can also do an isolate the short time string. So here we just want to display what time of day it is. All right, 335 in the afternoon. Great. We can also choose a more long form version of the date. And you can see it's Tuesday, March 15, 2016 as I record this. And we can do the same longer version for time as well. So my value dot to long time string. All right. And so you can see not only do we have hours and minutes, but also seconds in the long time string. Great. All right. So oftentimes what we'll want to do is do some date time math, which means we either want to add hours, minutes, I guess, seconds, seconds, minutes, hours, days, months, years, whatever the case might be. But we can do it through a series of helper methods, the add methods. So here I'm just going to console write line and we'll take my value and we'll start off with something simple like add days. You can see that we can add milliseconds, seconds, hours, days, and everything up from there. So let's just do something simple like add days. So we'll add three days and then we'll just do it to long date string on it like that. Now you may have noticed me do this in the past where I've used the period. Remember, that's the member access operator and chain together a series of commands. So in this case, we have a value that represents a date. If I were to call the add days method, notice as I hover my mouse cursor over it, that the return value of add days is another date time. So now since I have another date time in my hand that represents today plus three days, then I can call that date times to long date time string, which now returns as you can see a string data type. So that's the notion of chaining method calls together as long as you continue to chain together methods that return some value of some data type, you can continue to call methods for that given data type. Alright, so let's go ahead and see now three days from now it will be in fact March, Friday the March 18th. And let's do something with regards to hours and let's go my value add hours and we'll add three hours to long time string. And that will be six thirty eight PM. And then what if I wanted to subtract time? Are there any subtract hours or subtract days? No, however, what you can do is simply use a negative number to subtract. So instead of adding days, I'll subtract days. And yeah, we'll just go ahead and run that. And you can see three days ago it was Saturday, March 12th. All right, so in addition, we can just grab off parts of a date or time. So here again, let's go my value and let's just pull off this current month and this will return an integer. Now console that right line we know can accept an integer. So we'll just go ahead and print out the current month. So the third month obviously that's going to be March. All right, now we've looked at how to create the current daytime. But what if I wanted to create a daytime in the past or in the future? I could do something like this. So daytime and I'm going to call this my birthday. And here again is that new keyword that we've I've hinted at a number of times we will get to it. Don't worry, but I'm going to use it one more time, new daytime. And I'm going to pass in the year 1969, the month, December, and then the day, the 7th. That was when the day I was born. And so now what I can do is something like we've been doing up to this point console that right line. And just my birthday dot two short date strain just to prove that it's a date just like the other dates that we've been working on. What are you working with? So 12, 7, 1969. All right, now there's one final way to create a new daytime. So let's create another version of birthday equals date time dot parse. Remember we've used in parse. We were able to take a string and turn it into an integer. Here we're going to take a string and turn it into a date, hopefully. So we'll just type in my birthday again one more time. And that should give us a date time object that represents December 7, 1969. And now what I'm going to do is try to determine how many hours that I've been alive or how many days I've been alive. Days is probably more interesting number. And in order to represent a span of time, we're going to use a new data type called time span. So here I'm going to use a new time span. And we're going to call this my age equals date time dot now dot subtract. And the subtract method will take a the current date and subtract whatever date we want to use. So in this case, we'll use my birthday. Okay, so now that I have an object that represents a span of time, I can say represent that span of time in terms of days or years or whatever the case might be. So to do that, I'll go console dot write line and then I'll use this my age dot. And here I can say give me the total number of days that I've been alive and print those to screen. And you can see I've been alive what almost well 16,900 days. I'm getting old. Okay, I say that every time I record this video and I feel older every time. So at any rate, here we were able to format dates for display. We're able to manipulate dates by adding and subtracting date and time. And they were able to determine the the difference between two dates using a time span object. We also talked about different ways to create a date, whether it be now or sometime in the past or future by either just using one of the versions of the date time objects. Constructor, we'll talk about that later, or by using date time dot parse and passing in a string. Okay, so let's stop right there and we'll pick it up in the next lesson. Doing great. See you there. Thanks. Hi, I'm Bob Tabor with developer university. For more of my training videos for beginners, please visit me at devu.com. You might recall at the outset of this course, I said that a class is a container for related methods. And I use the console classes as an example of this. We had the console dot right line console dot read line console dot write we've used console dot clear all of these methods that had something to do with working with a console window. And so I said it makes sense to put them all in the same class the console class. Now truth be told, I intentionally oversimplified my explanation about classes and their relationship to methods. But first of all, I wanted you to gain a little bit of confidence in yourself that you can do this that this isn't hard. You can get your hands around it and you're going to do just fine. And I wanted to do that before we got into the topic of classes because while there's nothing hard per se about classes, they do lend themselves to a conversation about object oriented programming, a style of programming that some beginners find a little bit difficult to grasp at first. Now, the code that you've been writing in your methods have all been defined inside of classes, right? And you've been calling methods that were defined inside of classes, right? Classes have been all around you. You've been working with them up from the first line of code that you wrote. So you're really already an old pro at this, whether you realize it or not, I'm merely going to fill in some of the details that you don't yet know about. In this lesson and in a couple of subsequent lessons so that it rounds out your knowledge so that you can fully harness the power of the .NET Framework class library in your applications. Yeah, okay, maybe someday, whenever you sit down to architect some big application for some large company that you go to work for, you'll begin to think like an experienced object oriented software developer. But at this early point in your C sharp experience, I really just want you to be able to do one thing and one thing well. That is to find what you're looking for in the .NET Framework class library and be able to have the confidence to utilize the methods and the properties in those classes that have been defined there, all right? So the truth of the matter is that object oriented programming is such a massive topic that I certainly couldn't do at justice in this course. In fact, I have a whole course devoted to it on devu.com. Again, I really just want to accomplish one thing here. I want you to know enough about classes and objects and properties and methods and things like that so that you can harness the power of the .NET Framework class library inside of your own applications. Now the way that we're going to learn about classes and methods and properties and all that good stuff is by creating simple custom classes of our very own. So let's start by talking about creating a simple application for a car lot. So suppose that I own a car lot and I want to sell cars and I want to build an application that helps me keep track of all the cars on my car lot. So I might need to create a number of variables to hold information about a given car because I'm going to use that information to then determine its value based on its make and its model and so on, right? So I might start off by creating a couple of variables called car1make, car1model, car1year and so on in order to keep track of that information. Now what if I need a second car in my application? Well then I guess I could create another set of variables called car2make, car2model, car2year. What if I need a third one? Well I think you see where I'm going with this. Things are going to get out of hand pretty quickly here. Then what if I decide one day that the value of the car is not only based on the make model and the year but we also need to keep track of the color of the car as well. So in that case now I've got to do a car1color, carstring, car2color and so on. Alright so you can see that this simply is not the right approach to keep track of information that should be collected together about a given entity. So I need a way to keep all of this data about a car kind of together in its own little container. I want to keep track of the make the model, the year, the color and maybe a bunch of other things too about a single car. But I don't want to have to treat it like a bunch of loose information. I need it all kind of related together. So what I'm going to do is start off by defining a class that contains four properties that describe any given car on my car lot. So to begin what I'm going to do, you can see I have a project that I've already started with here, simple classes. Go ahead and pause the video and catch up with me if you like. And what I want to do is work actually outside of the first class that's already been defined in our program.cs file. So I want to work inside of the namespace, simple classes. But I don't want to define a new class inside of my existing class. I want to work outside of that class here. And so I'm going to define a new car class like so. And I'm going to give it four properties and I can type it all out like this and I'll explain what I'm doing here in just a moment. Or I can use a shortcut prop tab tab. And then I can use the replacement, the little replacement areas by using the tab on my keyboard. So I want to make a string tab tab model enter enter prop tab tab int year enter enter prop tab tab string tab tab color enter enter. Okay, so I just defined a class named car with four properties. This car class allows me to define a data type that describes a car. Every car in the world. Every car has a make a model a year and a color and a bunch of other information that I might or might not be interested in for my specific application. But my aim here is to use this definition of what comprises a car in order to create additional instances of the car class that represent all of the cars on my car lot. Okay, in other words, I want to create a bucket in the computer's memory that's just the right size to hold information about any given car on my car lot. So it should contain not only the fact that it's a car but then also the value of its make and its model and its year and its color all kind of in one big bucket up in the computer's memory so that I can access it. Alright, so there's two parts to this there's defining the class itself and then once I've defined it I can create instances of that class. So here the class is the definition but when I create a new instance of this class then I'll be working with an object and sometimes those terms get confused. But the class is the blueprint. The object is an instantiation or something that's been created as a result of having the blueprint or the pattern. Okay, so the way that we create a new instance of the car class is to do this. I'll just call this my car to avoid confusion. So at this point I've defined it just like any variable I would by declaring the data type itself, whether it be string or integer, this is just a little bit more interesting, a little more complex. It's the car class and then I give it a name that I want to call it by my car. Now that's part of what I need to do. The next thing that I want to do is actually then create a new instance of that class and say put this up in the memory, in the bucket so to speak. So here we go, new car. Alright, so again there's two parts of this equation. We'll talk about this more as we go throughout this course. But we're saying first of all I want to declare a new car in memory and then I want you to actually create the car. I want to create an instance of car and then put it up in the memory. So there's two distinct steps there. Alright, in the real world you can use the same blueprint to create many different houses, right? Like in the neighborhoods that I've lived in before, you might describe them as cookie cutter houses. They all look the same. You could use the same pattern to create clothing over and over or you could use the same recipe to create the same cake or casserole and get the same results each time. So each time you want to build a new house, it will be at a different address, right? Each time you follow the pattern, you'll create a new instance of the clothing that can be sold to a different customer. Each time you follow that recipe, you'll create a new instance of the recipe and you can offer it during either the same meal or a different meal. Alright, and the same is true with classes. Each time that you create a new instance of the class, you have a new object that is distinct and separate from the other instances of that same class in the computer's memory. Alright, so they each live by themselves. So a class is like a cookie cutter. Now, keep in mind, you can't eat the cookie cutter itself, right? You eat the cookies that you make from the cookie cutter. The cookie cutter gives each of the cookies some shape. And so when you instantiate a new instance of a class, you're basically using your class as a cookie cutter to stamp out new instances. And you have, you know, one, two, three, four new instances of cookies that you can then put in the oven and bake. Alright, so focus on the new keyword. It is what you would consider to be the factory. It actually builds the new car and puts it into memory. Alright, it uses the blueprint. It uses the pattern. It uses the recipe, uses the cookie cutter in order to create a new instance of that blueprint or that pattern or that recipe or that cookie cutter. And it brings the class to life in the computer's memory and it makes it usable by your application. And you can create many instances of a given class or you can create many objects all based on the same class, but each object will be distinct from the others if by no other distinction than by merely the address in memory where they live. So what I want to do is not only set the properties of this car because I have these four properties that I want to use to distinguish this car on my car lot to represent this single car, but then also I may want to then access or get those properties back out and it's working just like you're working with variables. Alright, so in this case, instead of just accessing make variable, I would go my car dot make right and I would set that equal to like Oldsmobile. Now, admittedly, in this particular case, I am merely hard coding these values. If this was a real application, I would ask an end user to input this information or I'd grab it from a database or something along those lines. Alright, so there we have it. We have one instance of the car class and I've set all of its properties and now I want to get those properties and print them out in a console window. And we'll just do this in the most easy way possible and we access or we get the values just like we set the values before by using the name of the object dot the name of the property. So let's go make car model year and color. Now you might be wondering, well, Bob, why did you do it that way and not car dot make or car dot model? Remember car in that instance car describes the class, the blueprint, but what we want to work with is one instance of the blueprint. So that's why we're calling that instance my car. It's the variable name in the computer memory that we want to work with. So let's go ahead and separate these out on the separate lines and then finally we'll go console dot realign. So and this should not be an exciting application at all because we're merely just printing things to strain. Okay, but at least I can show creating new instance of a class, setting the properties and then getting the properties and print them out. So that's what this get and this set are for. And there are actually longer versions of to declare a property. In fact, let's just do this prop full tab tab. And so this is a longer, more complete version of creating a property, but I don't want to talk about it right now. There are reasons why you would want to use this. But for the most part, for our simple needs, we'll just use this abbreviated version of defining a property in our classes. Okay. Now, did you notice that we got full IntelliSense support? So whenever I typed out my car dot and I use the member accessor operator that I'm able to see all the members of the class, the make, the model, the year and the color all represented as little wrench icons in IntelliSense so that I can access them, whether to set their value or get their value. Okay. Furthermore, I'm able to set values the way that I would just with normal variables by using the assignment operator. Okay. I'm able to work with the variables and write them just like I would any other variable in my system. Okay. So there's nothing all that special about it outside of the fact that they're all related to a specific instance of a class. So we created a new data type, the car data type. And since the data type, we can use it just like we would any data type in our system. So if I wanted to create a little method here, private static, and I'll use the decimal data type because I'm working with going to work with dollars or money currency. And I'm going to create a method called determine market value. All right. And I'm going to allow this to accept a car as an input parameter. And what I'll do is just in this case, I'm just going to hard code car value to $100. Okay. And we'll leave it at that. In fact, I'll go ahead and have the M here. However, if this was a real application someday, I might look up the car online using some sort of a web service to get a more accurate value. Okay, accurate value. But for today, we're just going to hard code the value to be 100 and we'll return car value. Okay. And so here I can go determine market value. I can pass in my car. And I should return back a value. So let's go a decimal value equals determine market value. And then let's go console that right line. And we'll use what we learned previously to print out the value of the car like so. And let's run the application. You can see that it's worth $100. All right. Now notice what I did here. I used an uppercase C in car and a lowercase C in car. The uppercase C corresponds to the name of the class because I named it with a capital C. And the C sharp compiler is smart enough to know that again, capital C car and lowercase C car are two different things. And this is a common naming convention to use the same name for an object if there's no reason not to. If there wasn't something special about the car like it being in some special state. But I can reuse the word car. I chose not to do that here just to make it obvious what I was actually doing. But there's nothing wrong with doing this as well. Defining this input parameters data type and then giving that input parameter the same name but just with the lowercase character. They're two very different things. Okay, so moving on. I want to talk about creating methods on the class. We've already said that we that classes are containers for methods. So in we've created this helper method here inside of my static void main, but it might make more sense for us to create that method here inside of the car class itself since the car class already has access to information like the make model the year in the color right. And that's the kind of information that we would use in making a determination on its value. So here let's go ahead and define this as a public decimal determine market value. Now we're not going to allow anything to be passed in because we already have all the information we need right here. Okay, so let's create a little little algorithm here. So if the year is greater than 1990, then we will set the value of the car, the car's value, which we need to define as a. So let's go decimal car value will set the car value equal to $10,000. So if it's a relatively new car will set it to 10,000. Otherwise, we'll say the car's value is only worth 2000. Alright, so this is a very, very overly simplistic example, but we just want to demonstrate the fact that inside of an instance of the class you're going to be able to access its properties. Okay, so we're able to access the current cars year in order to determine its value. And so in this case, what I'll do is let's comment this out and comment that out. And here we'll go console dot write line my car dot determine market value. Like so. And because this is going to come back as a decimal, I'm still going to want to format it. Alright, so now let's go ahead and run the application. Alright, and since it's in 1986 is before 1990, it's only worth $2,000. Okay. So in this lesson, we used a very concrete example. We've all seen cars, driven cars, own cars. Okay, a car is easy to conceptualize and represent in a class because there's a tangible real world equivalent. Now, my assumption again is that your main exposure to classes will be whenever you're using classes defined by Microsoft in the dot net framework class library. And most of the time those classes don't represent real tangible things. They're very conceptual in nature. You might have a class that represents a connection to the Internet. You might have a class that represents a buffer of information that's streaming from hard drive. Okay, they don't really have real world tangible equivalent. So you need to be aware of that. In most cases, the dot net framework class library classes don't have real world equivalents, but the ideas are exactly the same. As you mature as a software developer, you might want to invest a little bit more time in learning how to create your own library of classes. And those classes can interact with each other. They can represent real things in your company or in the real world or conceptual things. The process that you go through to break down a problem in the real world and represented in objects is object oriented analysis and design. Again, that's not a topic that we're going to cover in this series of lessons, but you can learn more about that at devu.com, where I spend a lot of time talking about those sorts of things. Okay, so to recap, a class is just a data type in dot net, and it's similar to any other data type like a string or an integer. It just allows you to define additional properties and methods. So you can define a custom class with properties and methods, and then you create instances of those classes. Or rather, you create an instance of an class, therefore working with an object using the new operator. You can then access that object's properties and methods using the dot operator, the member access operator, right? So there's quite a bit more to say about classes. Don't worry if you don't understand everything just yet, why you even need them, how to really fully utilize them. Just make sure you understand the process that we went through in this lesson of defining a new class, creating an instance of a class, setting its properties, getting its properties, passing an instance of a class into a method, or even defining the method inside the class itself, and allowing it to access its own members, like its other properties. So if you really don't understand much more than that, then you're doing just fine, you're exactly where you need to be, don't worry. We'll cover lots of other topics related to this in the upcoming lessons. We'll see you there. Thank you. Objects come to life, they live for a period of time, and then they die, they're removed from memory, and we'll talk about the .NET Framework runtime and its role in the creation, the maintenance, and then ultimately the removal of objects from memory. Next we'll talk about constructors, which are simply methods that allow us to write code as developers at the moment when a new instance of a class is created. And then finally we'll talk about static methods and properties that static keywords been lingering around now for some time, and we've been using static properties and static methods throughout this course, even from our very first example. So we'll finally tackle that issue in this lesson. All right, so let's begin by creating a new project. You can say I've already done that and pause the video and catch up to where I'm at right now. I've created a new project called Object Lifetime. Furthermore, you'll see that I copied the car definition from our previous lesson. If you like, you can type that in, help build some muscle memory, help remind you to use the prop tab tab shortcut. The code snippet in Visual Studio to create these shortened auto implemented versions of properties. We'll talk about that in a little while. And then ultimately you can see in line number 13, we create a new instance of our car class that new instance will call my car. And we talked about this in the previous lesson, but I felt like this deserved a little bit more explanation because there is actually a lot that's going on under the hood. And it would be helpful to understand this as we begin to work with classes and objects. So whenever we issue a command to create a new instance of a class like we have in line number 13, the .NET framework runtime has to go out and create a spot in the computer's memory that's large enough to hold a new instance of the car class. Now that much we know. The computer's memory has addresses that are similar to street addresses like the address you live at, the address that I live at. Now admittedly a computer's memory address is looked dramatically different than our addresses like 123 East Main Street. Because the computer's addresses are typically represented in hexadecimal values, but they're known addresses nonetheless and it's easy then for the computer to find something in its memory by using its address. So the .NET framework's first job is to find an empty available address where nothing is currently living where there's no data that's currently being stored. And that address has to be large enough to store an instance of our class. So the .NET framework runtime will then create the object instance and it will copy any of its values that are currently stored in that object instance up into that memory address. Then it takes note of where it put that object. It notes the address of the memory where it put that instance of our object. And then it serves that address back to us and we store that address in the actual name of or the instance name of our class. In this case myCar, that variable is actually holding on to a reference or in other words an address in the computer's memory where we can access that object once again. Now whenever we need to access the new instance of the car class, we merely can use its reference name. It's in this case myCar. So myCar is simply holding an address. It's simply a reference to an instance of, in this case, a car class in the computer's memory. Whenever you need to work with that instance of the car class, you just use the myCar identifier and the .NET framework class run time takes care of everything else for you. It gives you the illusion that you're actually working with the object itself, but in reality you're just holding on to a reference to an address in the computer's memory. So there's an analogy that helps me to sort all this out in my mind and we're going to continue to extend that bucket analogy. If that object is stored in the computer's memory and if it's what we have equated to a bucket, an address, an area that holds on to our values, then what's returned back to us as programmers is a handle. That's what myCar is. It's our handle to the bucket. And we've used that bucket analogy a number of different times and it served us well. But we essentially are storing values in that bucket just like we were before and we're holding on to that bucket using our reference to that memory area in our computer's memory. So what happens if we were to let go of the handle? Well, at that point, we'll no longer be able to get back to the bucket. We've lost the bucket somewhere in the computer's memory. The bucket will no longer be accessible to us. Now, can we ever get back to that bucket? Well, no. What happens is that the .NET framework run time will be constantly monitoring the memory that it manages. And it's looking for objects that no longer have any handles associated with them. So once we let go of a handle, the reference count, the handle count, I guess you could call it, will go to zero. And at that point, the .NET framework run time will say, I see that nobody's interested in you anymore. They've let all of their handles to you expire or to go out of scope. So that must mean that you're no longer needed and it removes it and throws it in the garbage. And so that process of monitoring memory, looking for objects that no longer have any references to them, is called garbage collection. It's one of the core features of the .NET framework run time. And it's one of the reasons why it's easier to work with C sharp at first as a developer than maybe going directly to C++. In an unmanaged language like C++, you the developer may have to manage memory on your own. And sometimes you might forget that you actually are leaving things in memory and you're not cleaning them up, you're not removing them yourself. So your application might have a memory leak or you might have a corrupted memory region where you're using an area of memory and you forget that you're using it so you copy something else to that area of memory. Now you go back to retrieve the value that you originally put in there and it's corrupted. So that leads to corrupted memory in applications. You don't really get that issue so much in C sharp because again the .NET framework run time takes care of all the memory management for you. Alright, so let's do a little experiment here. If we said that we can have one handle to a bucket, what happens if we attempt to create a second handle to the same bucket? So let me do this real quick. Let me go to my car and start setting some of the properties like the make equals Oldsmobile. Then we'll set the model equal to the Cutlass Supreme and then we'll set the year equal to 1986 and then finally we'll set the color to silver. Alright, now keep that in mind. We've created a new object called my or that we're referencing using the my car identifier. This car class lives, instance of the car class lives in memory and we're holding on to it with a handle called my car. But what if we were to create another car like this? So my other car. What have we really done right now? We simply have created a handle, but we've not attached it to any buckets of cars in our computer's memory. So at this point, what I could do is go my other car equals my car. Now what have we really done there? Well, we've merely taken one handle to a bucket in memory and we've created a second handle and said, hey, let me copy your address. So that we're both referencing the same bucket in the computer's memory. Now to prove that what I'll do is do a console dot right line. And we will do what we did before. Whoops, whoops, whoops. Alright, and just give me a second here and we'll reference my other cars make my other cars model. My other cars year and then my other cars color. Let me separate these two different lines for readability sake. Like so. Whoops. Alright, and then a console dot read line for good measure. Now let's run the application. Alright, and you can see that even though we created or set the properties of my car. Since we copied the reference to the car object in the computer's memory into a new variable called my other car. I can still get to the values that are that are in memory because they're both pointed to the same object, right? And now I can even do something like this where I actually change something my other car dot. Let's set the model equal to the to the 98. That was the large style model for that car. And let's then go back to and do something similar to this just to prove that they're one in the same here. And I'll say, hey, let's do that. Okay, so we're going to use our reference called my other car and set the model change the model from the value cutlass supreme to the 98. And then we're going to say, hey, show me what's in the my car object. Alright, so now we're going to run the application and you can see now we're printing out what's currently in my car. It's the same thing that we changed in my other car because they're both pointed to the same place. I just want to emphatically make that point here. Okay, so as you can see, we have now two references to the same object in memory. We essentially attached a second handle to the same bucket so that we can use either one to retrieve the data in the bucket, so to speak. Alright, if you don't like that analogy, maybe it helps to think of this in terms of balloons. So I have a balloon and I have two strings tied to the balloon. What happens when I cut the first string? I'm still holding on to the balloon, but what happens when I cut the second string? The balloon now will fly away and we'll never see it ever again. Okay, so as references go out of scope, in other words, whenever the current threat of execution leaves the current code block that we're currently in, or those object references are set to null intentionally by the software developer, then the number of references to the object, the number of handles to the bucket, the number of strings attached to the balloon, they go to zero. And so here again, when the .NET Framework runtime looks through memory and finds objects that have a reference count of zero, it will remove those objects from memory. So we talked about the two instances in which the connections to the object get removed. One is that the reference goes out of scope. Whenever we create a new variable called myCar, it will continue to be in scope as long as we're inside of this main method. But once we exit out of the main method, that variable goes out of scope. It's no longer available for us to access any longer. The same would be true if we created a different method and defined a variable. And as soon as we go out of scope of that method and we have finished executing all the lines of code in that method, then any of the variables that were declared inside of that method go out of scope. And in this case, we would lose then any references to the objects that we created in the context of that method. So that's one instance in which we'll lose references to objects that we've created. But the second is if we, as the developers, actively take a role in cutting the strings or removing the handles from the buckets in memory. And the way that we do that is by setting our objects equal to null. The value null is not zero and it's not an empty string, it just means indeterminate. In this case, what we'll do is go here and we'll set my other car equal to null, like so. And when we do this, now we'll remove one of the handles to the bucket so we're back to just one handle in the bucket. To prove this, let me go ahead and copy this little section of code and go here and put it below this. And when I do that, notice what happens, we'll get an exception. The exception is that there's a null reference exception that was unhandled. And the reason why it was a null reference exception is because we have now removed the handle. The handle does not point to any objects in memory and yet we're still attempting to access values from the object in memory. So we get an exception in our application. Now what would happen if we were to remove the second reference, like so, my car equals null. Well, at that point now we have removed all the references to the bucket. Even if we were to attempt to get to it with either my other car or my car, either way the references are gone completely. And so now the object will be removed at some indeterminate time in the future by the .NET framework runtime. And so in some situations this indeterminate period of time can cause a problem, especially when the object in memory is holding on to some special resource, maybe something like a reference to a network connection or a file on the file system or holding on to a handle to access a given database. So again, we don't know exactly when the .NET framework runtime will actually execute the garbage collection step and that might pose a problem in certain situations. In these cases you would want to use a more deterministic approach to requesting that .NET removes the object from memory and if necessary will finalize and clean up anything that needs to happen inside of that object to completely get rid of it in the computer's memory. So in these cases you want to learn about deterministic finalization. That's a little bit of an advanced topic so we're not going to talk about it in this series of lessons. Just keep in mind that whenever we set reference to null or whenever we go out of scope we will be removing all the references to our objects. But the .NET framework runtime itself figures out when it's ready and willing to remove those objects from memory completely. In most cases that's not a problem. Occasionally you're going to run into a situation where it is a problem, know that there is a remedy for it called deterministic finalization. So that should suffice our explanation of really what's going on whenever we create new instances of objects, how objects are maintained in memory and then at what point they're removed from memory. So let's move on and talk about constructors and I said at the very outset that a constructor is merely a method that allows us as developers to execute code at the moment that a new instance of a class is created. So there's something really subtle about what's going on here in this line of code line number 13. Did you notice that whenever we use the new keyword and we give it the name of the class that we want to create a new instance of that we're also calling it using the method invocation operator. Why do you suppose that is? Whether you realize it or not you're calling a method whenever you create a new instance of a class and that method is referred to as a constructor and it allows you the developer the option. You don't have to do this so it's an option to write some code at that very moment whenever a new instance of a class is created. So constructors can be used really for any purpose but typically they're used in order to put that new object into a valid state. Meaning that you can use it to initialize the values of the properties of that given object and so it's immediately usable. Now let me give you a really quick example here. Let's say that you want to create a constructor that would allow you to set a property of the car at the point whenever you create a new car class. So that that property is available immediately in the very next line of code whenever we begin to work with it here in line number 15. So whenever you actually want to create a constructor you would go and create something like this. Public car and in this case what I'm going to do is simply set the make property to Nissan. So by default whenever we create a new car class we're going to set one of its properties to make property to Nissan. Now let me say this as well. You might see the keyword this used. This keyword is optional. It refers to this instance of this object and it's just to help clarify where this variable name or this name is coming from. When I see that this keyword I automatically think oh that's part of the declaration of the class itself. It's saying that you want to access a member of this class that's been created. But as you can see it's kind of faded out in my text editor it might not be in yours which lets me know that I could actually remove this it's not necessary. So you might see that though in other people's code just understand what that is. Alright so now if we were to go ahead and create a new instance of the car class. Here's what I'll do. I'll actually comment out all of this code like so and then I'll comment out the code that we know will break the application. Let's allow we can leave the rest of it I suppose. Now whenever we run the application notice that the very first item that is displayed is the make of the car and it's set to Nissan. So I didn't set any other properties that's why we didn't get any other values there in the printout but hopefully you can at least see how we go about creating constructors. Now admittedly it may not make a lot of sense right now why you'd want to do this but I'm showing you the technique you'd use not the rationale necessarily. But the rationale is simple what we would typically do here is to put any new instance of an object into a valid state. So you could load values into the various properties of your class from a configuration file or from a database or some other place in order again to get that object into a valid state so that it's immediately usable at the point of whenever it's instantiated. So let's go ahead and talk about overloaded constructors you'll see this frequently whenever working with objects in the dotnet framework class library. So just like you can create an overloaded method in your classes by changing the method signature. In other words the number and the data type of the input parameters for the method you can do the same thing with a constructor. You can create an overloaded constructor so what I'm going to do is create an overloaded constructor here like so. Now at this point the method signatures are the same so I'm going to get a little error here but to modify that I will merely add at least one input parameter of type string but I'll go ahead and do them all as well. And then here in the body of the constructor I will just do make equals make so this capital M make is in reference to the property itself. This lowercase M and make is the name of the input parameter. It's a good convention to use the same name for readability sake and for your own sanity you don't have to do it this way. But just keep in mind that uppercase M and lowercase M are two different create two different items in as far as C sharp is concerned. So it's not confused you might be confused but it will be able to handle this just fine. Now you might ask well what's the point of that? Well in many cases whenever you create a new instance of a class typically you don't want to take five steps to do this. You would want to immediately whenever you create a new instance of a class so car. My third car equals new car. At this point you can do one of two things. Notice here that underneath car underneath the open parenthesis I have one of two ways that I can call the constructor. I can either give it no input parameters or I can give it four strings as input parameters to initialize that new instance of car and put it into a valid state immediately. So here I might go forward skate 2005 white like so. And now I have not only created a new instance of the car class but I've immediately initialized its values by calling its overloaded constructor to populate all of its values at the moment. So what would happen if we were to actually remove these two completely? What if we were to comment these out? What happens? So you can see that we're still using the method invocation operator for our new instance of car. That would suggest that we're calling a constructor but we don't have a constructor defined so why is this working? Why isn't it giving us an error? Well the reason is because a default constructor is automatically created for you whenever you compile your classes. It will be a constructor without any input parameters and it will have no body. But it's essentially the equivalent of doing this right here except with nothing inside of it. So that's created automatically for you. So no matter what you're going to have a constructor, it just won't do anything for you. The implicit default constructor has no input parameters, no method body, but it allows you to make calls and create new instances of classes in a consistent way. So it's actually just generated for you again at compile time. Of course by defining it yourself you're taking control of the process of instantiation. Alright so let's talk about the static keyword now. You've seen static around since the very beginning. I said let's ignore that for now. We created our own methods and I said we have to use the keyword static. I'll explain later. Well now is the time. So I want to ask a question. Did you ever notice that whenever we're working with a console window we never had to create an instance of console, the console class in order to call its methods, right? And that combined with the fact that whenever we wanted to work with datetime we could get to this moment in time by using the datetime.now property, but we never had to create an instance of datetime. Furthermore whenever we were actually working with arrays and we wanted to call the reverse method, do you remember we did array.reverse and then we passed in the array itself. How is it that we were able to use the reverse method without creating an instance of the array class? Well in each of these cases the creators of those classes or specifically those methods adorned their methods with the keyword static which means that you do not have to create an instance of a class in order to utilize that method. In some cases they may have defined an entire class as static meaning that all of its properties and methods were static. So you can create your own static methods and classes as well. Again the objective here at the very outset is to just help you utilize the .NET Framework class library. So just know that some of the classes and methods in the .NET Framework class library are static and some are instance or require you to create an instance of the class before you call its methods and properties. So the static methods will be available to you without first requiring you to create an instance of a class. So just so you can see how this works we can create a static method on our class like so. In this case we'll go public static void my method and here we'll do console dot write line called static my method. And now we can go here near the very top and just say car dot my method and notice I didn't have to create an instance of car. I'm using the actual car class definition itself when we run the application. Alright and before we go too far here let's comment out pretty much everything. Let's remove that and we'll go down here. Alright just make this so that we don't run into any potential issues here. So let's run the application and you can see that we were able to successfully call the static my method. Now what would happen if we attempted to reference one of the properties in our in our class. So let's just print out the make property. Notice that I immediately get a red squiggly line beneath the word make. It says that an object references required for the non static field method or property called car dot make. So it's important to keep in mind that there's a fundamental difference between working with classes that have static members versus instance members. Instance members are the things like we've been working up to this point where we have a series of properties that describe a single instance of a given entity like a car. They might be methods that operate on a single instance of a car like the constructors that we saw. Whereas a static member like a static method in this case they don't really operate on any single instance they're more like utilities. You can call them at any time they're not they don't depend on the state of a given instance of the class. Or even the application itself that can be used at any time because they're not really tied to one specific car. They're true of all cars and can be used at any time. So static members versus instance members just keep those two clean in your mind. You might want to ask the question why would you ever create a static member like a static method. Well that's a bit more complicated that might require a longer discussion of things like design patterns which are common solutions to common problems for software developers. Or coding heuristics which are more the best ways to go about solving problems. I just want you to know that there's a fundamental difference between static members in a class and instance members of a class. And it's easy to recognize them if it's a static member it'll have the static keyword. And in which case you cannot reference any instance members like instance properties or even other instance methods that act on instance properties. They require an instance of the class to operate. So just know that there are these two types of members in a given class. And that you're going to encounter both whenever you're working with the dotnet firmware based the class library. And why you would use one or the other well that's really again another story. I would say this that typically I would recommend that you don't mix and match them in the same class. Clearly not everybody agrees with me because you'll find that many times but it's not really important at this point to understand why you would use one or the other. Just know that that possibility exists. That's why you don't always have to create an instance of a class before you use the members of its class in this case a given method. Alright so let's recap what we talked about in this lesson. We began talking about the lifetime of an object, how we create a new instance of an object, what that's doing in terms of creating an area in the computer's memory, returning back to us an address, a reference to that object in memory, what happens during the lifetime of that object and ultimately what happens whenever we remove all of the references to that object. We talked about the role of the dotnet framework runtime and how it's keeping track of the number of references to objects so that it can perform garbage collection on objects that have no more references to them in memory as a means of keeping things clean and making the memory available to other applications or even our application again. We talked about constructors and how developers can use them to put a new instance of an object into a valid state at the point when that object is created. Then we talked about the static keyword. We looked at some usages of static members inside of the dotnet framework class library. We looked at creating our own static member, this main or this my method. We talked about the difference between static members and instance members and how it's really oil and water. You can't mix the two and why that is. We didn't really talk about why you would choose to use one or the other. However, that's again a topic for another day. So hopefully all of these concepts make sense. If not, don't continue on and hoping that you'll just catch up to them at some point in the future. Make sure you thoroughly understand this before you continue on. Okay. If you are continuing, great. We'll see you in the next lesson. We'll see you there. Thanks. Hi, I'm Bob Tabor with Developer University. For more of my training videos for beginners, please visit me at devu.com. Now we haven't spent a lot of time talking about variable scope. It's actually extremely important. We recently learned that it also impacts the lifetime of objects. So we want to spend a little bit more time really making sure we understand the scope of the variables, whether they be variables holding simple types or references to complex types in our applications. And not only do I want to fully explain that, but then I want to use that as a launching pad to explain keywords like public and private that we've seen several times in our course up to this point, but I haven't really talked about. Before we talk about that, let's talk about variable scope. So let me start by saying that whenever you declare variable inside of a block of code, that variable is only alive for the life of that code block and any of its code blocks, any of the interior code blocks or code blocks inside of that code block, meaning that when the code block is finished executing the variable that was defined inside of that code block is no longer accessible and its values are disposed of by the dot net framework runtime. So we'll start by looking at how that is impacted by common code blocks that we've been working with up to this point and then we'll use that and expand beyond there. So you can see that I've created a project called understanding scope and you can pause the video and catch up with me. I want to create this project and focus on testing how variable scope works and so I'll start with a pretty simple code example. Again, the concepts that we talk about also apply to object references, not just variables that hold simple strings and integers. Alright, so let's start by creating a simple for iteration statement and we'll just loop through 10 times and we'll do a console dot write line containing the value of I. And then here we'll do console dot read line so we can see our results and we'll run the application and as we would expect we can see values from zero through nine. Now, what if I wanted to access the value of I here right after the closing curly brace for the for for the for statement will notice that I'll get a red squiggly line under I and if I hover my mouse cursor over it says that I does not exist in the current context. Why because I is now outside of the scope of its definition we defined I inside of the for loop is available inside the for statement itself plus in the code block below it but not outside of either of those. Alright, so I'll have to comment that out. So second, let's start. We'll continue by going and creating a string of J equals empty string. And what we'll do inside of our loop here is just go J equals I dot two string. Now let's go outside of our loop will be able to access the value of J. So let's go outside of the for and will be able to actually print a screen the value of J. Alright, we're not getting any errors. So let's run the application. And you can see that the last value that was inserted into J was the value nine. Since we define J outside of the scope of this of this code block of the for statement and its code block, we can access it inside of that code block and outside of that code block as well. Alright, next up, let's look at the at something like this where we'll actually create what's called a field or a private field. So we'll go private static string K equals and and a private field is sort of like a property, except it's private in nature, but it is available to all of the members of the class. So we should be able to see K inside of our for loop. So let's do I dot two string. We should be able to see it here as well in the outside of the for loop. Like so, let's go ahead and run the application. And you can see that second console dot right line will also display the number nine. But the real question is what if we were to create a helper method static void. And we'll just call this helper method. And here we'll go console dot right line. And we'll say this is the value K from the helper method. We'll do that. Now, here, we'll call the helper method, like so, will this work, we'll be able to access the value of K as it was set inside of our for loop. Outside of our static void main. So run the application. And you can see that we can, in fact, get the value of K from the helper method. Why? Because K was defined at, I guess you could say the class level. It is a sibling to static void main and static void helper method. Therefore, it's accessible to each of these as well as any of their inner code blocks. All right, hopefully this is starting to make sense. Let's go inside of the for loop now. And here, what we'll do is a simple if statement. So if, if I is equal to nine, so on the very last run of this, then let's declare a string called L. And we set that to string, the I to string. And then outside of that, we'll go console dot right line, the value of L. And as you might anticipate, we will see that L does not exist in the current context. Why? Because we declared the value of the string variable L inside of the if statements curly braces outside of those curly braces. It's no longer accessible. So we have to comment that out. All right, so hopefully this solidified in your mind, many of the combinations that we can use in determining whether something's in scope or out of scope. And if you had any confusion about this, hopefully that cleared it up a little bit. All right, so now let's move on to the larger topic of accessibility modifiers. We've been creating classes, specifically the car class up to this point. And whenever we were creating methods, I would typically use the public keyword. Occasionally, I would use the keyword private like I did here in line number 11. Private and public are both accessibility modifiers. They're used to implement a tentative object oriented programming called encapsulation, which is actually pretty important. So in a nutshell, you should think of classes as black boxes. And when everything of a black box, maybe you can think of like one of those old style television sets. Maybe your parents or grandparents had one. I remember as a kid, I was having one. There were no remote controls yet to get up, walk across the room and actually turn the dials of the TV in order to tune to either VHF or UHF channels. You had another volume maybe where a dial where you would adjust the volume. You had an antenna in the back so you would connect this wire out to your antenna and you had another one where you would plug it into the wall. And everything else about the television was self contained. Now as a kid, I was fascinated whenever my dad would pop off the back of the television set and he'd go and try to fix it by changing out the tubes. And it's always seemed like magic to me because I knew absolutely nothing about the innards of televisions. All I knew were the public interfaces, the button for on off, the dials to turn the channel, the dial to turn the volume up and down, the antenna, whatever that did. And the little plug that would obviously give it electricity. But frankly, in order to use the television set, that's all you really needed to know, right? You did not know, have to know anything about how a television worked. All you really needed to know was how to plug it in and change channels, turn it on and off, and then adjust its volume. And that is exactly how your classes should be treated. All the important behind the scenes functionality should be encapsulated behind interfaces like public methods and public properties. Now classes might in fact have private fields like we looked at here in line number 11. Or they might have private methods that are used behind the scenes to enable all the magic that goes on inside of that class. But the consumer of the class shouldn't know anything about the inner workings of the class in order to work with the class, to operate the class. All they need to know is what's publicly exposed through the public properties and public methods. So in a nutshell, private means that a method can be called by any other method inside of the same class. So I use the term private helper method a number of times accidentally. Essentially, when I use the term private helper method, I'm talking about a private method that adds some additional functionality to those public methods that are exposed to anybody who needs to work with the class through that method. A public method is what's actually going to be then called by somebody outside of the class, some other code outside of the given class. And private methods are only going to be called by members inside of the class. So let me do this. I'm going to paste in some code to recreate our car class. And here I have a public and a private method. The public method is called do something and the private method is called just helper method. All right. And these are not very interesting examples. I want to keep this as simple as possible. Now from the outside of this car class, let's just kind of roll this whole thing up here and save it. And now whenever I want to go here inside of my static void main, I might want to work with the car class. So I'll go car, my car equals new car. And then I'll do my car dot and notice that I can only see the public method do something. Now I might happen to know there are other methods also inside this class, but I can't see them from outside. Their visibility is hidden to me because they're marked as private. All I really need to know is how to use the do something method. And if I understand that I can call that, all the implementation details will be hidden from me, but it will work as I expect it to work. Here you can see that it's merely prints out the words hello world. Now how it does that, whose responsibility inside of the class it is to actually display that, that's none of my concern. All I need to know is how to call the public method do something. So in a sense, the consumer of the car class has absolutely no idea that the helper method even exists. All it really knows is that there's one public method and it could call that public method, but it doesn't know any of the hairy implementation details. Now I use the term in a sense that in a sense the consumer of the car. The consumer is going to be a software developer and a software developer is going to be able to drill in and say oh I see how it's doing its work. It's actually making a call out to this other private helper method. And so there is a sense in which it is public to developers, but it's private from the perspective of the consumer which is this main method. It can only see the do something method, not the private helper method. That's all we really mean here. Now admittedly this is extremely mundane. It's a simple example that's only real value is to illustrate the notion of encapsulation that we typically want to hide the implementation of our classes behind well known public interfaces. In this case, a friendly method called do something. And the purpose of this lesson is to better understand the notion of scope. Because we said that once variables, especially variables that contain object references fall out of scope, their objects will be garbage collected. Furthermore, it's important to understand that there are parts of classes that you have access to and parts of classes that you don't have access to. Now if you ever decide that you want to create your own custom classes someday, even a library of classes that represent the business domain of your company or of your specific application, it could be a game. You should strive to expose public methods and give a simple straightforward obvious way to call the public methods from your class. But keep all the other helper method, all the other internals privately tucked away and not available to prying eyes. So you don't want a developer to simply go fiddling around inside of all of your methods and use your class in a way in which it was unintended. You want to give them a way to use your class properly through the methods that you've designed and that you've made available through public interfaces. And this also will help to remove any ambiguity in the usage of your classes. And it should be much cleaner as well. All of these things were under consideration whenever the developers built the .NET Framework class library. In the .NET Framework class library methods and properties are exposed using the public keyword. Now they might also be using private fields and private methods behind the scenes, but you would never know. They may use other types of accessibility modifiers as well. There's actually a couple available called protected and internal. However, these are primarily for whenever you're working either in a rich inheritance relationship between classes that and you're building a rich inheritance hierarchy between classes. Or whenever you're working with a very large library that's compiled into separate assemblies. That's when some of these other accessibility modifiers might come into play. They're topics that are beyond the scope of this absolute beginner series, but topics that I do cover on developer universities. So if you want to know more about object-oriented programming and encapsulation, by all means go ahead. All right, we are well past halfway through this course. You're doing great. We've already covered the most difficult material already. Now we're just adding on details. So you should be encouraged by that, that you're still plugging away at this. And you're doing great. We'll see in the next lesson. Thank you. Hi, I'm Bob Taper with Developer University. For more of my training videos for beginners, please visit me at devu.com. Previously in this course, I said that the .NET Framework class library is merely a collection of classes. Each containing methods filled with functionality that we can utilize in our applications, but we didn't have to write. Microsoft has spent tens of thousands of man hours building and maintaining this library of code, and we can benefit from it by merely calling into its classes and methods inside of our applications. Now the Framework class library is massive. Thousands of classes, each with their own set of methods. And so the developers of the Framework class library wisely decided to split this library of code up into multiple files. Just imagine if you had to load the entire library into memory every time you wanted to run your application. First of all, it would be excruciatingly slow. And then secondly, it would probably take up the majority of your computer's memory. And so they split up the code into multiple files. These code files are called .NET Assemblies. In fact, even the applications that we build, they're ultimately compiling into .NET Assemblies. As you can see, I have a new project called Assemblies in Namespaces already open. I've added two lines of code. If you want to pause the video and catch up, that would be great. In lines 13 and 14, I'm merely printing Hello World to the screen and then pausing the execution of the application. However, even in this application, a executable .NET assembly is being generated the very first time that we run the application while we're debugging. So now, if you want to take a look at what happens, go to your projects directory. And inside of the project folder, you'll see that there's a bin directory. We avoided this very early in this course, but now I want to talk about it briefly. The bin directory will contain both a debug and a release version, ultimately a release version. The debug version will contain additional files required by Visual Studio to connect to the execution of the compiled executable. This allows us to step through the execution and pause the execution line by line in the Visual Studio debugger. Now, we can additionally, then after we've created our application and thoroughly debugged it, we can say I want to create a release version of the application and go to build solution in the build menu. And it will create a version of our application without any of those debugging symbols without that connection to the debugger. If you look at the file system, you might be a little confused to see that it also has a lot of those extra files in there, but they're basically ignored. But that is what's going on behind the scenes. Notice that in each of these cases, we're building an executable file that will run, and we could even just double-click it and run the application from here like we did before, right? Now, that is different from the type of .NET assembly that allows you to create a library of code that can be shared across multiple projects. In that case, you'd be compiling a project into a .DLL file extension. We can create a code library. I'll show you how to do that in another video. But at any rate, the .NET framework has to already be installed on any computer where you want your application to work or to run. Fortunately, basically every copy of Windows already has the .NET framework runtime and the class libraries installed in a location that's globally accessible called the global assembly cache. And so every .NET application can reference the same set of assemblies in that one spot on your hard drive. Now, you might say that whenever you build your application and set up your application, you may not realize that by choosing to create a file new project and then selecting the console window project template, you were actually creating references to those files in the .NET framework class library. That's one of the functions of the setup routine for a project template. So if you take a look at the references node underneath your project and the solution explorer over here on the right hand side, you'll see that there are some references already to these things like system, system.core, system.data, system.net, and so on. Now, we'll talk about what these are in just a moment, but that's indicative of the fact that we have references into files of the .NET framework class library that the creator of the console window application thought we might be or might find useful at some point. All right, so we'll come back to that in just a moment. Now, sometimes you'll need an assembly from the .NET framework class library that has not been referenced, and I'll demonstrate how to do that in an upcoming lesson, or perhaps you need to add a reference to an assembly created by a third party, maybe even yourself. Again, I'll demonstrate not only how to create your own class library, but then also how to create references to third party assemblies as well. Again, there are tens of thousands of classes defined in the full .NET framework class library. In a few cases, the same class name was used, or at least there was the potential for it to be used. And so when that happened, the creators needed a way to be able to tell one class from a different class. And so they introduced the notion of namespaces, and namespaces are like last names for your classes. So think about your name or my name, for example. Somebody might say Bob loves coffee, and you might say, well, which Bob? There's like a billion Bobs in the world, right? But if somebody were to say Robert Theron Tabor likes coffee, well, that narrows it down. I'm pretty sure that I'm the only person in the world that has that combination of first, middle, and last name. So I could either use the full name, Robert Theron Tabor, to reference one person, or once we understand the context of who we're talking about, maybe we're talking about only people in this room, then you might say, well, Bob likes coffee, he's the only Bob in this room, so they must be talking about Bob, right? So the same idea works with your code. We could use the full name of the classes that we need inside of our application. So for example, the full name of the console class is actually system.console.rightline, or the system.console class, right? That's the full name of the class, and then we're calling the method in that class. However, you'll notice that I didn't have to use the word system here. Why not? Well, because we used a using statement at the very outset of this code file, which says, I want you to look inside of these namespaces whenever you find a class reference that you don't recognize. And so the C-sharp compiler, it finds the word console, and it says, hmm, I wonder where that came from. And it begins to look through the namespaces listed in the code file, and it says, oh yeah, I found a class name called console inside of a namespace called system. All right? And so it thinks, all right, well that must be the console class that he's talking about. Now, occasionally, you might have two classes with the same name, and you've added using statements for each of these inside of your code file. When that happens, you merely need to disambiguate by adding the full name of the class instead of relying simply on the using statement. Now, you'll notice here that by default, the program.cs file has a number of different using statements. In my text editor, they're kind of faded out a little bit, which indicates to me that they're not being utilized at this moment. So we could remove unused using statements from our code and our code will compile just fine. And this is a convenience for us that was set up for us by whoever created the project template for a console window application. So to further illustrate this idea, let's talk about how we can go about using the dot in a framework class library to do meaningful things and how we would go about finding the source code or the classes we need to do something cool in our application. So for example, maybe I want to write data to a text file. How could I go about doing that? Well, I might open up Bing.com and I might search for and I'm going to type in site Microsoft.com. So I'm going to limit the search results to just those that are returned by Microsoft.com. All right, this is going to going to help me find the documentation specifically created by Microsoft as opposed to third party articles or whatever the case might be. So site colon Microsoft.com and then I might just say write to a text file and then using C sharp, right? And so one of the top results are from MSDN.Microsoft.com and MSDN stands for the Microsoft Developer Network. This is your primary source of information as a software developer on the Microsoft platform. And so in this case, here's a how to article that will describe the code that we would need to write in order to write data to a file. Here's a long code example. In fact, it gives us three examples in one. And we could use one of these examples in our application in order to in order to write file, write data to a file. And we might decide to go ahead and use the second example. It comes close to what we want to work with here. And I copy and paste it into my application. And I might remove some of the extra some of the extra information here just because I don't need it. And I may need to modify this path. I believe I created a folder called lesson 17 for this purpose. And notice that it's going to use a class name file and a method called write all text. Now in this particular case, notice that we already are given the full name, the full namespace of this file class system.io.file. Now what we could do is actually remove that from here and go up and add a using statement for system.io, like so. All right, and notice that the compiler will find it and we'll be able to run our application. And got a little message there because I was in release mode. Let's go back to debug and start that over again. All right, and we don't get any feedback there. But if we were to open up our lesson 17 folder, we would be able to find the text in in a text file. Great. Okay, so now we can use that little snippet of code to do what we want to do. But notice it all started by searching on MSDN finding a code snippet that we could use and then we can modify it and add our own text here. That we want to then we want to write this to our file, right? Okay, so that we start stitching things together. So that's one thing that we can one way that we can find the features inside of the dotnet framework class library that we need is to search on MSDN. Let's try one more quick example. And let's go back here back to Bing.com. And here again, I want to go site colon Microsoft com. And then I want to do C sharp download HTML as a string. And I might find another another reference. Now this is a different style web page. There is one web page on MSDN for every class and every method in the dotnet framework class library. So in this case, we're looking at a specific page for this download string method. And if you were to look at the remarks and and some of the additional the syntax and some of the exceptions that it would throw, what we ultimately get to is a little is a little snippet of code that we can copy and that we can paste inside of our application. Now notice what happens this time. It does not recognize the term web client. Why not? Well, we may not have the assembly referenced in our project or we may have the assembly reference, but we do not have a using statement that would include the web client class. So to remedy this, I'm going to hit control period on my keyboard. And it says that it found this class in system.net. So we can automatically add a using statement for the system.net class by merely hitting the enter key on my keyboard. Or I can just go ahead and say let's go ahead and use the full name of the class here. I'll choose the first option using the arrow keys in the enter key on my keyboard. Notice what happens it adds a using statement for system.net. And then notice that the web client class is now found. It is obviously in a different color. There's no red squiggly line. So it looks like it found the correct class that we're looking for. Now we merely need to get it a URL. So let's try msdn.microsoft.com like so. And then we will just write this out to screen reply. And then we might even rework our application and attempt to save that into our text file as well. All right, so let's see what we get here. Hopefully this will work. We'll run the application. All right, it took a moment, but it loaded up a bunch of HTML into our console window. We can see the closing body in the closing HTML tag. Now if we were to go back to our folder and find our lesson 17 folder and open up our text file, we see here is the full web page that we scraped off of msdn.microsoft.com. So that pretty much wraps up what I wanted to say in this lesson. We are able to utilize the classes and methods in the .NET Framework class library. And we can find what we need by doing simple searches in bing.com using site colon microsoft.com to find the classes and the methods that we want to work with. Once we find those classes that we want to work with and we find maybe even little code snippets, we can copy those into our program. And we may need to at that point fix the references to those classes. Now in this first case, remember that it gave us the full name of our file class, system.io.file. But in the second case, we had to provide the using statement system.net in order for the compiler to find the class that we were wanting to reference and work with. And ultimately we did that with hitting control period on the keyboard to add a using statement to the very top of our code. We talked about the purpose of namespaces to provide disambiguity between class names. We talked about the using statement as a way of creating a shortcut or a context and say we are not talking about every class in the .NET Framework class library. We are only talking about the classes that happen to be in these namespaces. So if you find Mr. C. Sharpe compiler, if you find a class that you don't recognize, look in those namespaces first before you complain. So we are going to continue on these ideas in the next lesson. We'll see you there. Thank you. Hi, I'm Bob Taber with Developer University. For more of my training videos for beginners, please visit me at devu.com. Previously I said that the creator of the console window application project template added references to those assemblies in the .NET Framework class library that we as developers might find useful for the majority of use cases. However, if we need an assembly containing some portion of the .NET Framework class library that has not already been added to our project, then we can simply add a reference to it. So this is one of three ways that we'll demonstrate on how to add a reference to an assembly. The first being an assembly from the .NET Framework class library. Now there's a number of different ways to go about this. The easiest way I think is to go to the solution explorer and then right click on references and select add reference. And here you can see there are a series of I guess tabs along the left-hand side that would allow us to choose from the various types of assemblies that are available to us. We want to choose the framework. And these are all of the assemblies that are part of the .NET Framework class library. Now you can see that there's already check marks next to a number of the system.this, system.that, system.net.http. And these contain a number of different classes each with many methods. And these are just automatically accessible if we needed something that is not contained here. Then we could choose, for example, to just select a check mark next to the one that we want to add to our project, system.net, and click OK. And you can see that it's added a reference to system.net into our project in the solution explorer. Now we can reference any of the classes and utilize any of the methods in that particular assembly. So that's one way if we need to access some part of the .NET Framework class library. Now, in addition to that, there are libraries that are created both by Microsoft and there are libraries that are created by open source contributors, other companies that are provided for free for very specific purposes in our applications. And so these are often common features that many applications need. That's why they've been open sourced. However, they're available through a special tool called NuGet, which is a repository that's maintained by a foundation supported by Microsoft, but ultimately its own entity. All right. And so there are a number of different ways to work with NuGet in Visual Studio. I'm going to choose the visual way to do it. And I find that to be the easiest for those who are just getting started and for me because I'm a more visual kind of person. There's also a textual, almost command line style interface that would allow you to do similar sorts of things and even script these things. So let's go to the tools menu and select NuGet package manager and then manage NuGet packages for solution. This will open up a tab undoubtedly no matter what you see on my screen. It'll look different on your screen because this is going under active development for the last few years and it has changed frequently. Now, if there were a package that we wanted to add to our solution, we could simply search for it. Typically, we can learn about these sorts of things through blog posts and what have you. Say for example, I wanted to access a database from my console window application and I wanted to use the entity framework API from Microsoft. It's available through as a NuGet package through this manage NuGet packages for solution dialogue. So I can select it as one of the options. You can see it's one of the most frequently downloaded. Furthermore, I would then choose which project in my solution that I wanted to add it to and I can choose the install button. There's some other options as well. I'll leave you to investigate those on your own. I'm going to go ahead and click OK. I agree to the terms for using the entity framework. In this particular case, it installed just a number of references to assemblies and copied them down locally to my computer. Now, depending on the type of package, it could not only contain .NET assemblies but also sample source code files. It could actually run macros inside of Visual Studio. It could include things like style sheets and HTML and even graphical assets that it will include your project. So this is the second way that we can go about adding assemblies and more to our projects. But the third way that I want to talk about is whenever we want to add a reference to a class library that we've created. Now, we haven't created a class library up to this point, so this is a perfect opportunity to do that and then add a reference to it in our project. So what I'll do is start off by creating a new project. Let me go ahead and let you see my entire screen here. And in the new project dialog, I want to make sure to choose C sharp. And then I want to choose class library. Notice that I chose the one that doesn't have a little NuGet logo next to it. It looks like several books in the little C sharp logo. Now, this will undoubtedly look different to you, but just make sure that it's a regular old class library. And here we're going to call this my code library and click OK. And I'm going to go ahead and say I don't really care to save my other solution there. All right. And so inside of this, you can see that I don't have a program dot CS. All I have is a class one dot CS. There's no static void main. And so what I'll call this is the scrape class and we'll have one public method. So public string scrape web page. And we'll create a version of this where you provide it just the URL. And then we'll create a second version of this where you provide the URL and a file path. All right. And so in this first case, I'm just going to copy down some of the code that we've worked with previously to create this functionality, where we were actually using this web client to go out, download a page and then save it to a text file. I'm just going to generalize this. Remember what I did previously when I hit control period on the keyboard in order to add a reference to system dot net or add a using statement for system dot net. And the next thing I'm going to do here is actually replace this hard coded string with whatever gets passed in by the end user. All right. Finally, I'm also going to have to add or resolve this reference to the file class. It's in the system dot IO namespace. So I'm going to add a using statement for that. However, in this specific case, I'm not going to write this to a file and this overloaded version of it. In fact, what I'll just do is turn whatever's been input actually downloaded from client dot download string. All right. Now the second version will do something almost identical. Here, let me replace this with URL and get rid of console dot right line. And we'll go ahead and write this to the file path that was passed in and then we'll return the reply. Now truth be told, this might be a good situation where I could actually take these lines of code and create a private helper method out of them. And maybe that's a good idea. Let's do that right now. Private string get webpage. All right. And we'll pass in the URL. And so here. And now both of these can just call whoops, get webpage. So and here we'll go string return or reply goes get webpage. All right. See what I did there? I was able to use a private helper method to encapsulate the functionality of actually getting the webpage itself. And then in this case, I was able to extend the scrape webpage method to include writing that to an actual file path. All right. So now that I've created this and let me go ahead and rename this file as well by right clicking on it and selecting rename. And I'm going to choose to name this file scrape as well. I could name it anything I want. It won't matter because the name of the class itself is scrape. But at this point now, I'm going to go ahead and build the solution. It looks like it built. In fact, let me go ahead and build a release version of this. All right. Great. So now let's open up a second version of Visual Studio. And I'm going to call this my client. So this will be a console application called my client. And we'll click OK. And what I want to do is to, first of all, add a reference to that DLL that we created just a moment ago. So I'm going to go and right click on references and select add a reference. All right. So here I have some choices. Ideally, I would be able to look and find it in the same solution. We'll come back to that and do it in just a little bit here. But I may have to go and actually browse through the file system to find this. And unfortunately, this is popping off the screen. However, hopefully we can work our way through this. And I'm going to navigate to the bin directory into the release directory and find my code library. And then I'm going to select the add button and then click OK. All right. And so now that I've done that, what I should be able to do is get to the scrape class, right? But it doesn't see the scrape class. So I'm going to hit control period on my keyboard and notice that it will find the correct using statement. The using my code library namespace scrape my scrape equals new scrape. Okay. And now I should be able to go my scrape dot. And there we go. Scrape webpage. And I should be able to give it a URL. So let's go. All right. And that should return a string. So string return, or actually just value equals. All right, let's move this over a little bit. And then I should be able to print that to screen. So console right. So and now we should be able to run the application. And it takes a moment, but it pops up. All right. So what we're able to do there, well, we created a reusable library now. So whenever we want to scrape a webpage, we can utilize this in any of our other projects. Now, did you find how inconvenient it was to actually go and search around whenever we wanted to add a reference to it? I had to go and browse through the through all my projects and everything. But I do want you to notice one thing about what happened after we did that. Let's go to my projects and let's find that client and let's navigate into the bin directory. And notice that it copied my code library.dll into the bin directory for the client application. So that's one of the things that it will do with any of the third party assemblies that will that will utilize. All right. It's easier if we were to start this over from scratch and we were to create a single solution that had both the client and the code library in the same in the same solution. So let's do that now. I'm going to actually open up a third copy of Visual Studio and here let's create a new solution. So what I'm going to do is actually scroll all the way to the bottom and choose other project types and choose Visual Studio solutions and find blank solution. Now, this might be in a different place. So you may have to kind of hunt around for it, but you ultimately want to choose blank solution that should be available to you. And we're going to call this lesson 18. So the solutions name will be lesson 18. But what we're going to do is add projects to this solution. So the first project that I'm going to add and there's a number of ways to do this, like add, but it goes off to the right hand side of screen. I could add new project file, add new project. And then we're going to choose the class library. So we'll call this the scrape library. All right. And then I'm going to choose to create another another project and add it to our solution called. Of type console application. And this will be the scrape client. Okay. All right. So in our scrape library, what I do just for simplicity sake is actually go to the work that we've done here a moment ago. And I'm going to copy all of this. Like so. And let's come back here and I want to paste all this in. Like so. And yes, I'm going to have to resolve these class names by adding using statements here and here as well. All right. That should work. Looks like I actually lost my class name. So let's go public class. Great. Let's make sure to put everything inside of it. All right. There we go. Now we get it working. And I'll rename this as well to just scrape. Great. I could have left it called the class one, but that that'll work just fine. Okay. So I'm going to go ahead and build that right clicked on the project name and selected build. All right. And now what I want to do in the client to utilize that class library is I need to add a reference to it. So here again, I'm going to right click and select add reference. I'm going to go to projects and notice if solution is selected, the scrape library will be an option. I'll choose that and click OK. And now we can utilize scrape library in our application. So let's go ahead and just type in the word scrape. Oops, scrape dot. And it's not going to find it. So I'm going to hit control period and I need to add a using statement. Since I renamed it, now it's called scrape library. So I'm going to add the scrape library namespace to using statement to the code file. So scrape my scrape. In fact, I don't have to do all that, right? I can just copy and paste it from the previous client like so. And we can rerun the application. Whoops. And it says a project with an output type of class library cannot be started directly. All right. Why do you suppose that happened? Well, because there are actually two projects now in my solution. And you can't execute a library, correct? So what we need to do is right click on the client project and select set a startup project. And now, let's go ahead and close that. When we attempt to run the application, it'll work. Furthermore, if we were to make any changes to how the library actually works, let's say, what could we do here? That's interesting. Let's do this. Make a change in one spot. And then I'll go content plus equals. That's all folks for the very end. All right. Of that string that's returned. And I'll return content this time. All right. Let's make sure we add everything there. So we've made pretty big, pretty big change to the application. And now when I run the application, it will recompile the DLL. It will add it to our project. And at the very end, it adds, that's all folks. Okay. It's just the only thing I can think of off the top of my head. All right. So hopefully now you can see that there are several different ways to add assemblies. If it's part of the .NET framework class library, then obviously there's a way to do that. If it's a free or open source package that's available from NuGet, we can use the NuGet package manager, or we can create our own third party class library and then add a reference to it by browsing. Or if we were to create the client and the library inside of the same solution, then we can reference it in the add reference dialog, but just under the project solution option. And we get the added benefit of being able to make updates, not having to go through two copies of Visual Studio to update it. It'll update the next time we hit run. It'll recompile it and everything. Okay. So at any rate, that's pretty much it for this lesson. We'll continue on in the next lesson. We'll see you there. Thanks. Hi, I'm Bob Tabor with Developer University. For more of my training videos for beginners, please visit me at devu.com. Previously, we looked at arrays, which allowed us to keep a sequence or a group of related data together inside of the same variable. We would create an array by providing a data type, and so each item in the array had to be of that data type. We would also provide the number of elements we expected in the array by defining that number between a set of square brackets. Now that we have that predefined sized array, we could add items into each element of the array with three values out of each of the elements of the array by indexing into the array using a zero-based index to index in and address one specific element of the array. Now once we had the data collected into an array, we could do some interesting things. We could iterate through the array and investigate each element of the array. Or we could even pass the array around as if it were one variable, pass it in, for example, as an input parameter to a method. But you recall at that time I also said that at some point we would talk about collections. And I even gave collections a nickname calling them arrays on steroids, alright? And I think you're going to agree after this lesson that collections are great whenever you're working with all data types, especially those data types, those custom data types that we've been working with up to this point in this series of lessons, for example the car class that we created ourselves. Now as far as the .NET Framework class library is concerned, it will often use both arrays and collections depending on the need. But I think you will probably wind up preferring to use collections in your applications because of the rich filtering, sorting, and aggregation features that are available to collections through a technology, a language called LINK, L-I-N-Q, which stands for the Language Integrated Query. It was a very innovative feature whenever it was first introduced back a number of years ago in C-Sharp and other .NET languages. Other languages have since implemented something similar to it. But we're going to dive into that topic of LINK and what you can do with it in the very next lesson. But first of all, let's talk about collections. We're going to talk about two collections specifically, lists and dictionaries. Now truth be told, there's probably a dozen additional varieties of collections that you could use for very specific purposes. They each have a superpower. They each have a very specific use case where they're intended to be used. I find myself using lists and dictionaries 95% of the time, so we're going to focus on those for this lesson. But after this lesson, by all means, feel free to go off and learn all the additional collections that are available to you and what they can do that's a little bit different than the list in the dictionary. Okay, so suppose that I have a number of cars on my car a lot and I want to write an application that allows me to manage them. So I need some way to collect all of the individual instances of the car class together into a single array or collection. Now again, I might use an array of cars, but I think I'm probably going to choose to use a collection because of the added features that I'm going to gain using collections. We're going to talk about a bunch of different types of collections, but I want to start off with kind of a conversation about an older style of collection that's no longer used anymore to show why there's a newer style collection that's available and it'll help you maybe understand that idea a little bit better. As you can see, I've got a project called Working with Collections already set up here. Please take a moment and create a new console window project. I'm also going to paste in two classes that I've defined, simplified version of the car class that we've used before. And then also I'm going to create a book class as you can see there at the bottom, very simple classes. And the next thing that I'm going to do is actually paste in some code to actually create new instances of each of these classes and then populate their values. So you may want to pause the video yet a third time and copy in the code that I have copied to screen there as well. Alright, so the very first thing that I'm going to want to do is to work with a collection and I'm going to work with something called an ArrayList. And so let me just say this about ArrayLists that they are dynamically sized, which is one of the great benefits. You don't have to do anything to say I need to add one more item and another item and another item. Remember with Arrays, I said it was possible to resize an Array, but it's a little bit of an advanced operation, not so with an ArrayList. So that's one of the big benefits. You can just keep adding items to it and it'll be just fine. It'll also support cool features like sorting, you can easily remove items from the collection and so on. So let's go ahead and create a new instance of this ArrayList. And when I do notice that we don't already have a reference or a using statement to a namespace. So what I'll have to do is hit control period on my keyboard and you can see that it is in a namespace called using system.collections. I'll go ahead and add that namespace to my project. And so we'll create a new one called myArrayList equals newArrayList like so. And now that I have myArrayList, I can begin to add items to the ArrayList. Like for example, the car, the first car, and then I can add a second car like so. Now one of the problems with the old style collections like the ArrayList is that there was no easy way to actually limit the type of data that would be stored inside of the Array. So for example, I want to work with automobiles, but I might accidentally add a book into the Array and it will work just fine. All right, there's no complaints. So the old style collections are not strongly typed in so much that you can put anything inside of a collection at first glance that might seem great. But what if I wanted to actually then print out a list of all of the cars, makes, and models? So let me start by at the very bottom here, type console.readline so we can get through that formality. And then I'm going to just do a for each. And what am I going to work with here? Let's just say I'm going to work for each car car in my ArrayList. All right, and then I might go console dot write line. And let's just go car dot make like so. All right, and print that to screen and let's run the application. And we will get an exception whenever we hit the third item in our in our ArrayList notice that it is printed the first two to screen. But when we get to the book, it says that there's an invalid cast exception. In other words, we could not convert a car or rather a book, which was the third item in the ArrayList into a car. So when we get to this spot in our, as we're iterating through each of the items in our ArrayList, we're going to hit a problem here. And the fundamental problem is that we allowed our collection to store something other than cars. So we cannot work with these collections in a strongly typed fashion. Now what I can do, what's one of the neat features here is that I can actually remove that item prior to going into that for each list. And we should just be able to execute the application without problem. All right, so that is at least one of the good benefits there. But unfortunately, the downsides outweigh the benefits. And so let's go ahead and take a look at the newer style collections. The first I said was that we were going to look at a list and more correctly, we're going to look at something called a generic list. So often you'll see it referred to as a list of T like so. All right. And so that that of T in the term generic might require a little bit of explanation. When .NET was first released, the first set of collections allow you to put anything you wanted into them like we saw here just a moment ago. Now it might make sense in some contexts, but typically it doesn't. And it leads to potential errors like you saw. Now, at some point then C sharp introduced the notion of generics. And specifically for our purposes, they released a series of generic collections. And so a collection is essentially generic, but it requires that you make it specific by giving it the data type that should be allowed inside of that collection. So we have a generic list, but we're going to make it a specific list to cars so that we can't even add a book to that collection. So let's attempt to do this one more time. This time we're going to go list and notice that I'm using angle brackets. And in between the angle brackets, I'm going to say what data type I want to use. In this case, I want to use the car data type. So list of car called my list equals new list of car like so. And at this point, we can go ahead and add car one just fine. We can add car list to my list. We can add car two just fine. But what happens when we attempt to add the book into our list? Well, at the point when we attempt to add the book to the list, we get an exception. We hover over and it says it cannot convert a book to a car. And that makes a little bit more sense. It is specific to a car data type, so we cannot add a book to that list. But from this point on, we can work with it now with some confidence. So each car, car in my list. And we can just do car.model like so. And we would get what we would expect here. Great. A list of our car models. So that's one of the big benefits of working with a generic type, is that it allows us to work with a specific data type and only allow those types into our collection. So this is probably the most popular of all of the collections available. But I'm going to show you one additional collection called a dictionary. And a dictionary is similar to like think of Webster's dictionary where you have a word and you look it up in alphabetical order and find the word that you want a definition of. And then once you find the word, you can look to its right and it will have the definition. So there is a key which is the word itself that we want to look up. And then there is the definition next to it. So there are two components to each entry in a dictionary. There's the key and then the value itself. And so typically when you see a generic dictionary mentioned, it's going to be kind of listed like this dictionary of t key, t value. So in this case what we'll do is specify the data type of the key. This allows us to find one specific item by the key. Now the key should be something that is unique to every entry in the dictionary. In the case of people there might be some identifier. It could be a customer ID in your system. It could be a social security number if you're in the United States. But something that uniquely identifies one entity inside of that dictionary. And then the value can be of any data type. In the case of again a customer you might have the customer ID being the key because the customer object itself is the value that we actually want to get access to. Now in our case this seems a little bit weak because our car class only has make and model. And we know that we can have multiple cars that have the exact same make and model. They may have different colors. They might have been created in different years. But you can have multiple cars in the car lot that have the exact same make and model. So neither of these are good candidates for keys. But there is something called a vehicle identification number. So let's go crop string and what's called as a VIN. That will differentiate every car in the world that's been created. So what I'll do is come back up here to the definition and the car one VIN. And I'm just going to use a very short VIN number. I think they're typically like 18 or 24 characters long or something along that. Not exactly sure but this should uniquely identify every car in the world, especially every car in our car lot. So now what I can do is create a dictionary of my cars by starting off and saying something like dictionary and then we're going to give it the two data types. The VIN will be a type string and then the actual value will be of type car. We're going to call this my dictionary equals new dictionary of string car. Notice that IntelliSense helped me out by essentially giving me a lot of that and I can just hit the semicolon at the end of the line for it to type out that entire phrase. Now that I have this what I can do is go mydictionary.add and it will do car one dot VIN and pass in car as the actual value. So the car one VIN again is our key into the actual car one itself. And likewise we'll go add car two dot VIN and car two. And so at this point here if I were to attempt to find a given item, so console dot our right line and I need to find a specific car on my car lot I can allow a user to type in the VIN number and I can look it up in the dictionary quite easily. So say for example I want to do mydictionary dot and then there's a number of different ways to go about this. I think probably one of the easiest ways actually let's go back and not use the dot. And here I'm actually going to use the key itself. So we'll call this V2 and then now we can reference a specific item in the dictionary of type car so we can get the make for example and print that out to string like so. Alright we're able to find the geo that way. Alright so hopefully that makes sense. Let's continue on if you recall when we originally were looking at and let me comment all this out. We were looking at arrays. I said there's some interesting things you can do to initialize an array with values like we see here. Here we're creating a string of name an array of strings called names and to initialize it I give it a collection of names that are common to limited. And so now I have an array that has four elements in it and it's already been initialized with the values. You can do the same thing with the objects to initialize objects at the point of instantiation. So to do that we'll use an object initialization syntax. In fact let's go ahead and just approve this all works. Let's comment out everything we have up here as well. And get rid of the cars in the book. And we'll come down here and go car. Car 1 equals new car and then notice what I do. I use that same syntax the curly braces. And inside of here what I can do is actually define all the values. So make equals let's just dream large here and go make and the model would be a 750 and we'll make it an LI and then we'll also give it a vehicle information of C3. Alright like so. So now I've done actually three things in one line of code. I create a new variable called car. I create a new instance of car in the computer's memory and now I'm getting access to that address in memory by using the car 1 label, the variable name. And then I go ahead and populate the properties of the car object at the moment that I create that new instance by using this object initializer syntax. Some people don't like this. It looks like it might be doing too much in one line of code but I think you'll find that if you ever do need to hard code examples like I do frequently that this shortened syntax actually saves you several lines of code and it's just fine. It's valid code. Alright so let's go ahead and while we're working here let's go ahead and create a Toyota. We'll set the model equal to a forerunner and we'll give that event of D4. Alright like so. Okay. And so now we can work with the cars just like we did before but in and of itself this might not be so interesting but this is the object initializer syntax and we can take this one step further when it comes to working with collections. So we can use collection initializer syntax. And I want to point out one other thing that we didn't have to use a constructor to make this work like we looked at before that we're able to regardless of the constructor go ahead and set these attributes just like we used the syntax there. Alright but let's now talk about collection initializer which can look a little hairy but it's essentially the same thing we're just taking it to the next level here. So in this particular case let's go ahead and create a list of car called myList equals a new list of car. Alright now at this point what I can do and notice that I put this on separate lines here I typically might keep this on the same line just for my own sanity here and now inside of this new empty list of cars I can create a series of car objects like so. In fact what I can do at this point then is use an object initializer inside of that so here we'll go make to be equals a olds build and then we'll set the model equal to cutlass supreme and then the number we'll set that equal to a v5 so comma and then we'll create another new car to add to this list of cars and we'll set its object initializer setting its make equal to Nissan and its model equal to an Ultima and then finally it's been equal to f6 something like that. And now what I've done all in one line of code essentially is I've created a collection and I've added two objects and each of those objects I went ahead and already initialized all of their property values. So there's a lot going on there in just that one line of code. Great. So at any rate just wanted to recap the things that we talked about in this lesson. First of all we talked about the difference between arrays and collections and I've promised that there will be a more obvious set of features that are available to collections which we'll learn about in the next video. We talked about the old style collections versus the new generic collections and we said generic collections are superior because they allow us to make sure that we're only adding types, specific types to our collections. So we make a generic collection specific by passing in the data type that should be allowed to be referenced inside of that collection. Then we looked at object initializers, just a shorthand syntax force initializing the properties of a new instance of an object and then finally taking that one step further within a collection initializer where not only are we creating a new collection but then initializing it with new instances of the car collection in both of those cases then we are using object initializers. So we can do it all on one line of code. Now honestly unless you're building a lot of example code like I do you may not see this as often unless you are creating some hard coded objects for use within your application. But I wanted you to be aware of that syntax nonetheless because we're going to use it again in the next lesson and we'll see you there. Thank you. Hi, I'm Bob Taber with Developer University. For more of my training videos for beginners please visit me at devu.com. In this lesson we're going to look at link the language integrated query syntax that was introduced some years ago to provide a way to filter, sort and perform other aggregate operations on collections of our data types. And so we'll demonstrate two different styles of link syntax. There's a query syntax that will resemble the structured query language SQL for querying databases. So if you're already familiar with SQL this will at least feel familiar. Then there's also a method syntax which might feel more familiar to C sharp developers. However, there is one little strange nomenclature thingy that we got to figure out. But I think it's pretty easy. I think I have a good way of explaining it to you and hopefully you'll understand what it's trying to do there. But what I'd recommend is you find the code for this lesson. There should be a before and after folder and you want to copy the code in the before folder into your projects directory and then open it up and you'll be where I'm at right now in the understanding link project. You can see that I merely created a car class and then I also have here a collection initialized class filled with cars filled with attributes that we'll be able to search and sort on and that'll give us something to work with here. Alright, so what I want to do to begin with is to kind of show you a comparison between links query syntax and then links method syntax to do the exact same thing and you'll see the obvious difference and we'll talk about the ways in which they're different. But let's begin with a query where we want to find all BMWs in this list of cars called my cars. So it's as easy as this and we'll talk about the var keyword here in just a moment. But BMWs equals from car in cars oops, from car in my cars where car.make is equal to BMW select car. And so now let's come down here and print all those guys out so let's go to console.write line and let's provide the vehicle information number so let's do 01 so we'll go actually let's do this let's go for each tab tab var car in my and BMWs console.write line and then we'll go car.model and car.vin and let's go ahead and add some of our replacement characters in there now let's go ahead and run the application and we see that we get three cars that have been returned so of all the cars I think there's five or six three of them with those VINs A1, C3 and E5 are BMWs and if you take a look at the data that would be correct so very quick concise way of finding only those cars that match that criteria all right what if we wanted to add additional criteria so say for example we wanted to also see where the cars year equals 2010 we could do that and we run the application and now we see that it just finds one of the BMWs was created in 2010 it is this last one the 555i all right so that is the language integrated query syntax the query syntax of link let's go ahead and comment that out and compare that to the method syntax and so here we'll go bar BMWs equals my cars dot dot where all right so this will give us all the BMWs so let's go ahead and run that and it gives us the same three that we got before now what if we wanted to also find only those where the year is equal to 2010 we would do that and so you can see we found just that e5 that last one all right so this might take a little explanation here for the moment let's just ignore this last part we'll talk about that in a moment but what you see here in fact the whole thing in between the opening and closing parentheses is called a lambda expression and you can think of it as a mini method essentially what will happen is you say given p so given an instance of the collection only return back to me those instances of car where the make is equal to BMW see how easy that is again just a mini method you can kind of think of this as like the input parameter and then this is just some condition and when it's true then return that instance and add it into this little collection over here so now that we have a subset of all of the available cars on our car lot furthermore I just added the logical and operator and said and make sure also that it was in 2010 okay well that filters out two of them so again mini method lambda expressions are just mini methods so for any given item in the collection it has to match that criteria and if it does then we can add it to this little subset collection over here that I'm calling BMWs all right so the var keyword it has a very different connotation in C sharp than it does in other programming languages so here I would ask that you forget what you know about var from maybe JavaScript or Visual Basic or some other programming language it does not mean the same thing in this case the var keyword says that we're going to let the compiler figure out what the correct data type is I'm not even sure what gets returned from this little query that we do here if you were to hover your mouse cursor over the where you can see that it's going to return back an innumerable of car what's that not entirely sure doesn't really matter I don't care what it is I know that it is a collection of cars and kind of to kind of prove this point we'll talk about this in just a moment a little bit later where the var keyword can really come in handy because we truly don't know what it is that's being created by our link queries so again the var keyword it's still strongly typed we're just going to let the compiler figure out what the type is at the point when the code is compiled alright so let's move on and let's take a look at a few other examples so I may want to find a list an ordered list of cars so I might go ordered cars equals from car in my cars order by car dot year descending select car alright so that's how I would take all of my cars and order them in descending order by their year and let's just change this from BMW let's put this to order cars like so and this might help we actually saw the year itself so I'll add in car dot year as well and let's go ahead and run the application and so you can see it starts at 2010 and in descending order works its way back to 2008 awesome alright so that same query if we were to do it in using the method syntax instead of the query syntax it would look something like this so order cars equals my cars dot order by descending so given each item in the collection only return those or actually order them by the year like so and we should see the same grouping and we do starts at 2010 works its way back to 2008 awesome so again in my opinion this is more concise the only conceptual hurdle you got to jump over is just make sure you understand what a lambda expression is in this particular case it's not a filter we're just saying given each item in our collection we want to order by this particular property the year and then add that ordered item to our collection of our new collection of cars over here now there's a lot of interesting things that we can do and I'm only going to work with the method syntax from this point on and so the first we might do something like this for example if we wanted to find just the first item so let's go ahead and grab this and maybe we want to find the first item where the make is equal to BMW like so so this will give us the first car the first BMW car in the list that it finds so let's go ahead and console in fact let's change the name of this to first BMW so console. Right line first BMW dot should be sufficient let's run that so we can find that the first BMW in the list was A1 or we can do the first BMW and we can actually start by ordering by descending given the year we can see I'm chaining these together we've talked about method chaining before this will return a collection of cars and then this will return a single car in that collection and then we'll print that single items then out to window in this case E5 so the list is first sorted and then we grab the first one that matches our criteria of BMW so that's how we can use first comment that out we can also do something like this console dot right line and inside of here let's go mycars dot true for all and say the year is greater than 2012 and one more right there so is it true mycars in my car lot are that every one of them is greater than 2012 that would be false alright well then how about are they all greater than at least 2009 alright that's still false because we have at least one that's that was created in 2008 we were to change this to 2007 are they all at least greater than 2007 true alright so that's true for all very helpful in order to aggregate and look across all of them and see is this true for all the items in my list we can also even do something interesting like this instead of doing this for each statement where it's essentially what at least two if not four lines of code we can create a for each like so mycars dot for each and then inside of here for every item let's just do a console dot line and in here I can do a p dot then and p dot the sticker price alright so let's go ahead and do that in fact just do that as well well 0 hopefully that all makes sense else from that see what happens alright so here we are we're able to list them all out and format their values so you see how much more compact this looks than what we were writing here do it all in one line of code again we're passing in for every single item in our collection just call console dot right line and then use that particular items then sticker price inside of our our formatted string ok here's another interesting example of this in each case we want to go so mycars dot for each in fact here let's do this before that line of code let's keep that one and then go mycars dot for each and in this case I want to perform an operation on each of the data inside of there so I might take the sticker price and reduce it by three thousand dollars actually let's go minus equal to so this will take the sticker price and subtract three thousand dollars from the sticker price of every car in my collection so you can see now what was if we could kind of get a comparison going here see unfortunately not going to be very easy to do but you can see that what was fifty five thousand dollars is now fifty two thousand what was thirty five is now thirty two thousand and so on ok so again a lot of functionality in a very small space let's continue on with this thought and go and do something like my cars dot exists do we have a car in our car lot where the model is equal to the seven forty five li true or false console right line and so this will be the turn true or false yes we have at least one item in our inventory where the model is equal to seven forty li great now here's another good aggregate function here let's just do console right line and let's go my cars dot some and here we'll say sum up all the sticker prices let's see what the total value of our car lot is right now and you can see that it's about two hundred forty seven thousand so actually there should probably be a better way to format that using a format code but hopefully you get the idea we're able to sum up a single field across all objects in our collection of cars alright alright and there's so many other things that I can show you but I don't want to overwhelm you but I want this just to go in one ear and out the other for now this var keyword we've looked at and I said that we use it because we want the compiler to figure out what the data type is sometimes it's easy for us to figure out what the data type of something is sometimes it's not so easy so to kind of illustrate this let's do a console dot right line and what I want to do is call on my cars I'm going to call the get type in fact all data types in dot net have this get type method declared because it is it's declared on the grandparent of all objects called system dot object it defines this method called get type which will tell us what the type of a given object is and we can print it to screen so in this first case what is my cars well my cars is a generic list of the car data type so this understanding link is the namespace and specifically though it's the car type so we're basically saying this is a list of T a list of car right so that's what's being printed out whenever we're looking at what the cars are and that's pretty easy to see because we defined it here but once we perform an operation like one of these here let's just see ordered cars we'll copy that again and stick it down here and then I'm going to do console dot right line ordered cars dot get type and they'll show us what the data type is for ordered cars let's go ahead and compare the two right now in this case you can see that we're no longer dealing with a list of car even though under the hood we know we're working with a list of cars the way that it's represented in dot net is that it's actually an ordered innumerable so an ordered list of system dot of the link dot car understanding link dot car okay so again okay that makes sense that's an ordered version of cars how about just a regular old where statement so let's do that let's copy that and see what the data type of that is and then do the same sort of thing here where we're going to see what that is let's go ahead and run the application again alright here's this third one so this second one was an ordered innumerable then the one where we just called the where was an innumerable plus the where list iterator alright so things are starting to get a little funky here right it might be difficult for us to be able to express this ourselves if it were not for this var keyword and so the var keyword is essential to help us to be able to create these very complex queries and not have to worry about what the data type of it is that's returned we know that it is a type of list it's an innumerable list whether it's ordered or not and we can we can for each our way through it or whatever the case might be now the last thing that I want to demonstrate is I'm going to take this this first query here and I'm going to pop it all the way to the bottom again if this stuff doesn't make sense don't worry about it too much I wanted to go in one area out the other just to kind of explain again the value of of the the var keyword in this case let's change something about this let's call this my new cars alright and here I'm not going to return cars I'm actually going to do what's called a projection I'm going to only take certain values certain properties of a car and I'm going to project them into a new data type where am I defining that data type I'm not going to define the data type it's an anonymous type what's the name I have no idea it's anonymous alright so we can define types at runtime and only choose those properties that we need in the type for the moment why may not be obvious just yet but I want you understand that there is this idea what we can do here so in this case let us merely let's pull out just a few things like the car car dot make and the car whoops I'm sorry this should be select new car dot make car dot model that's all we want we're going to leave all the other attributes of car alone we're going to only take these two values put them into a new anonymous type where's the type defined nowhere we just made it up off top of our heads and we're going to save each of those anonymous types into a new collection of anonymous types alright alright so let's go console dot right line and then take a look at new cars dot get type and let's run the application yet another time alright and so this time you can see that we also here have an innumerable plus where select list iterator whatever that means but then notice that the data type involves something called an anonymous type that has two attributes two string attributes which would be the make and the model alright and so I'll say all that to say this that whenever you're working with link there's a lot going on under the hood to make it all very easy and accessible but it all depends on defining your types as var which says we'll let the compiler figure out what the type is we're not so worried about it because the data type might be so crazy that we can't even comprehend what it is alright so hopefully that makes sense just to kind of recap the things that we talked about talked about the difference between the the query syntax and the method syntax we looked at a number of different examples of the various link extension methods that were available we saw how we could kind of break apart an individual lambda expression to better understand that essentially we're saying each item in the collection run this little mini method against it and return back a given item that matches that criteria and then we looked at how to how to tell what the types were and what the value of the var keyword is and then we looked at anonymous types alright so we covered a lot of ground I didn't explain everything in great detail but the key here is just to kind of look at that little formula and try to understand what it needs look for examples online make yourself a little cheat cheat and you should be able to utilize these methods inside your own application alright so that wraps up link this will be the hardest thing that you have to think about today I guarantee it alright so that that's it we'll see in the next lesson thanks I'm Bob Tabor with developer university for more my training videos for beginners please visit me at devu.com in this lesson we will introduce a new decision statement the if else if else statement and the conditional operator are both great they work best when there's only a handful of things to evaluate but if you start needing to evaluate many different potential cases you might find that the switch statement is a little bit more concise and keeps things a little tidier and that would probably be the only one of the only reasons why you would use it and I'll show you a second reason why in this video as well so we'll come back to the switch in just a little bit but first I want to talk about a special data type called an enum or an enumeration typically we want to limit the possible values of a given variable now admittedly we're already limiting the possible values that can go into a variable by virtue of the fact that we've given it a specific data type however even within that I may want to limit the number of possible values to just a handful so typically in software development you want to limit and constrain your data to ensure its validity inside of your system and enumeration is a data type that limits and constrains all possible values to only those that are valid and have meaning within our system so for example we might want to keep a keep track of series of to-do items maybe that's the type of application we're building and each to-do item is represented by an instance of a to-do class and so we may want to keep track of the current status of a given to-do item on our list and so we may want to constrain the possible statuses to maybe like five that the task has not been started yet or it's in progress or it's on hold it's been completed or perhaps it's been deleted there might be some other statuses but you can see how I may want to just limit the number of options that are available for a status field or status property of my to-do class so we could do this in a number of different ways we could just concoct a numbering scheme where one is always represents not started and two represents in progress but numbers are I refer to those as magic numbers they may have some meaning in the system but it's not readily obvious if you're reading the source code you know as the developer you may have to look up at some external reference maybe some code comments and who knows maybe those aren't even maybe they're not even current anymore maybe things have changed since whoever wrote those code comments originally wrote them so I may need to reference or look through a number of different code to ensure that what number one means in the system what number two means in the system and so on the same thing can be true with strings I could just use a literal string to indicate the current status so I could use a literal string not started or in progress but the problem with literal strings is that somebody can mistype them or they could be you know there could be a space and not started one time and then somebody uses not started without a space the other time and so they may not if you're looking for all those items that have not been started yet you may have a hard time finding those that have not yet been started or in progress because they're not spelled correctly or whatever the case might be so the great thing about an enumeration is that it gives us a textual equivalent to a numerical value so that it will remove any ambiguity inside of the system as developers we know exactly what we're working with and yet behind the scenes it's still working with the number and enumerations are used frequently in the .NET Framework class library for the very same reasons so for example you can see here that I have a project enums and switch and here again if you look in the code folder for this lesson there should be a before and after you want to copy the enums and switch the project from the before folder and copy it into your projects directory so that you can catch up to me where I'm at at this moment you can always pause the video and type all this in if you like and be quite a bit of typing though you can see that in this project I've already created a to do class with a description of each to do item the estimated number of hours it should take to complete the to do item and then notice that I have a status of type status where does this come from well you can see directly below I've created an enumeration called status and I have not started in progress on hold completed and deleted did you notice as I hover my mouse cursor over it that each of these values are given and a numeric value so not started equals zero in progress equals one on hold equals two and so on if we were to store these values somehow in a database or a text file those are the values that might actually get stored however they will be translated into this more textual format so that when we're actually working with the data as you can see here in static void main as we're creating a new list of to do's using the collection initializer syntax here I'm setting the status equal to either completed or in progress or deleted or not started and so on and visually it's much easier for a developer to work with with the with those options in a more textual sort of way now the framework class library will use enumerations extensively in fact even in the console window if we were to set the foreground color notice that IntelliSense automatically pops up to the console color enumeration see it says enum over there whoops let's see I don't think you can see that let's go up a little bit here alright there okay so now you can see the word enum right here it's a console color enumeration and when I hit the the member access operator the period it will show me all the colors that we can choose for the foreground color of our of our console window so I might choose dark red okay so again enumerations are great because they are descriptive and they limit the number of possible values for our applications for the properties of our classes alright the next thing I want to talk about is the switch statement and these two are going to marry together here in just a moment but a simple switch statement looks something like this in fact I'm just going to go switch tab tab and that will create essentially the the outline for it and in this case we could do something like we can use an individual to do item and choose its oh let's say estimated hours and so if the estimated hours are for example case 4 alright then we might perform some operation until we hit the break statement or we could go case 5 so we could perform something and then we would hit the break statement and break out there's also a default case that would be the catch all just like the else statement in the if else if else construct but the most important aspect of this is the construct of the switch statement you have the keyword switch then you have a value a variable that's under evaluation and then a series of cases where we would try to match it up with one of these cases and then we use the colon after that case we write our code below it and then we use a break statement to break out and continue on our execution of the code now we might choose to for example do something like this this makes a little bit more sense to work with the statuses so in this case we might go status.completed we might do something versus case status.deleted and we might perform some operation there and so on now the beauty of the switch statement and the numerations are that they kind of can conspire together here so watch what I do I'm going to type switch tab tab and then I'm going to go to do which is each of the items in the to do collection dot status and then hit the enter key on my keyboard twice and the second time I hit enter see the macro that will actually blow out each of the individual statuses so that I can write code associated with each status alright isn't that crazy and so now I can do here you know anything that my business logic would require I have an idea though let's use the foreground color and change it up for each of the individual to do items so it can be as simple as this let's just copy this and we'll put it here for each of these statuses and then here at the very end we will actually then do a console dot right line the actual item itself the to do dot description and here we'll change up the color so if it's not started we'll we'll leave it as dark red if it's in progress we'll use green if it's on hold maybe we'll use yeah the dark red let's use the red here in the dark red there completed let's mark those as blue and if it's been deleted we'll mark that with yellow alright so now let's go ahead and save and run our application and you can see we have a very colorful list of tasks that are color coded by their current status see how cool that is alright so in this lesson we talked about enumerations and why we would use an enumeration to constrain the possible values for given property of our classes and we saw how it was used in the dotnet library one little instance of it here where they've created their own enumerations just be aware of as you're trying to work with a given class and its properties always look at for example in this case what data type it is so this is a data type console color and typically IntelliSense will point you kind of in the right direction as you hit the equal sign it will down to that data type that enumeration so that you can hit the period and then you can make your selections there so that's a really good hint and then we looked at the switch statement the structure of the switch statement where we're evaluating something in between the opening and closing parentheses we looked at the body, the opening and closing curly braces the entire body of the switch statement and then inside the creation of a number of cases each case equating one possible value of the item that's being evaluated and then a colon and after that any of the code that we want to write and finally a break line which will pop us out of that switch statements body and then finally we saw that there is a catch all the default colon which we can use to write any code for cases that we haven't accounted for in any of the other previous cases okay all right so that wraps up this lesson doing great we're getting close to the end now you feel pretty confident C-Sharp you've got the majority of it under your belt just a few more topics we want to cover and then we'll wrap this up we'll see you in the next lesson thank you Hi I'm Bob Taber with Developer University for more of my training videos for beginners please visit me at devu.com in this lesson we're going to talk about handling exceptions that occur within our applications we'll discuss what can go wrong why things go wrong and how to build resilient applications that are impervious to crashing through the use of C-Sharp's try catch block statement so when the compiler catches a data type mismatch or an unresolved reference to a class or some malformed C-Sharp instructions it'll refuse to compile your C-Sharp code into a .NET assembly until you fix the problem these type of errors are called compilation errors and that's not what we're talking about in this lesson however there are other types of errors that happen during run time or in other words they happen when the compile .NET assembly is actually in the act of executing and honestly there are countless number of reasons why you could encounter an exception while the application is running depending on the kinds of things you're trying to do in your application and many times these things are outside of the direct control of the software developer so for example if your application can't read or write to disk because a folder or a file is missing on disk where it expected to see it it could trigger a run time exception or maybe the files corrupt maybe the network access to that resource is unavailable or attempted perhaps to access a database and it couldn't find something in the database that it needed a table, a column, whatever the case might be because the structure has changed of the database out from under the application all of these things and many many many more could cause the absolute failure of your application and the user will see a nasty error message at run time and the they'll in frustrations say the developer so in some cases the developer may have not even foreseen that that could have potentially been an issue and so if they didn't see that it could be an issue they couldn't have counted for it maybe the developer for example allows the users to type in a country but the user misspells the country name maybe they did that intentionally or unintentionally perhaps they use maliciously use numerical characters instead of alpha characters but as a software developer your job is to make sure that you account for all of these possibilities a friend of mine was fond of saying that 80% of all code exists to solve 20% of all the potential problems that could happen in your application so generally software developers should be pessimistic regarding the reliability of everything outside of their control whether that be input from an user any connection to a network to the file system anything that the developer cannot directly control should be should be held in great suspicion so again if you rely on a file or a network resource you should treat it with great suspicion if you rely on the user to type data into your application definitely treat that with great suspicion it's absolutely evil ok so this is the software developers equivalent to driving defensively always code defensively which means you are always looking for problems all around you all the time now the way that the C sharp developer codes defensively or one of the ways in which they do it is through the use of a try catch block and I'll demonstrate that in this lesson up to this point we've been reading or actually writing files to disk this time I want to read a file to disk we use the same file class like we've used previously and notice that I already have a using statement for system.io so it finds it and this time I want to use the read all text instead of the write all text and here let's just go ahead and set this example up notice that I'm already I've already got a project created called handling exceptions please pause the video and catch up with me if you like but here we go string content equals file dot read all text and then let's just for the sake of argument hard code a location I'll put this at lesson 22 dot a slash example dot txt and then we'll do console dot write line the content and then finally console dot read line like so great alright so far so good and now here I just want to demonstrate that this actually will work so you can see that I created off my roots a lesson 22 folder with example dot txt in it let's go and run the application and show that there is actually text in that file it's just a quote from Mark Twain and now that we've got it working correctly let's break the application by by giving it a fake name just by removing the e on the end of example dot txt and now you can see that we get an exception this is a file not found exception was unhandled and so tell you what let's do this let's stop the application that's what the the developer will see while they're debugging their application if they were to run across this issue while they're building the application but what if we were to build a release version of the application by changing the solution configuration and then selecting build solution now we're going to go out to our projects directory and I'm going to act like an end user and actually attempt to use this application outside of visual studio so outside of the debugging environment just to see what the end user would see if they ran it to this exception at runtime the name of this is handling exceptions sorry there you go and let's go into the bin directory into the release folder and I'm going to double click the handling exception and whoops notice that I get this ugly little message here and it's trying to help me figure out what happened and I get all this text here with all this ugly information just a spewing out all this information that an end user probably would have absolutely no idea what the issue was although we can read here near the very top that the problem is that it couldn't find the file less than 22 slash example with no e.txt now a very observing user might be able to look at this and figure it out if they stare at it for a while but most users are going to be scared off by this and I don't blame them we ideally would like to make sure that the end user never sees anything like that whenever they run our application so again windows will then close the application notify you if there's a solution available there's just a mess going on there and we want to protect our end users from this mess from ever seeing this so what we can do is actually wrap a try statement a try and catch around this so we'll do this and there's a couple different ways to go about it I'm just going to take the easiest approach to begin with and what I might do is just go ahead and let's switch back to the debug configuration and run the application now you may have noticed that the application ran briefly and then went away and the reason it did was because we ran this we had an exception here the catch statement kicked in there's nothing defined in the catch statement and we continued on what if we were to move this to right there we would at least see the application now run for a little bit and we would see no output so still not an ideal situation but at least we're not seeing any exceptions let's go one more step with this and say let's actually catch the exception that occurred so here we're going to catch an exception call that we're going to call EX now exception is the most general the most general type of exception that can be thrown what we're going to look at in a few moments are very specific versions of exceptions but this is the most general version so that at least we can see what the problem is and we might do something like you know there was a problem something like that and even we could provide a description of the problem so the message from the exception so let's go and run the application and at least this time you know at least we're given the user some feedback here there was a problem could not find the file lesson 22 slash example dot txt so that's better again it would require an observant and slightly more technical and user to be able to resolve this issue on their own to say wait a second I wonder if that file might be named something different here on my own hard drive as they traverse through and look for the file and they're like oh I see what the problem is there's no E on the end of example that's asking a lot of your end user but that at least is a step in the right direction at least we're giving them some clue as to what the issue is now really what I would like to do is account for all the possibilities and be a little bit more specific so if I were to hover my mouse cursor over this read all text where the issue seems to be mostly you'll see that we've only been looking at the return value and the input parameters for a call to a method but notice below that there's a list of possible exceptions that could occur also if we were to go to system.io.file.readall text let me just copy this pop open let's go to Bing and we're basically going to be searching through Bing here for system.io.file.readall text that should help us find an article in MSDN that has a full description of this method you'll see that there's two overloaded versions we're using this first overloaded version of it and then if we were to scroll down and pass some of the initial information there's a list of exceptions and it would provide some scenarios why that particular exception might happen like a security exception the caller doesn't have the required permission the path is in a format interesting the file not found the file in the path was not found and there's also a directory not found interesting okay so maybe the path was too long or maybe we provided a null value so there's a lot of things that could potentially go wrong whenever calling this method and as developers we really need to to the extent that we can account for all those those potential situations at least the ones that make sense so for example I could re-write this code example to to begin catching some of those specific examples so for example let's take a look here first of all make sure that the directory exists and then if it does exist then we'll check to make sure the file exists and so then if the file in the directory exists but we're still getting exception then maybe we let it drop off to this most generalized exception here so I'm going to start for the most specific case and then work my way to the most general case in this case I think the file not found exception is probably the most the most specific of the ones that we're going to work with and then we'll catch the directory not found directory not found exception and then if that doesn't work we'll just print out whatever the exception was so here I might do something like console.WriteLine and say there was a problem and then give it a specific make sure the name is a name of the file is named correctly should be example .txt and then we can do something similar here and say there was a problem make sure that the name of the directory directory make sure make sure the file make sure the directory SQL slash lesson 22 exists something like that and remember we're getting the red squiggly why? because we either need to add another backslash here or add the add symbol there from earlier so let's go ahead and test our application and I'm going to set a breakpoint here whenever we hit this line of code so that we can watch this execute so let's go ahead and step over and it looks like it found the file not found exception and so we will see there was a problem make sure the name of the file is named correctly let's go out and let's rename this to lesson 22a and I think we're still going to get the same actually exception so maybe that's I don't know we did get the directory not found exception good ok so in this case we'll see that error message make sure the directory lesson 22 exists alright and then for any other exception maybe there's a permissions issue on the computer maybe there's a the file is corrupt somehow and we can't read from it we would get this last catch all where we catch just the general exception and print it to the user ok so at any rate the key to this is that we check the most specific exceptions first and the most general or generic exceptions last there's also one other item I want to add here and that's a finally statement and this is where we would we would write any any code to finalize which might mean setting objects to null closing database connections alright but this code is going to run no matter what and so we're just going to go console dot write line closing application now dot dot dot like so so that we can see that this code will run regardless it's just that one last chance as a developer to clean up our mess before we stop the execution of the application so and you can see that now represented here by closing the application now great now you might look at this and you might think to yourself ok great I'm going to use this try catch around everything in my application every single line of code in my application I'm going to wrap it with a try catch so just take every method and I'm going to blindly just copy and paste everything in there and that's definitely a strategy that some people take is a little bit lazy quite honestly some developers have done that but they're often ostracized by their end users for providing very cryptic error messages so if we were to you know leave all of these off and just wrap everything and just only show the exception EX we would just be saying hey there was a problem here figured out for yourself that's not really advocating on part of the user right we're not protecting the user furthermore we might be tempted to provide some type of debugging information for ourselves as developers sometimes you'll see some error codes pop up that no human could be expected to understand except the guy who originally wrote the application the reason developers do this is because sometimes they take that exact approach where they just say hey we're going to forget about the user I'm just going to wrap everything in a try catch and it'll pop the error message to me it'll flip back to me I'll fix the problem and everything will be ok but again this catch all is convenient to the developer but it's really frustrating to the end user so you shouldn't do that you should strive to put the same amount of attention into protecting your user from having these sorts of issues and protect them from having to guess at what to do next by simply helping them fix the problem tell them specifically if you possibly can if you the developer can fix the problem or at least you can point the user in the right direction then that's awesome you should do that but if you can't well then at least you know try to identify the exact nature of the problem and then ask the user for the type of input that you would need as the developer to fix the issue you don't want to leave your users feeling stupid that they that they did something wrong you want them to feel empowered and you want them to feel like your application is well built and it considered them whenever you were building it so that's what makes your application polished it what it's what users expect a reliable experience with no surprises alright so to recap this lesson we talked about a number of different things related to exceptions that can can happen essentially anytime that you the developer are not completely in control and you're accessing things outside of your your boundary of control outside of your domain you need to wrap those in a try catch and be thoughtful about the types of exceptions that you'll be handling listening for specific exceptions that you know a particular method could raise and it's easy to find out all you gotta do is hover your mouse cursor over it or you go to msdn and you find the call that method and you look for like we did all the potential exceptions that could happen and then you know be reasonable about it but then write the code necessary to handle those exceptions and protect your end user we looked at the try the catch we looked at catching the exception and then using certain properties of the exception like the message property to print out to the user what the issue was we could all you also use this to log the error even send it to a centralized logging service like application insights that's available from Microsoft Azure to report back to the developers what the issues were and then you can use the finally to clean up any connections you have to file systems databases you can set objects equal to null and go ahead remove all your references and be very explicit about that before you shut down the application okay so hopefully that was helpful this is a great lesson in building resilient applications giving the users the kind of experience that they would expect whenever things go wrong in your apps alright so we'll have one or two more lessons and then we're done we'll see in the next lesson thank you hi I'm Bob Taber with developer University for more of my training videos for beginners please visit me at devu.com in this final tutorial video in this course we're going to discuss event driven programming event driven programming is really at the heart of Microsoft's presentation APIs whether it be for web or windows and really for that matter it's at the heart of just about every other API in the .NET Framework class library it is so essential that we have to spend a little bit of time here near the end talking about it because it's that next step that will help you graduate on to building real applications with real user interfaces beyond this this course so events allow you the developer to respond by handling those key moments in the life cycle of the applications execution allowing you to write code to respond to an event being raised alright so up to this point in our simple console window applications that we've been building there's really only been one event that ever gets fired off and that is the application start up so on application start up the static void main is executed so it's handling that event I guess you could say and this is where we write the majority of our code and that's why it actually executes whenever we run the application now in a modern user interface whether it be for windows or for web users can interact with the various elements that they see on their screen so they can hover their mouse cursor over given things like buttons or graphics or text boxes and they can see maybe a change in the visual presentation maybe they see a pop up that explains the usage of that given item perhaps they can click on an item to enact some business functionality inside the application they can press keys on the keyboard to make things happen they can type inside of text fields or they can drag and drop items around the user interface and each of those will raise a number of events so as a software developer you can decide to write code that responds to those interactions between the end user and those various user interface elements on screen and you can also choose to ignore those that really don't make sense that you really don't care about you don't implement for your application so a given component let's say a button for example in its in its development by Microsoft they included or defined an event let's say it's the click event for that button now the developer you and I we say hey I want to write code that performs this business logic that I'm writing here in C sharp whenever that event the buttons click event is raised then I want this code that I write to be executed so the developer creates a method and attaches that method to the event and I'll show you how we do that in just a little bit here so as the application is running the user is interacting with the application eventually they click that button the dotnet framework runtime says okay if you were listening for the button click event here it is it just happened and it will notify every one of the methods that you and I developers have attached to that specific event notification all right that we are registered to that event and I'm going to show how events are used in a simple windows application near the end of this video in a more realistic scenario but first I want to start with the absolute basics and keep things as clean as possible so we're going to work purely in a console window application and we're going to work with a timer class a timer object and it has one event which is elapsed so we can say after a certain amount of time we want you a timer object to to execute or to raise an elapsed event and then we're going to attach our code our event handler code to that event so that it gets executed every time that event is raised all right so maybe it would be easier to see this in action than to explain it there are a number of different timer classes inside of the dynamic framework class library we want to make sure we get the right one so I want to work with system.timers.timer okay and I don't want to use that long name every single time so I'm just going to add that to my using statements up here at the top like so and I'm going to say timer my timer equals new timer and one of the overloaded versions of the constructor for this timer class allows us to pass in the interval in milliseconds so every let's say 2000 milliseconds we want the elapsed event to fire to be raised alright so 2000 milliseconds would be simply two seconds right which is an eternity as far as the computer is concerned okay so the next step what we're going to want to do is say alright my timer we know that it will raise an event called elapsed and so we want to create an event handler a method that will be executed whenever the elapsed event is raised by the .NET framework runtime so here you can see that we get this little message on screen that says press tab to insert and I press tab and it automatically the method stub called my timer elapsed so and it creates in a very specific way with a very specific method signature and it also gives us this little stubbed out hey don't forget you did not implement me so throw new exception not implemented exception let's go ahead and remove that for the moment but notice what happened here as well so we are asking or registering an event called my timer elapsed to the elapsed the elapsed event so this references this code block right here okay and so inside here we can write the code that we want to execute each and every time the elapsed event is triggered inside of our application so this is where I might write something like a console dot right line and the elapsed event will send along some event arguments and one of the interesting event arguments is actually the signal time and that will give me the exact down to the millisecond when that particular event was erased okay so here we go and I'll just go actually let's do it this way so that we can format it nicely we'll call this elapsed and then our minute second dot that should give us down to milliseconds and now what we'll do is actually tell the timer to start ticking by calling the start method and then we're going to go console dot read line and we're going to say continue running until somebody hits the enter key on the keyboard okay hopefully that makes sense and let's run the application and now we see every two seconds we get this message so you can see it 32, 34 seconds, 36 seconds, 38 seconds plus 1,000 milliseconds there we get that my timer elapsed method executing alright now let's do this let's say that we want more than one event handler to execute whenever the event is raised so I can do this all day long I can say hey well let's go ahead and add another method this time it'll be called my timer underscore elapsed one notice the one here I'm going to get rid of this little box again and inside of this new method I can do essentially the same thing and I'll just use that elapsed one versus elapsed in fact just to make this obvious I'm going to use that little trick we learned just a lesson or two ago where we set the foreground color equal to the console color red so that will be for the second one and then we'll do console dot foreground color equals console color dot white alright so we can see clearly the two different event handlers that are both executing whenever the elapsed event is raised by our timer so let's run the application alright and now we see the pair running every two seconds alright and we can continue adding additional event handlers to the event so that's what this little operator is doing is saying how many current items are subscribing or have been attached to this event I want you to attach this other one too ok now we can do the opposite as well in fact let's go console dot console dot right line and we'll say press enter to remove the red event probably a better way to say that but hopefully you get the idea and so then after this read line we'll add another read line for the very end of the application and in between what we'll do is actually unregister detach this second event handler for this event so let's do the reverse my timer dot elapsed minus equals my timer elapsed one so now we have removed it and it should no longer execute whenever the elapsed event is raised so let's run the application alright so you can see here we go and now I'm going to hit the enter keyboard and we should only see the white the first version of our event handler firing every two seconds ok hopefully that all makes sense alright so this is the most simple scenario that I could think up without having to actually create a real application and by real application I mean one with a graphical user interface but now that we've broached that topic let's go ahead and build an example WPF application WPF stands for Windows Presentation Foundation it's one of the APIs inside of the .NET Framework class library that you use to build Windows applications in other words applications that are executed on the Windows desktop not web pages that are executed on a server and their markup is delivered into a browser but a true application that's running on the end user's desktop so I'm going to go file a new project and here I want to make sure to choose WPF application it should be one of the templates that are installed in the new project dialog and what I'm going to call this is WPF events like so and then click OK and this is not going to be a tutorial on how to create Windows presentation application interfaces or how to work with it but I just want to show you the basics are generally the same so here we have a basic application we can actually run the application it will do nothing at all but I want to go to my tool box over here on the left hand side I'll even pin it down briefly and then inside of here what I'm going to go is to this rolled up area called common WPF controls now what you see on screen might be a little bit different than what I see on my screen just make sure you're working inside of this main window.sample and that you see some visual representation of your form here in the main area you can ignore everything below that's the actual markup that will generate what you see visually here we're only going to work with the visual editor but know that there's some markup that's going on to produce this but again that's a topic for another day I'm going to drag and drop a button onto the design surface like so I'm going to go over here to the properties window on the right hand side this will allow me to set various attributes of that object that visual object so for example I can change the content to click me like so I'm also going to add a label control I'm going to drag and drop it anywhere on this design surface I'm going to remove the content completely but I'm going to change the name to my label like so ok what I want to do is print out the phrase hello world whenever somebody clicks the click me button I'm going to choose the click me button again by selecting it here on in the visual editor and then I'm going to look for this little this little lightning bolt over here in the properties window and click the lightning bolt and this will show me a list of all of the events all of the events that this single control this button can raise now a lot of these are going to be for very specific situations and we can ignore the vast majority of these but the most important one here at the top is the click event now I can write C sharp code that will execute as a result of this click event being raised by the dotnet framework because somebody the user clicked on that click me button so I'm just going to double click here in this white area it created this button click method stub alright so this is going to be my event handler code let me use the auto hide pin to get rid of that so I can see this and here what I'm going to do is type in my label dot content equals hello world like so and then I'm going to save my work and then I'm going to start the application by running it I'm going to click the click me button and it displays the word hello world inside of the little label alright now what you didn't realize is that maybe that whenever we double clicked inside a visual studio in this little white area right here it created a event handler for us and it wired up or attached or registered that event to this button you might wonder where is the code that looks something like um button click plus equals you know where's that code at well that's a little bit difficult to describe if you take a look at this markup code here at the bottom and we scroll all the way to the right that is essentially what happens right here this code will get converted into C sharp at the point of compilation and it will create that little snippet that we were used to looking at in the previous code example however I can create a second event handler in C sharp by doing this let's go to the toolbox and I'm going to actually grab another label and just plop it down anywhere I'm going to select that label and go to the properties window and I'll change this to be named my other label and then I want to change the actual content of that label to be blank as well alright so now what I want to do then is go to the main window .sample.cs and here I'm going to go button underscore .click plus equals and I could click tab to insert but I'm not going to do that I'm going to name this manually myself button my other click like so and then I hit enter on the keyboard or actually I'm going to hit that and now I'm going to you can see that I get a red squiggly line I'm going to put my mouse cursor on that line and hit control period and then choose generate the method button underscore my other click and it does it for me and you'll see something that looks very familiar here a method stub with the not implemented exception and here I'm going to say my other label .content equals hello again like so alright so now if I did this correctly whenever my user clicks the button it will not only fire off this event handler but then also this event handler and this event handler we wired up manually using the technique that we learned in the previous code example so let's just run it and make sure this actually works and it does alright so the same principles are at play here the difference is the vast number of events that are accessible to every single visual control in your toolbox that you're working with the windows presentation foundation alright alright so the main takeaway of this lesson is that events are all around us and especially whenever working with windows and web applications we're going to write our code in methods that respond to specific events that are raised by the .NET framework runtime in response to events that are published by the various objects inside of our windows apps our web apps and so on and we can either rely on visual studio to wire things up for us in a very quick and elegant way like we saw here in our XAML code where we let it essentially do the wiring for us or we can take control of that process of wiring of attaching of registering an event handler to a specific event and then write the code ourselves to actually respond to that event being raised ok so again extremely important hopefully that's the next logical step for you is to move on to other APIs whether it be something like ASP.NET or WPF like we've worked with a little bit right here or the universal windows platform to build windows store applications and you'll need to know these concepts for all of those and that pretty much wraps up this lesson and this entire course will have a couple of closing comments in the next video and then that's it so we'll see you there, thanks