Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
C# is a very popular programming language for writing .NET applications. Many people consider it the de facto language for .NET, and rarely consider using the other in-box languages. For most of the examples in this course, I will be using C#, so this is a good time to explore the basics of the C# language and its code editor. I'm inside Visual Studio, and I've opened this project called CSharpEditor. I'm going to double-click on this Program.cs file to open it up in the code editor, and then we're going to start writing our code here on my machine on line 11 in this static void Main method, which, since this is a console application, is considered the starting point of the app.
I just pressed the Enter key to enter a new line of code. You'll notice that on the left margin there's a yellow marker over there now. That signifies that I have made a change since I've opened this file, but I haven't saved the changes yet. If I save the file by clicking on this Save button up here on the toolbar, you'll notice that it changes to a green bar. That signifies that I have made some changes since I opened the project, but I have currently saved them to the hard drive.
So it's a status symbol. I'm going to write some code to read and write from the console. I'm going to start by writing "hello" to the console. I'll type in the word "Console". Notice that I get IntelliSense; I get this dropdown window. I have more details on IntelliSense in another movie later in this title. And then I'm going to press the Tab key to finish typing. Then I'll type dot, and then, the word "WriteLine". I've got enough of the word WriteLine written now, so I can just press the Tab key again.
Then the open paren, close paren, and the semicolon. In C# you always end your lines of code with your semicolons, and you always write your code within curly braces. What do I want to write? I need to put a string in here, so I'm going to say "Hello". If I come up and I run the application at this moment by saying Start Debugging, the application will start and run, and immediately stop. It ran so fast you might not even have seen that flash on the screen.
I need to wait to see the results of the application. So, Visual Studio provides this mechanism, Debug > Start Without Debugging. Let's see the difference. I'll say Start Without Debugging. It prints my application. My application is now terminated, but it leaves it on the screen so that I can see the end results. When I'm done looking at the end results, I can press any key on my keyboard, and it'll then terminate that window. If I'd rather not run using Start Without Debugging, I can write my own line of code to listen to the users keystrokes by typing in "Console.ReadLine". And what this does is it stops, waits for the user to type something in the keyboard, and then press Enter before it continues.
Let me modify this a bit. "Hello. What is your name?" Then on my ReadLine, I'm going to declare a variable to hold the user's name. I'm going to do that here on line 15 on my computer. I'm going to say "string name =", and then I'll copy this code; Ctrl+C. Paste it in; Ctrl+V. So, what this is going to do it's going to say "Hello. What is your name?" and then I'm going to read from the Console whatever the user types, and then I'm going to wait for them to type something else in.
That way, the application won't end immediately. And then down here, I am going to output whatever the user typed in back to the console. Now, down here, I'll say "Console.WriteLine", and then I need my semicolon at the end. So, what am I going to do here? I'm going to say "Hello", and then this is the concatenation operator, meaning I'm going to take one string from the user and I'm going to add it to the end of the other string from my code.
I'm going to say "Hello "+ name. I think that's good; let's try it out. Debug > Start Debugging. It says, "Hello. What is your name?" I'm going to type my name in, "Walt Ritscher", and then press the Enter key, and then I see my name mirrored back to me, "Hello, Walt Ritscher." It's now waiting for me to press the Enter key to terminate the application. I showed you one way to create a variable. There's another way to create a variable that's very popular now in C#. You can use the var keyword.
The var keyword says to the compiler, "I'm going to declare a variable," demo, "and I want you to figure out the type of the variable." So, if I say var demo = 6;, that's telling the compiler to look at this number 6. Let me hover my cursor over it. And that is an Int32, which means it's a 32-bit integer, and I want you to make this variable of type Int32. This is the same as me typing in "Int32 demo" like that.
These are equivalent lines of code. I'm a personal fan of using the var keyword. I like using it a lot. Sometimes you're going to work with code that has long names. Let me show you another example: var fs = new System.IO.FileStream();. I'm going to press the Tab key to finish typing here. So again, I'm asking to the compiler to figure out the data type of this variable. It's going to look at the right side of the assignment operator and see that I'm asking for it to make a new instance of the FileStream class.
So it's going to make this variable type FileStream. There is a way to limit how much typing I have to do. I can eliminate having to type this namespace at the beginning of the word "FileStream" by going to the top of my code, up here at the top of my Code window, and typing in what's called a using directive, "using System.IO;". Now, down here in the body of my code, I can eliminate the word "System.IO". So this cleans up my code quite a bit and makes it a little more readable while I'm writing my application.
Now you've seen the basics of reading and writing information to the console. C# is a powerful language and a perfect companion to working in .NET. This is not a C# tutorial, however, so if you are new to the language, you will need to dig into the documentation. Be sure and check lynda.com for other movies related to this topic.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101370 Viewers
61 Video lessons · 88126 Viewers
71 Video lessons · 71965 Viewers
56 Video lessons · 103789 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.