From the course: Learning C#

Hello World C# - C# Tutorial

From the course: Learning C#

Hello World C#

- [Instructor] It's a bit of a tradition in the software development world, to start off learning a new language by writing a hello world program. So let's do that for our first example. I'm going to auto-generate a C# console program, which is a program that, as we've already seen, uses the terminal, or console as its main user interface. So here in my terminal, I'm going to go into the start folder in my exercise files. And then into overview and then into hello world. And we'll see what's in here. So right now there's just a little read me file in here that explains what this folder is. So I'm going to create a new console app by running this command, I'm going to type dot net, new console, hit return, we can see the dot net is doing its thing, and after a few moments, we can see that it's succeeded, so now we have our new programs, let's do dir again, and now you can see that we have this hello world, dot CS project file, we have an empty object folder, and we have our program dot CS file. And that's where our code is going to go, so let's go open the code in our editor. So I'm going to jump over to Visual Studio Code, and then in start, overview, hello world, I'm going to click on program dot CS. And when I open the program dot CS file, you can see that there's a few lines of code that were auto-generated by .NET. So at the top of the file is this using systems statement, which indicates that our program will be using code that is in the .NET system namespace, and we'll get into that a little bit more later. So speaking of namespaces, we can see that's on the next line here, this is namespace, hello world. So namespaces help organize programs, and prevent names that are in your code from colliding with names that are in other third party libraries that you might include later, or in .NET itself. Next is the class program definition. So C#, and .NET as a whole, is an object oriented language, and all of your code is organized into classes. And we'll learn more about how to work with classes and other object oriented C# features later in the course, for now, you don't need to focus too much on the details of the program structure here. So the program class is where we put our main function, which is next, right here on line seven. So each C# console program has an entry point and that entry point is named main, so when .NET starts up the program, it's going to look for the main entry point and it's going to begin the program there. So this function has no return value and that's what the void word here means. And again, we'll learn more about functions a little bit later, and this array of strings are whatever the command line arguments that the app was started with. But again, you can just ignore all that for now. And then finally, we get to the first line of actual code. The console object represents a terminal and it's contained within the system namespace. And then right line is a function, or method, on the console object and it prints a string to the console, and then the line of code ends with a semi-colon which completes the statement. Now this is not optional, in C# you have to have semi-colons some languages like JavaScript don't require it, Python, you don't use them, in C# you do. So let's run what we already have, and to do that I'm going to type the command in the directory where my program is. Now remember, I can do that either by starting up the integrated terminal here, or by hopping back to my terminal. And what I'm going to do is just type dot net run, And so .NET will build the application, compile the code, build the executable and there you can see the output, it says hello world. All right, so that worked just fine. So we're going to be using the terminal a lot in this course, so let's try something else before we move on, let's have the program ask the user for their name and then print hello, along with their name. So let's go back to the code. So first I'll add another call to console write line, so I'll call console dot write line, and I'll type, what is your name? In double quotes, and I got that semi-colon. Then I'll add a line of code that creates a string variable, and we'll learn more about variables later, but for now just follow along with me. So I'll write string, str equals, and then I'm going to call console dot read line, and that will let the user type in a line of text. So where write line outputs a line of text, read line lets the user type in some text, followed by the carriage return or the enter key. And then finally we'll print hello one more time, along with the user's name. So I'm going to go ahead and copy this line, and paste it down here. And I'm going to write, why hello there, a space, and then plus str, which is the string variable that we just had the user enter. All right, so I'm going to go ahead and save, and now let's go back to the terminal, and in the terminal we'll run this again. And so it's going to rebuild my changes, so now here's the output, we can see hello world, what is your name? And I'll type in Joe. And it says, why hello there, Joe. All right, so we have our first program running, and we have it both inputting and outputting some content, but let's do one more thing, let's go back to the code. Now you might be saying to yourself, wow, that sure is a lot of boiler plate code just to do something basic. And you know what, you're absolutely right. So starting with C# nine, Microsoft gives you the option to drastically simplify this by getting rid of a lot of the namespace boiler plate and just using what are called top level statements. So I'm going to get rid of all this extra code, everything above the console write lines and read lines, so I'm going to delete all these curly braces, and then I'm going to indent each of these lines back to the beginning, and then just remove everything except for the statements. We don't have anything else, except for just the codes, I going to save this and now let's run this again. So we'll do dot net, run, and after few moments of compilation, okay, then we see again the same results. So I'll type in Joe again and I get the same output. So if you're just building a simple application, you can actually get rid of a lot of that boiler plate. Now we won't be doing this very much in the rest of the course, but I just wanted to demonstrate that it's possible to build simple programs without a whole lot of the extra code that .NET auto-generates.

Contents