Join Bruce Van Horn for an in-depth discussion in this video Creating a console application, part of C# Essential Training: 1 Syntax and Object Oriented Programming.
- [Instructor] Now that we've got Visual Studio installed, why don't we take it for a test drive? We'll create our very first project in C Sharp, and I'm going to show you how to make probably the simplest kind of program in C Sharp, and that's a console app. To do this, let's go to File, New, Project. Make sure that you're in the section labeled Visual C Sharp. You don't want to wind up in Visual C++ or Visual Basic. You have similar types in there, so let's just make sure that you're in the right place.
Visual C Sharp, and you're looking for Console App. I'm going to call my console app First Console App. Make note of the location where you're saving your code, and let's click OK. Visual Studio will generate bunch of files for us that we need for our project. If you look over on the right, you'll find, in Visual Studio, the Solution Explorer.
The Solution Explorer is the part of Visual Studio that tracks all of the files that make up your application. So we can see that we have program dot cs here, and C Sharp files bear the dot cs extension. And we can see that it's automatically opened programd dot cs for us over here in the editor. There are a number of other things in the Solution Explorer that don't necessarily correspond to files, for example, properties and references aren't actually files on your system, those are just project settings that exist within your Visual Studio solution files.
So let's write a little bit of code. At the top, you'll see a bunch of using statements, and right now they're grayed out, meaning that we haven't used any of them yet. Visual Studio puts the most commonly used ones in there automatically for us, so for now we're going to leave them there. Visual Studio has generated a namespace for us. We'll learn about that a little bit later. Everything in C Sharp is object-oriented, so we have a class, and it's called Program, and inside of P rogram we have one method and that's the entry point for our program.
We'll cover all of this in greater detail as we go. Let's just write a few lines of code here so that we can get the feel for Visual Studio and the C Sharp syntax. So all we're going to do here is we're just going to write some lines out to the screen. So I'll say Console, and you'll notice as I start typing I get IntelliSense. This is the hinting system in Visual Studio. It really cuts down on the amount of typing that you need to do. As soon as you see a match in the window, you can just hit Tab, and it'll fill the rest of it in for you.
So we're going to write console dot write line, and we're just going to put some simple text in here. Welcome to C Sharp Essential Training Part 1. All code lines in C Sharp terminate with a semicolon. It's like using the period at the end of the sentence, except it's a semicolon, and let's add a couple of more lines. For this, we're actually going to have a little bit of a pause in here.
So I'll say write line press enter to continue. And the reason we do this is when we run this program, it's going to run so fast that you won't even be able to see it unless you introduce some sort of pause here. So here's the pause part, I'm going to say console dot read line, and that is our very first program. So console dot write line outputs text to the console window.
Console dot read line waits until we press Enter, so once we do press Enter, the program is going to execute, because there's no more code after that. Make sure you've saved everything. You can tell whether or not you've saved based on the green line over here. You might have noticed as we changed things that we get these yellow bars. That's code that has not yet been saved, and this is a great visual marker. It shows you what areas of your file that you've recently changed and how much of it is saved versus how much of it is not saved.
Incidentally, this is a comment. This is ignored by the compiler, you use this for documentation, recording your innermost thoughts as you write your code so that when you come back to it later you can figure out what you were thinking when you wrote it. We'll just take that out for now, though, and we'll save everything. I can tell it's all saved because this is green, and now I'll run this. I can come to the top here, find the button labeled Start, click Start. My program will build, and we'll see our console app appear.
So we can see our output, Welcome to C Sharp Essential Training Part 1, press enter to continue. One thing I want to point out is that when you're running your program, you can't alter your code, and you can tell that you're running, aside from the obvious console app window on the screen, by looking down at the bottom and noting that this bar down here turned orange. A minute ago it was blue. So any time you've got an orange bar down here, that means that you've got something running. I can dismiss my program by pressing Enter a couple of times, and then it will go away and stop on its own.
So that's your very first C Sharp program.
- The history of C#
- Setting up your development environment in Visual Studio
- Declaring variables
- Working with strings
- Using mathematical operations
- Creating classes and properties
- Using expressions
- Using static, abstract, and virtual methods
- Building a user interface
- Extending classes
- Working with subclassed objects
- Using the object-oriented features of C#